blob: 5825bf1d1b00172339c5ffbf96d46c66dee1524f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar4033c552017-09-16 20:54:51 +0200111#if !defined(FEAT_LISTCMDS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# 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
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200125# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000127# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200129#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130# define ex_cclose ex_ni
131# define ex_copen ex_ni
132# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200133# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000135#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
136# define ex_cexpr ex_ni
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static int check_more(int, int);
Bram Moolenaarded27822017-01-02 14:27:34 +0100140static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200142#if !defined(FEAT_PERL) \
143 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
144 || !defined(FEAT_TCL) \
145 || !defined(FEAT_RUBY) \
146 || !defined(FEAT_LUA) \
147 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000148# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100149static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static char_u *invalid_range(exarg_T *eap);
152static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000153#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100154static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000155#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100156static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
157static void ex_highlight(exarg_T *eap);
158static void ex_colorscheme(exarg_T *eap);
159static void ex_quit(exarg_T *eap);
160static void ex_cquit(exarg_T *eap);
161static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100162static void ex_close(exarg_T *eap);
163static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
164static void ex_only(exarg_T *eap);
165static void ex_resize(exarg_T *eap);
166static void ex_stag(exarg_T *eap);
167static void ex_tabclose(exarg_T *eap);
168static void ex_tabonly(exarg_T *eap);
169static void ex_tabnext(exarg_T *eap);
170static void ex_tabmove(exarg_T *eap);
171static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200172#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100173static void ex_pclose(exarg_T *eap);
174static void ex_ptag(exarg_T *eap);
175static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#else
177# define ex_pclose ex_ni
178# define ex_ptag ex_ni
179# define ex_pedit ex_ni
180#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static void ex_hide(exarg_T *eap);
182static void ex_stop(exarg_T *eap);
183static void ex_exit(exarg_T *eap);
184static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100186static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#else
188# define ex_goto ex_ni
189#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static void ex_shell(exarg_T *eap);
191static void ex_preserve(exarg_T *eap);
192static void ex_recover(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#ifndef FEAT_LISTCMDS
194# define ex_argedit ex_ni
195# define ex_argadd ex_ni
196# define ex_argdelete ex_ni
197# define ex_listdo ex_ni
198#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100199static void ex_mode(exarg_T *eap);
200static void ex_wrongmodifier(exarg_T *eap);
201static void ex_find(exarg_T *eap);
202static void ex_open(exarg_T *eap);
203static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_GUI
205# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100206static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#endif
208#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#else
211# define ex_tearoff ex_ni
212#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000213#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#else
216# define ex_popup ex_ni
217#endif
218#ifndef FEAT_GUI_MSWIN
219# define ex_simalt ex_ni
220#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000221#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222# define gui_mch_find_dialog ex_ni
223# define gui_mch_replace_dialog ex_ni
224#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000225#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226# define ex_helpfind ex_ni
227#endif
228#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100229# define ex_cscope ex_ni
230# define ex_scscope ex_ni
231# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif
233#ifndef FEAT_SYN_HL
234# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200235# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000236#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200237#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200238# define ex_syntime ex_ni
239#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000240#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000241# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000242# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000243# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000244# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000245# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200247#ifndef FEAT_PERSISTENT_UNDO
248# define ex_rundo ex_ni
249# define ex_wundo ex_ni
250#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200251#ifndef FEAT_LUA
252# define ex_lua ex_script_ni
253# define ex_luado ex_ni
254# define ex_luafile ex_ni
255#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000256#ifndef FEAT_MZSCHEME
257# define ex_mzscheme ex_script_ni
258# define ex_mzfile ex_ni
259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#ifndef FEAT_PERL
261# define ex_perl ex_script_ni
262# define ex_perldo ex_ni
263#endif
264#ifndef FEAT_PYTHON
265# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200266# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267# define ex_pyfile ex_ni
268#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200269#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200270# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200271# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272# define ex_py3file ex_ni
273#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100274#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
275# define ex_pyx ex_script_ni
276# define ex_pyxdo ex_ni
277# define ex_pyxfile ex_ni
278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279#ifndef FEAT_TCL
280# define ex_tcl ex_script_ni
281# define ex_tcldo ex_ni
282# define ex_tclfile ex_ni
283#endif
284#ifndef FEAT_RUBY
285# define ex_ruby ex_script_ni
286# define ex_rubydo ex_ni
287# define ex_rubyfile ex_ni
288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifndef FEAT_KEYMAP
290# define ex_loadkeymap ex_ni
291#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100292static void ex_swapname(exarg_T *eap);
293static void ex_syncbind(exarg_T *eap);
294static void ex_read(exarg_T *eap);
295static void ex_pwd(exarg_T *eap);
296static void ex_equal(exarg_T *eap);
297static void ex_sleep(exarg_T *eap);
298static void do_exmap(exarg_T *eap, int isabbrev);
299static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100300static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000301#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100302static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#else
304# define ex_winpos ex_ni
305#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100306static void ex_operators(exarg_T *eap);
307static void ex_put(exarg_T *eap);
308static void ex_copymove(exarg_T *eap);
309static void ex_submagic(exarg_T *eap);
310static void ex_join(exarg_T *eap);
311static void ex_at(exarg_T *eap);
312static void ex_bang(exarg_T *eap);
313static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200314#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_wundo(exarg_T *eap);
316static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200317#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_redo(exarg_T *eap);
319static void ex_later(exarg_T *eap);
320static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_redrawstatus(exarg_T *eap);
322static void close_redir(void);
323static void ex_mkrc(exarg_T *eap);
324static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100326static char_u *uc_fun_cmd(void);
327static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_startinsert(exarg_T *eap);
330static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_checkpath(exarg_T *eap);
333static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_findpat ex_ni
336# define ex_checkpath ex_ni
337#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200338#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340#else
341# define ex_psearch ex_ni
342#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100343static void ex_tag(exarg_T *eap);
344static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#ifndef FEAT_EVAL
346# define ex_scriptnames ex_ni
347# define ex_finish ex_ni
348# define ex_echo ex_ni
349# define ex_echohl ex_ni
350# define ex_execute ex_ni
351# define ex_call ex_ni
352# define ex_if ex_ni
353# define ex_endif ex_ni
354# define ex_else ex_ni
355# define ex_while ex_ni
356# define ex_continue ex_ni
357# define ex_break ex_ni
358# define ex_endwhile ex_ni
359# define ex_throw ex_ni
360# define ex_try ex_ni
361# define ex_catch ex_ni
362# define ex_finally ex_ni
363# define ex_endtry ex_ni
364# define ex_endfunction ex_ni
365# define ex_let ex_ni
366# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000367# define ex_lockvar ex_ni
368# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369# define ex_function ex_ni
370# define ex_delfunction ex_ni
371# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000372# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100374static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100376static int makeopens(FILE *fd, char_u *dirnow);
377static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
378static void ex_loadview(exarg_T *eap);
379static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000380static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#else
382# define ex_loadview ex_ni
383#endif
384#ifndef FEAT_EVAL
385# define ex_compiler ex_ni
386#endif
387#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100388static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#else
390# define ex_viminfo ex_ni
391#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100392static void ex_behave(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100394static void ex_filetype(exarg_T *eap);
395static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#else
397# define ex_filetype ex_ni
398# define ex_setfiletype ex_ni
399#endif
400#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000401# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402# define ex_diffpatch ex_ni
403# define ex_diffgetput ex_ni
404# define ex_diffsplit ex_ni
405# define ex_diffthis ex_ni
406# define ex_diffupdate ex_ni
407#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static void ex_digraphs(exarg_T *eap);
409static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
411# define ex_options ex_ni
412#endif
413#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static void ex_nohlsearch(exarg_T *eap);
415static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#else
417# define ex_nohlsearch ex_ni
418# define ex_match ex_ni
419#endif
420#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100421static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422#else
423# define ex_X ex_ni
424#endif
425#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100426static void ex_fold(exarg_T *eap);
427static void ex_foldopen(exarg_T *eap);
428static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#else
430# define ex_fold ex_ni
431# define ex_foldopen ex_ni
432# define ex_folddo ex_ni
433#endif
434#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
435 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
436# define ex_language ex_ni
437#endif
438#ifndef FEAT_SIGNS
439# define ex_sign ex_ni
440#endif
441#ifndef FEAT_SUN_WORKSHOP
442# define ex_wsverb ex_ni
443#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000444#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200445# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000446# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200447# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449
450#ifndef FEAT_EVAL
451# define ex_debug ex_ni
452# define ex_breakadd ex_ni
453# define ex_debuggreedy ex_ni
454# define ex_breakdel ex_ni
455# define ex_breaklist ex_ni
456#endif
457
458#ifndef FEAT_CMDHIST
459# define ex_history ex_ni
460#endif
461#ifndef FEAT_JUMPLIST
462# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200463# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464# define ex_changes ex_ni
465#endif
466
Bram Moolenaar05159a02005-02-26 23:04:13 +0000467#ifndef FEAT_PROFILE
468# define ex_profile ex_ni
469#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200470#ifndef FEAT_TERMINAL
471# define ex_terminal ex_ni
472#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000473
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474/*
475 * Declare cmdnames[].
476 */
477#define DO_DECLARE_EXCMD
478#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200479#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100480
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481static char_u dollar_command[2] = {'$', 0};
482
483
484#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000485/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486typedef struct
487{
488 char_u *line; /* command line */
489 linenr_T lnum; /* sourcing_lnum of the line */
490} wcmd_T;
491
492/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000493 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000495 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000497struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 garray_T *lines_gap; /* growarray with line info */
500 int current_line; /* last read line from growarray */
501 int repeating; /* TRUE when looping a second time */
502 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100503 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 void *cookie;
505};
506
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100507static char_u *get_loop_line(int c, void *cookie, int indent);
508static int store_loop_line(garray_T *gap, char_u *line);
509static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000510
511/* Struct to save a few things while debugging. Used in do_cmdline() only. */
512struct dbg_stuff
513{
514 int trylevel;
515 int force_abort;
516 except_T *caught_stack;
517 char_u *vv_exception;
518 char_u *vv_throwpoint;
519 int did_emsg;
520 int got_int;
521 int did_throw;
522 int need_rethrow;
523 int check_cstack;
524 except_T *current_exception;
525};
526
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100527static void save_dbg_stuff(struct dbg_stuff *dsp);
528static void restore_dbg_stuff(struct dbg_stuff *dsp);
Bram Moolenaared203462004-06-16 11:19:22 +0000529
530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100531save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000532{
533 dsp->trylevel = trylevel; trylevel = 0;
534 dsp->force_abort = force_abort; force_abort = FALSE;
535 dsp->caught_stack = caught_stack; caught_stack = NULL;
536 dsp->vv_exception = v_exception(NULL);
537 dsp->vv_throwpoint = v_throwpoint(NULL);
538
539 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
540 dsp->did_emsg = did_emsg; did_emsg = FALSE;
541 dsp->got_int = got_int; got_int = FALSE;
542 dsp->did_throw = did_throw; did_throw = FALSE;
543 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
544 dsp->check_cstack = check_cstack; check_cstack = FALSE;
545 dsp->current_exception = current_exception; current_exception = NULL;
546}
547
548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100549restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000550{
551 suppress_errthrow = FALSE;
552 trylevel = dsp->trylevel;
553 force_abort = dsp->force_abort;
554 caught_stack = dsp->caught_stack;
555 (void)v_exception(dsp->vv_exception);
556 (void)v_throwpoint(dsp->vv_throwpoint);
557 did_emsg = dsp->did_emsg;
558 got_int = dsp->got_int;
559 did_throw = dsp->did_throw;
560 need_rethrow = dsp->need_rethrow;
561 check_cstack = dsp->check_cstack;
562 current_exception = dsp->current_exception;
563}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564#endif
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566/*
567 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
568 * command is given.
569 */
570 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100571do_exmode(
572 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
574 int save_msg_scroll;
575 int prev_msg_row;
576 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100577 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000578
579 if (improved)
580 exmode_active = EXMODE_VIM;
581 else
582 exmode_active = EXMODE_NORMAL;
583 State = NORMAL;
584
585 /* When using ":global /pat/ visual" and then "Q" we return to continue
586 * the :global command. */
587 if (global_busy)
588 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 save_msg_scroll = msg_scroll;
591 ++RedrawingDisabled; /* don't redisplay the window */
592 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#ifdef FEAT_GUI
594 /* Ignore scrollbar and mouse events in Ex mode */
595 ++hold_gui_events;
596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
598 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
599 while (exmode_active)
600 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000601 /* Check for a ":normal" command and no more characters left. */
602 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
603 {
604 exmode_active = FALSE;
605 break;
606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 msg_scroll = TRUE;
608 need_wait_return = FALSE;
609 ex_pressedreturn = FALSE;
610 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100611 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 prev_msg_row = msg_row;
613 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (improved)
615 {
616 cmdline_row = msg_row;
617 do_cmdline(NULL, getexline, NULL, 0);
618 }
619 else
620 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
621 lines_left = Rows - 1;
622
Bram Moolenaardf177f62005-02-22 08:39:57 +0000623 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100624 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000626 if (curbuf->b_ml.ml_flags & ML_EMPTY)
627 EMSG(_(e_emptybuf));
628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000630 if (ex_pressedreturn)
631 {
632 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000633 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000634 msg_row = prev_msg_row;
635 if (prev_msg_row == Rows - 1)
636 msg_row--;
637 }
638 msg_col = 0;
639 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
640 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000643 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
644 {
645 if (curbuf->b_ml.ml_flags & ML_EMPTY)
646 EMSG(_(e_emptybuf));
647 else
648 EMSG(_("E501: At end-of-file"));
649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 }
651
652#ifdef FEAT_GUI
653 --hold_gui_events;
654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 --RedrawingDisabled;
656 --no_wait_return;
657 update_screen(CLEAR);
658 need_wait_return = FALSE;
659 msg_scroll = save_msg_scroll;
660}
661
662/*
663 * Execute a simple command line. Used for translated commands like "*".
664 */
665 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100666do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 return do_cmdline(cmd, NULL, NULL,
669 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
670}
671
672/*
673 * do_cmdline(): execute one Ex command line
674 *
675 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100676 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * 2. Split up in parts separated with '|'.
678 *
679 * This function can be called recursively!
680 *
681 * flags:
682 * DOCMD_VERBOSE - The command will be included in the error message.
683 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100684 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 * DOCMD_KEYTYPED - Don't reset KeyTyped.
686 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
687 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
688 *
689 * return FAIL if cmdline could not be executed, OK otherwise
690 */
691 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100692do_cmdline(
693 char_u *cmdline,
694 char_u *(*fgetline)(int, void *, int),
695 void *cookie, /* argument for fgetline() */
696 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
698 char_u *next_cmdline; /* next cmd to execute */
699 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100700 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 static int recursive = 0; /* recursive depth */
702 int msg_didout_before_start = 0;
703 int count = 0; /* line number count */
704 int did_inc = FALSE; /* incremented RedrawingDisabled */
705 int retval = OK;
706#ifdef FEAT_EVAL
707 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000708 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 int current_line = 0; /* active line in lines_ga */
710 char_u *fname = NULL; /* function or script name */
711 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
712 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000713 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 int initial_trylevel;
715 struct msglist **saved_msg_list = NULL;
716 struct msglist *private_msg_list;
717
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100719 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000721 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000723 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100725# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726# define cmd_cookie cookie
727#endif
728 static int call_depth = 0; /* recursiveness */
729
730#ifdef FEAT_EVAL
731 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
732 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000733 * This ensures that the do_errthrow() call in do_one_cmd() does not
734 * combine the messages stored by an earlier invocation of do_one_cmd()
735 * with the command name of the later one. This would happen when
736 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 saved_msg_list = msg_list;
738 msg_list = &private_msg_list;
739 private_msg_list = NULL;
740#endif
741
742 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100743 * here. The value of 200 allows nested function calls, ":source", etc.
744 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100745 if (call_depth >= 200
746#ifdef FEAT_EVAL
747 && call_depth >= p_mfd
748#endif
749 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {
751 EMSG(_("E169: Command too recursive"));
752#ifdef FEAT_EVAL
753 /* When converting to an exception, we do not include the command name
754 * since this is not an error of the specific command. */
755 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
756 msg_list = saved_msg_list;
757#endif
758 return FAIL;
759 }
760 ++call_depth;
761
762#ifdef FEAT_EVAL
763 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000764 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 cstack.cs_trylevel = 0;
766 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
769
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100773 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 ++ex_nesting_level;
776
777 /* Get the function or script name and the address where the next breakpoint
778 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 {
781 fname = func_name(real_cookie);
782 breakpoint = func_breakpoint(real_cookie);
783 dbg_tick = func_dbg_tick(real_cookie);
784 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
787 fname = sourcing_name;
788 breakpoint = source_breakpoint(real_cookie);
789 dbg_tick = source_dbg_tick(real_cookie);
790 }
791
792 /*
793 * Initialize "force_abort" and "suppress_errthrow" at the top level.
794 */
795 if (!recursive)
796 {
797 force_abort = FALSE;
798 suppress_errthrow = FALSE;
799 }
800
801 /*
802 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000803 * exception handling (used when debugging). Otherwise clear it to avoid
804 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000806 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000807 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000808 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200809 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811 initial_trylevel = trylevel;
812
813 /*
814 * "did_throw" will be set to TRUE when an exception is being thrown.
815 */
816 did_throw = FALSE;
817#endif
818 /*
819 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000820 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 * If force_abort is set, we cancel everything.
822 */
823 did_emsg = FALSE;
824
825 /*
826 * KeyTyped is only set when calling vgetc(). Reset it here when not
827 * calling vgetc() (sourced command lines).
828 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 if (!(flags & DOCMD_KEYTYPED)
830 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 KeyTyped = FALSE;
832
833 /*
834 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000835 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * - for multiple commands on one line, separated with '|'
837 * - when repeating until there are no more lines (for ":source")
838 */
839 next_cmdline = cmdline;
840 do
841 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844#endif
845
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000846 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (next_cmdline == NULL
848#ifdef FEAT_EVAL
849 && !force_abort
850 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852#endif
853 )
854 did_emsg = FALSE;
855
856 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000857 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100858 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 * 3. If a line is given: Make a copy, so we can mess with it.
860 */
861
862#ifdef FEAT_EVAL
863 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000864 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
866 /* Each '|' separated command is stored separately in lines_ga, to
867 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100868 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 /* Check if a function has returned or, unless it has an unclosed
871 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000872 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000874# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000875 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000876 func_line_end(real_cookie);
877# endif
878 if (func_has_ended(real_cookie))
879 {
880 retval = FAIL;
881 break;
882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000885 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887 script_line_end();
888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100891 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
893 retval = FAIL;
894 break;
895 }
896
897 /* If breakpoints have been added/deleted need to check for it. */
898 if (breakpoint != NULL && dbg_tick != NULL
899 && *dbg_tick != debug_tick)
900 {
901 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100902 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 fname, sourcing_lnum);
904 *dbg_tick = debug_tick;
905 }
906
907 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
908 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
909
910 /* Did we encounter a breakpoint? */
911 if (breakpoint != NULL && *breakpoint != 0
912 && *breakpoint <= sourcing_lnum)
913 {
914 dbg_breakpoint(fname, sourcing_lnum);
915 /* Find next breakpoint. */
916 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100917 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 fname, sourcing_lnum);
919 *dbg_tick = debug_tick;
920 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000921# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000922 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000923 {
924 if (getline_is_func)
925 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100926 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 script_line_start();
928 }
929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
931
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000932 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000934 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100935 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * below, so that it stores lines in or reads them from
937 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 * while/for loop. */
939 cmd_getline = get_loop_line;
940 cmd_cookie = (void *)&cmd_loop_cookie;
941 cmd_loop_cookie.lines_gap = &lines_ga;
942 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100943 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000944 cmd_loop_cookie.cookie = cookie;
945 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947 else
948 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100949 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 cmd_cookie = cookie;
951 }
952#endif
953
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100954 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (next_cmdline == NULL)
956 {
957 /*
958 * Need to set msg_didout for the first line after an ":if",
959 * otherwise the ":if" will be overwritten.
960 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100961 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100963 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#ifdef FEAT_EVAL
965 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
966#else
967 0
968#endif
969 )) == NULL)
970 {
971 /* Don't call wait_return for aborted command line. The NULL
972 * returned for the end of a sourced file or executed function
973 * doesn't do this. */
974 if (KeyTyped && !(flags & DOCMD_REPEAT))
975 need_wait_return = FALSE;
976 retval = FAIL;
977 break;
978 }
979 used_getline = TRUE;
980
981 /*
982 * Keep the first typed line. Clear it when more lines are typed.
983 */
984 if (flags & DOCMD_KEEPLINE)
985 {
986 vim_free(repeat_cmdline);
987 if (count == 0)
988 repeat_cmdline = vim_strsave(next_cmdline);
989 else
990 repeat_cmdline = NULL;
991 }
992 }
993
994 /* 3. Make a copy of the command so we can mess with it. */
995 else if (cmdline_copy == NULL)
996 {
997 next_cmdline = vim_strsave(next_cmdline);
998 if (next_cmdline == NULL)
999 {
1000 EMSG(_(e_outofmem));
1001 retval = FAIL;
1002 break;
1003 }
1004 }
1005 cmdline_copy = next_cmdline;
1006
1007#ifdef FEAT_EVAL
1008 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001009 * Save the current line when inside a ":while" or ":for", and when
1010 * the command looks like a ":while" or ":for", because we may need it
1011 * later. When there is a '|' and another command, it is stored
1012 * separately, because we need to be able to jump back to it from an
1013 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001015 if (current_line == lines_ga.ga_len
1016 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001018 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
1020 retval = FAIL;
1021 break;
1022 }
1023 }
1024 did_endif = FALSE;
1025#endif
1026
1027 if (count++ == 0)
1028 {
1029 /*
1030 * All output from the commands is put below each other, without
1031 * waiting for a return. Don't do this when executing commands
1032 * from a script or when being called recursive (e.g. for ":e
1033 * +command file").
1034 */
1035 if (!(flags & DOCMD_NOWAIT) && !recursive)
1036 {
1037 msg_didout_before_start = msg_didout;
1038 msg_didany = FALSE; /* no output yet */
1039 msg_start();
1040 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001041 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 ++RedrawingDisabled;
1043 did_inc = TRUE;
1044 }
1045 }
1046
1047 if (p_verbose >= 15 && sourcing_name != NULL)
1048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001050 verbose_enter_scroll();
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 smsg((char_u *)_("line %ld: %s"),
1053 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001054 if (msg_silent == 0)
1055 msg_puts((char_u *)"\n"); /* don't overwrite this */
1056
1057 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 --no_wait_return;
1059 }
1060
1061 /*
1062 * 2. Execute one '|' separated command.
1063 * do_one_cmd() will return NULL if there is no trailing '|'.
1064 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1065 */
1066 ++recursive;
1067 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1068#ifdef FEAT_EVAL
1069 &cstack,
1070#endif
1071 cmd_getline, cmd_cookie);
1072 --recursive;
1073
1074#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 if (cmd_cookie == (void *)&cmd_loop_cookie)
1076 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#endif
1080
1081 if (next_cmdline == NULL)
1082 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001083 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#ifdef FEAT_CMDHIST
1085 /*
1086 * If the command was typed, remember it for the ':' register.
1087 * Do this AFTER executing the command to make :@: work.
1088 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001089 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 && new_last_cmdline != NULL)
1091 {
1092 vim_free(last_cmdline);
1093 last_cmdline = new_last_cmdline;
1094 new_last_cmdline = NULL;
1095 }
1096#endif
1097 }
1098 else
1099 {
1100 /* need to copy the command after the '|' to cmdline_copy, for the
1101 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001102 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 next_cmdline = cmdline_copy;
1104 }
1105
1106
1107#ifdef FEAT_EVAL
1108 /* reset did_emsg for a function that is not aborted by an error */
1109 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001110 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 && !func_has_abort(real_cookie))
1112 did_emsg = FALSE;
1113
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116 ++current_line;
1117
1118 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 * An ":endwhile", ":endfor" and ":continue" is handled here.
1120 * If we were executing commands, jump back to the ":while" or
1121 * ":for".
1122 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001126 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 /* Jump back to the matching ":while" or ":for". Be careful
1129 * not to use a cs_line[] from an entry that isn't a ":while"
1130 * or ":for": It would make "current_line" invalid and can
1131 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (!did_emsg && !got_int && !did_throw
1133 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 && (cstack.cs_flags[cstack.cs_idx]
1135 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 && cstack.cs_line[cstack.cs_idx] >= 0
1137 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1138 {
1139 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 /* remember we jumped there */
1141 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 line_breakcheck(); /* check if CTRL-C typed */
1143
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001144 /* Check for the next breakpoint at or after the ":while"
1145 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (breakpoint != NULL)
1147 {
1148 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001149 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 fname,
1151 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1152 *dbg_tick = debug_tick;
1153 }
1154 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001159 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1160 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162 }
1163
1164 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001169 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1171 }
1172 }
1173
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001174 /* Check for the next breakpoint after a watchexpression */
1175 if (breakpoint != NULL && has_watchexpr())
1176 {
1177 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1178 *dbg_tick = debug_tick;
1179 }
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 /*
1182 * When not inside any ":while" loop, clear remembered lines.
1183 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
1186 if (lines_ga.ga_len > 0)
1187 {
1188 sourcing_lnum =
1189 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1190 free_cmdlines(&lines_ga);
1191 }
1192 current_line = 0;
1193 }
1194
1195 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1197 * being restored at the ":endtry". Reset them here and set the
1198 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1199 * This includes the case where a missing ":endif", ":endwhile" or
1200 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 cstack.cs_lflags &= ~CSL_HAD_FINA;
1205 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1206 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 did_throw ? (void *)current_exception : NULL);
1208 did_emsg = got_int = did_throw = FALSE;
1209 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1210 }
1211
1212 /* Update global "trylevel" for recursive calls to do_cmdline() from
1213 * within this loop. */
1214 trylevel = initial_trylevel + cstack.cs_trylevel;
1215
1216 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001217 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 * files) is aborted because of an error, an interrupt, or an uncaught
1219 * exception, cancel everything. If it is left normally, reset
1220 * force_abort to get the non-EH compatible abortion behavior for
1221 * the rest of the script.
1222 */
1223 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1224 force_abort = FALSE;
1225
1226 /* Convert an interrupt to an exception if appropriate. */
1227 (void)do_intthrow(&cstack);
1228#endif /* FEAT_EVAL */
1229
1230 }
1231 /*
1232 * Continue executing command lines when:
1233 * - no CTRL-C typed, no aborting error, no exception thrown or try
1234 * conditionals need to be checked for executing finally clauses or
1235 * catching an interrupt exception
1236 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001237 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 * looping for ":source" command or function call.
1239 */
1240 while (!((got_int
1241#ifdef FEAT_EVAL
1242 || (did_emsg && force_abort) || did_throw
1243#endif
1244 )
1245#ifdef FEAT_EVAL
1246 && cstack.cs_trylevel == 0
1247#endif
1248 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001249 && !(did_emsg
1250#ifdef FEAT_EVAL
1251 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001252 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001253 * the :endtry to be missed. */
1254 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1255#endif
1256 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001257 && (getline_equal(fgetline, cookie, getexmodeline)
1258 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 && (next_cmdline != NULL
1260#ifdef FEAT_EVAL
1261 || cstack.cs_idx >= 0
1262#endif
1263 || (flags & DOCMD_REPEAT)));
1264
1265 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001266 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267#ifdef FEAT_EVAL
1268 free_cmdlines(&lines_ga);
1269 ga_clear(&lines_ga);
1270
1271 if (cstack.cs_idx >= 0)
1272 {
1273 /*
1274 * If a sourced file or executed function ran to its end, report the
1275 * unclosed conditional.
1276 */
1277 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001278 && ((getline_equal(fgetline, cookie, getsourceline)
1279 && !source_finished(fgetline, cookie))
1280 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 && !func_has_ended(real_cookie))))
1282 {
1283 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1284 EMSG(_(e_endtry));
1285 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1286 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001287 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1288 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 else
1290 EMSG(_(e_endif));
1291 }
1292
1293 /*
1294 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1295 * ":endtry" in a sourced file or executed function. If the try
1296 * conditional is in its finally clause, ignore anything pending.
1297 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001298 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 */
1300 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001301 {
1302 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1303
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001304 if (idx >= 0)
1305 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001306 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1307 &cstack.cs_looplevel);
1308 }
1309 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 trylevel = initial_trylevel;
1311 }
1312
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001313 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1314 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 ? (char_u *)"endfunction" : (char_u *)NULL);
1318
1319 if (trylevel == 0)
1320 {
1321 /*
1322 * When an exception is being thrown out of the outermost try
1323 * conditional, discard the uncaught exception, disable the conversion
1324 * of interrupts or errors to exceptions, and ensure that no more
1325 * commands are executed.
1326 */
1327 if (did_throw)
1328 {
1329 void *p = NULL;
1330 char_u *saved_sourcing_name;
1331 int saved_sourcing_lnum;
1332 struct msglist *messages = NULL, *next;
1333
1334 /*
1335 * If the uncaught exception is a user exception, report it as an
1336 * error. If it is an error exception, display the saved error
1337 * message now. For an interrupt exception, do nothing; the
1338 * interrupt message is given elsewhere.
1339 */
1340 switch (current_exception->type)
1341 {
1342 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001343 vim_snprintf((char *)IObuff, IOSIZE,
1344 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 current_exception->value);
1346 p = vim_strsave(IObuff);
1347 break;
1348 case ET_ERROR:
1349 messages = current_exception->messages;
1350 current_exception->messages = NULL;
1351 break;
1352 case ET_INTERRUPT:
1353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355
1356 saved_sourcing_name = sourcing_name;
1357 saved_sourcing_lnum = sourcing_lnum;
1358 sourcing_name = current_exception->throw_name;
1359 sourcing_lnum = current_exception->throw_lnum;
1360 current_exception->throw_name = NULL;
1361
1362 discard_current_exception(); /* uses IObuff if 'verbose' */
1363 suppress_errthrow = TRUE;
1364 force_abort = TRUE;
1365
1366 if (messages != NULL)
1367 {
1368 do
1369 {
1370 next = messages->next;
1371 emsg(messages->msg);
1372 vim_free(messages->msg);
1373 vim_free(messages);
1374 messages = next;
1375 }
1376 while (messages != NULL);
1377 }
1378 else if (p != NULL)
1379 {
1380 emsg(p);
1381 vim_free(p);
1382 }
1383 vim_free(sourcing_name);
1384 sourcing_name = saved_sourcing_name;
1385 sourcing_lnum = saved_sourcing_lnum;
1386 }
1387
1388 /*
1389 * On an interrupt or an aborting error not converted to an exception,
1390 * disable the conversion of errors to exceptions. (Interrupts are not
1391 * converted any more, here.) This enables also the interrupt message
1392 * when force_abort is set and did_emsg unset in case of an interrupt
1393 * from a finally clause after an error.
1394 */
1395 else if (got_int || (did_emsg && force_abort))
1396 suppress_errthrow = TRUE;
1397 }
1398
1399 /*
1400 * The current cstack will be freed when do_cmdline() returns. An uncaught
1401 * exception will have to be rethrown in the previous cstack. If a function
1402 * has just returned or a script file was just finished and the previous
1403 * cstack belongs to the same function or, respectively, script file, it
1404 * will have to be checked for finally clauses to be executed due to the
1405 * ":return" or ":finish". This is done in do_one_cmd().
1406 */
1407 if (did_throw)
1408 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001409 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001411 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 && ex_nesting_level > func_level(real_cookie) + 1))
1413 {
1414 if (!did_throw)
1415 check_cstack = TRUE;
1416 }
1417 else
1418 {
1419 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001420 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 --ex_nesting_level;
1422 /*
1423 * Go to debug mode when returning from a function in which we are
1424 * single-stepping.
1425 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001426 if ((getline_equal(fgetline, cookie, getsourceline)
1427 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001429 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 ? (char_u *)_("End of sourced file")
1431 : (char_u *)_("End of function"));
1432 }
1433
1434 /*
1435 * Restore the exception environment (done after returning from the
1436 * debugger).
1437 */
1438 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001439 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441 msg_list = saved_msg_list;
1442#endif /* FEAT_EVAL */
1443
1444 /*
1445 * If there was too much output to fit on the command line, ask the user to
1446 * hit return before redrawing the screen. With the ":global" command we do
1447 * this only once after the command is finished.
1448 */
1449 if (did_inc)
1450 {
1451 --RedrawingDisabled;
1452 --no_wait_return;
1453 msg_scroll = FALSE;
1454
1455 /*
1456 * When just finished an ":if"-":else" which was typed, no need to
1457 * wait for hit-return. Also for an error situation.
1458 */
1459 if (retval == FAIL
1460#ifdef FEAT_EVAL
1461 || (did_endif && KeyTyped && !did_emsg)
1462#endif
1463 )
1464 {
1465 need_wait_return = FALSE;
1466 msg_didany = FALSE; /* don't wait when restarting edit */
1467 }
1468 else if (need_wait_return)
1469 {
1470 /*
1471 * The msg_start() above clears msg_didout. The wait_return we do
1472 * here should not overwrite the command that may be shown before
1473 * doing that.
1474 */
1475 msg_didout |= msg_didout_before_start;
1476 wait_return(FALSE);
1477 }
1478 }
1479
Bram Moolenaar2a942252012-11-28 23:03:07 +01001480#ifdef FEAT_EVAL
1481 did_endif = FALSE; /* in case do_cmdline used recursively */
1482#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 /*
1484 * Reset if_level, in case a sourced script file contains more ":if" than
1485 * ":endif" (could be ":if x | foo | endif").
1486 */
1487 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001488#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 --call_depth;
1491 return retval;
1492}
1493
1494#ifdef FEAT_EVAL
1495/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001496 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 */
1498 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001499get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 wcmd_T *wp;
1503 char_u *line;
1504
1505 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1506 {
1507 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001508 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (cp->getline == NULL)
1512 line = getcmdline(c, 0L, indent);
1513 else
1514 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001515 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 ++cp->current_line;
1517
1518 return line;
1519 }
1520
1521 KeyTyped = FALSE;
1522 ++cp->current_line;
1523 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1524 sourcing_lnum = wp->lnum;
1525 return vim_strsave(wp->line);
1526}
1527
1528/*
1529 * Store a line in "gap" so that a ":while" loop can execute it again.
1530 */
1531 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001532store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
1534 if (ga_grow(gap, 1) == FAIL)
1535 return FAIL;
1536 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1537 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1538 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 return OK;
1540}
1541
1542/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001543 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 */
1545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001546free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 while (gap->ga_len > 0)
1549 {
1550 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1551 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
1553}
1554#endif
1555
1556/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001557 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1558 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001561getline_equal(
1562 char_u *(*fgetline)(int, void *, int),
1563 void *cookie UNUSED, /* argument for fgetline() */
1564 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001567 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001568 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar89d40322006-08-29 15:30:07 +00001570 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001571 * function that's originally used to obtain the lines. This may be
1572 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001573 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001574 cp = (struct loop_cookie *)cookie;
1575 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
1577 gp = cp->getline;
1578 cp = cp->cookie;
1579 }
1580 return gp == func;
1581#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001582 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583#endif
1584}
1585
1586#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1587/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001588 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 * getline function. Otherwise return "cookie".
1590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001592getline_cookie(
1593 char_u *(*fgetline)(int, void *, int) UNUSED,
1594 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001597 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001598 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
Bram Moolenaar89d40322006-08-29 15:30:07 +00001600 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001601 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001603 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001604 cp = (struct loop_cookie *)cookie;
1605 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607 gp = cp->getline;
1608 cp = cp->cookie;
1609 }
1610 return cp;
1611# else
1612 return cookie;
1613# endif
1614}
1615#endif
1616
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617
1618/*
1619 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1620 * ":bwipeout", etc.
1621 * Returns the buffer number.
1622 */
1623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001624compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625{
1626 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001627 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 int count = offset;
1629
1630 buf = firstbuf;
1631 while (buf->b_next != NULL && buf->b_fnum < lnum)
1632 buf = buf->b_next;
1633 while (count != 0)
1634 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001635 count += (offset < 0) ? 1 : -1;
1636 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1637 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001638 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001639 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001640 if (addr_type == ADDR_LOADED_BUFFERS)
1641 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001642 while (buf->b_ml.ml_mfp == NULL)
1643 {
1644 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1645 if (nextbuf == NULL)
1646 break;
1647 buf = nextbuf;
1648 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001650 /* we might have gone too far, last buffer is not loadedd */
1651 if (addr_type == ADDR_LOADED_BUFFERS)
1652 while (buf->b_ml.ml_mfp == NULL)
1653 {
1654 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1655 if (nextbuf == NULL)
1656 break;
1657 buf = nextbuf;
1658 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001659 return buf->b_fnum;
1660}
1661
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001662static int current_win_nr(win_T *win);
1663static int current_tab_nr(tabpage_T *tab);
Bram Moolenaarf240e182014-11-27 18:33:02 +01001664
1665 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001666current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001667{
1668 win_T *wp;
1669 int nr = 0;
1670
Bram Moolenaar29323592016-07-24 22:04:11 +02001671 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001672 {
1673 ++nr;
1674 if (wp == win)
1675 break;
1676 }
1677 return nr;
1678}
1679
1680 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001681current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001682{
1683 tabpage_T *tp;
1684 int nr = 0;
1685
Bram Moolenaar29323592016-07-24 22:04:11 +02001686 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001687 {
1688 ++nr;
1689 if (tp == tab)
1690 break;
1691 }
1692 return nr;
1693}
1694
1695# define CURRENT_WIN_NR current_win_nr(curwin)
1696# define LAST_WIN_NR current_win_nr(NULL)
1697# define CURRENT_TAB_NR current_tab_nr(curtab)
1698# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * Execute one Ex command.
1702 *
1703 * If 'sourcing' is TRUE, the command will be included in the error message.
1704 *
1705 * 1. skip comment lines and leading space
1706 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001707 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001709 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 6. parse arguments
1711 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001713 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 *
1715 * This function may be called recursively!
1716 */
1717#if (_MSC_VER == 1200)
1718/*
Bram Moolenaared203462004-06-16 11:19:22 +00001719 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001721 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#endif
1723 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001724do_one_cmd(
1725 char_u **cmdlinep,
1726 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001728 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001730 char_u *(*fgetline)(int, void *, int),
1731 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 char_u *p;
1734 linenr_T lnum;
1735 long n;
1736 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001737 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 exarg_T ea; /* Ex command arguments */
1739 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001740 int save_msg_scroll = msg_scroll;
1741 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001743#ifdef HAVE_SANDBOX
1744 int did_sandbox = FALSE;
1745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 cmdmod_T save_cmdmod;
1747 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001748 char_u *cmd;
Bram Moolenaarded27822017-01-02 14:27:34 +01001749 int address_count = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751 vim_memset(&ea, 0, sizeof(ea));
1752 ea.line1 = 1;
1753 ea.line2 = 1;
1754#ifdef FEAT_EVAL
1755 ++ex_nesting_level;
1756#endif
1757
Bram Moolenaar21691f82012-12-05 19:13:18 +01001758 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if (quitmore
1760#ifdef FEAT_EVAL
1761 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001762 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001763#endif
1764#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001765 /* avoid that an autocommand, e.g. QuitPre, does this */
1766 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768 )
1769 --quitmore;
1770
1771 /*
1772 * Reset browse, confirm, etc.. They are restored when returning, for
1773 * recursive calls.
1774 */
1775 save_cmdmod = cmdmod;
1776 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1777
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001778 /* "#!anything" is handled like a comment. */
1779 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1780 goto doend;
1781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 /*
1783 * Repeat until no more command modifiers are found.
1784 */
1785 ea.cmd = *cmdlinep;
1786 for (;;)
1787 {
1788/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001789 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 */
1791 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1792 ++ea.cmd;
1793
1794 /* in ex mode, an empty line works like :+ */
1795 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001796 && (getline_equal(fgetline, cookie, getexmodeline)
1797 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001798 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
1800 ea.cmd = (char_u *)"+";
1801 ex_pressedreturn = TRUE;
1802 }
1803
1804 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001805 if (*ea.cmd == '"')
1806 goto doend;
1807 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001808 {
1809 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001814 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 */
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001816 p = skip_range(ea.cmd, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 switch (*p)
1818 {
1819 /* When adding an entry, also modify cmd_exists(). */
1820 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 continue;
1824
1825 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 continue;
1829 }
1830 if (checkforcmd(&ea.cmd, "browse", 3))
1831 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001832#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 cmdmod.browse = TRUE;
1834#endif
1835 continue;
1836 }
1837 if (!checkforcmd(&ea.cmd, "botright", 2))
1838 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 cmdmod.split |= WSP_BOT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 continue;
1841
1842 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1843 break;
1844#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1845 cmdmod.confirm = TRUE;
1846#endif
1847 continue;
1848
1849 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1850 {
1851 cmdmod.keepmarks = TRUE;
1852 continue;
1853 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001854 if (checkforcmd(&ea.cmd, "keepalt", 5))
1855 {
1856 cmdmod.keepalt = TRUE;
1857 continue;
1858 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001859 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1860 {
1861 cmdmod.keeppatterns = TRUE;
1862 continue;
1863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1865 break;
1866 cmdmod.keepjumps = TRUE;
1867 continue;
1868
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001869 case 'f': /* only accept ":filter {pat} cmd" */
1870 {
1871 char_u *reg_pat;
1872
1873 if (!checkforcmd(&p, "filter", 4)
1874 || *p == NUL || ends_excmd(*p))
1875 break;
Bram Moolenaard29459b2016-08-26 22:29:11 +02001876 if (*p == '!')
1877 {
1878 cmdmod.filter_force = TRUE;
1879 p = skipwhite(p + 1);
1880 if (*p == NUL || ends_excmd(*p))
1881 break;
1882 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001883 p = skip_vimgrep_pat(p, &reg_pat, NULL);
1884 if (p == NULL || *p == NUL)
1885 break;
1886 cmdmod.filter_regmatch.regprog =
1887 vim_regcomp(reg_pat, RE_MAGIC);
1888 if (cmdmod.filter_regmatch.regprog == NULL)
1889 break;
1890 ea.cmd = p;
1891 continue;
1892 }
1893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 /* ":hide" and ":hide | cmd" are not modifiers */
1895 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1896 || *p == NUL || ends_excmd(*p))
1897 break;
1898 ea.cmd = p;
1899 cmdmod.hide = TRUE;
1900 continue;
1901
1902 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1903 {
1904 cmdmod.lockmarks = TRUE;
1905 continue;
1906 }
1907
1908 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1909 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 cmdmod.split |= WSP_ABOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 continue;
1912
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001913 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001914 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001915#ifdef FEAT_AUTOCMD
1916 if (cmdmod.save_ei == NULL)
1917 {
1918 /* Set 'eventignore' to "all". Restore the
1919 * existing option value later. */
1920 cmdmod.save_ei = vim_strsave(p_ei);
1921 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001922 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001923 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001924#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001925 continue;
1926 }
Bram Moolenaar3bcfca32016-07-30 19:39:29 +02001927 if (!checkforcmd(&ea.cmd, "noswapfile", 3))
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001928 break;
1929 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001930 continue;
1931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 cmdmod.split |= WSP_BELOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 continue;
1936
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001937 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1938 {
1939#ifdef HAVE_SANDBOX
1940 if (!did_sandbox)
1941 ++sandbox;
1942 did_sandbox = TRUE;
1943#endif
1944 continue;
1945 }
1946 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001948 if (save_msg_silent == -1)
1949 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 ++msg_silent;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001951 if (*ea.cmd == '!' && !VIM_ISWHITE(ea.cmd[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 /* ":silent!", but not "silent !cmd" */
1954 ea.cmd = skipwhite(ea.cmd + 1);
1955 ++emsg_silent;
1956 ++did_esilent;
1957 }
1958 continue;
1959
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001960 case 't': if (checkforcmd(&p, "tab", 3))
1961 {
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001962 long tabnr = get_address(&ea, &ea.cmd, ADDR_TABS,
Bram Moolenaarded27822017-01-02 14:27:34 +01001963 ea.skip, FALSE, 1);
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001964 if (tabnr == MAXLNUM)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001965 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +02001966 else
1967 {
1968 if (tabnr < 0 || tabnr > LAST_TAB_NR)
1969 {
1970 errormsg = (char_u *)_(e_invrange);
1971 goto doend;
1972 }
1973 cmdmod.tab = tabnr + 1;
1974 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001975 ea.cmd = p;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001976 continue;
1977 }
1978 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 cmdmod.split |= WSP_TOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 continue;
1982
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001983 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1984 break;
1985 if (save_msg_silent == -1)
1986 save_msg_silent = msg_silent;
1987 msg_silent = 0;
1988 continue;
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 cmdmod.split |= WSP_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 continue;
1994 }
1995 if (!checkforcmd(&p, "verbose", 4))
1996 break;
1997 if (verbose_save < 0)
1998 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001999 if (vim_isdigit(*ea.cmd))
2000 p_verbose = atoi((char *)ea.cmd);
2001 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 p_verbose = 1;
2003 ea.cmd = p;
2004 continue;
2005 }
2006 break;
2007 }
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002008 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010#ifdef FEAT_EVAL
2011 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2012 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2013#else
2014 ea.skip = (if_level > 0);
2015#endif
2016
2017#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002018# ifdef FEAT_PROFILE
2019 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002020 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002021 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002022 if (getline_equal(fgetline, cookie, get_func_line))
2023 func_line_exec(getline_cookie(fgetline, cookie));
2024 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002025 script_line_exec();
2026 }
2027#endif
2028
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 /* May go to debug mode. If this happens and the ">quit" debug command is
2030 * used, throw an interrupt exception and skip the next command. */
2031 dbg_check_breakpoint(&ea);
2032 if (!ea.skip && got_int)
2033 {
2034 ea.skip = TRUE;
2035 (void)do_intthrow(cstack);
2036 }
2037#endif
2038
2039/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002040 * 3. Skip over the range to find the command. Let "p" point to after it.
2041 *
2042 * We need the command to know what kind of range it uses.
2043 */
2044 cmd = ea.cmd;
2045 ea.cmd = skip_range(ea.cmd, NULL);
2046 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2047 ea.cmd = skipwhite(ea.cmd + 1);
2048 p = find_command(&ea, NULL);
2049
2050/*
2051 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 *
2053 * where 'addr' is:
2054 *
2055 * % (entire file)
2056 * $ [+-NUM]
2057 * 'x [+-NUM] (where x denotes a currently defined mark)
2058 * . [+-NUM]
2059 * [+-NUM]..
2060 * NUM
2061 *
2062 * The ea.cmd pointer is updated to point to the first character following the
2063 * range spec. If an initial address is found, but no second, the upper bound
2064 * is equal to the lower.
2065 */
2066
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002067 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002068 if (!IS_USER_CMDIDX(ea.cmdidx))
2069 {
2070 if (ea.cmdidx != CMD_SIZE)
2071 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2072 else
2073 ea.addr_type = ADDR_LINES;
2074
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002075 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01002076 if (ea.cmdidx == CMD_wincmd && p != NULL)
2077 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002078 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 /* repeat for all ',' or ';' separated addresses */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002081 ea.cmd = cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 for (;;)
2083 {
2084 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002085 switch (ea.addr_type)
2086 {
2087 case ADDR_LINES:
2088 /* default is current line number */
2089 ea.line2 = curwin->w_cursor.lnum;
2090 break;
2091 case ADDR_WINDOWS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002092 ea.line2 = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002093 break;
2094 case ADDR_ARGUMENTS:
2095 ea.line2 = curwin->w_arg_idx + 1;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01002096 if (ea.line2 > ARGCOUNT)
2097 ea.line2 = ARGCOUNT;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002098 break;
2099 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002100 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002101 ea.line2 = curbuf->b_fnum;
2102 break;
2103 case ADDR_TABS:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002104 ea.line2 = CURRENT_TAB_NR;
2105 break;
2106 case ADDR_TABS_RELATIVE:
2107 ea.line2 = 1;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002108 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002109#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002110 case ADDR_QUICKFIX:
2111 ea.line2 = qf_get_cur_valid_idx(&ea);
2112 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002113#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002116 lnum = get_address(&ea, &ea.cmd, ea.addr_type, ea.skip,
Bram Moolenaarded27822017-01-02 14:27:34 +01002117 ea.addr_count == 0, address_count++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (ea.cmd == NULL) /* error detected */
2119 goto doend;
2120 if (lnum == MAXLNUM)
2121 {
2122 if (*ea.cmd == '%') /* '%' - all lines */
2123 {
2124 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002125 switch (ea.addr_type)
2126 {
2127 case ADDR_LINES:
2128 ea.line1 = 1;
2129 ea.line2 = curbuf->b_ml.ml_line_count;
2130 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002131 case ADDR_LOADED_BUFFERS:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01002132 {
2133 buf_T *buf = firstbuf;
2134
2135 while (buf->b_next != NULL
2136 && buf->b_ml.ml_mfp == NULL)
2137 buf = buf->b_next;
2138 ea.line1 = buf->b_fnum;
2139 buf = lastbuf;
2140 while (buf->b_prev != NULL
2141 && buf->b_ml.ml_mfp == NULL)
2142 buf = buf->b_prev;
2143 ea.line2 = buf->b_fnum;
2144 break;
2145 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002146 case ADDR_BUFFERS:
Bram Moolenaar4d84d932014-11-30 14:50:16 +01002147 ea.line1 = firstbuf->b_fnum;
2148 ea.line2 = lastbuf->b_fnum;
2149 break;
2150 case ADDR_WINDOWS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002151 case ADDR_TABS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002152 if (IS_USER_CMDIDX(ea.cmdidx))
2153 {
2154 ea.line1 = 1;
2155 ea.line2 = ea.addr_type == ADDR_WINDOWS
2156 ? LAST_WIN_NR : LAST_TAB_NR;
2157 }
2158 else
2159 {
2160 /* there is no Vim command which uses '%' and
2161 * ADDR_WINDOWS or ADDR_TABS */
2162 errormsg = (char_u *)_(e_invrange);
2163 goto doend;
2164 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002165 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002166 case ADDR_TABS_RELATIVE:
2167 errormsg = (char_u *)_(e_invrange);
2168 goto doend;
2169 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002170 case ADDR_ARGUMENTS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002171 if (ARGCOUNT == 0)
2172 ea.line1 = ea.line2 = 0;
2173 else
2174 {
2175 ea.line1 = 1;
2176 ea.line2 = ARGCOUNT;
2177 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002178 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002179#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002180 case ADDR_QUICKFIX:
2181 ea.line1 = 1;
2182 ea.line2 = qf_get_size(&ea);
2183 if (ea.line2 == 0)
2184 ea.line2 = 1;
2185 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002186#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 ++ea.addr_count;
2189 }
2190 /* '*' - visual area */
2191 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2192 {
2193 pos_T *fp;
2194
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002195 if (ea.addr_type != ADDR_LINES)
2196 {
2197 errormsg = (char_u *)_(e_invrange);
2198 goto doend;
2199 }
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 ++ea.cmd;
2202 if (!ea.skip)
2203 {
2204 fp = getmark('<', FALSE);
2205 if (check_mark(fp) == FAIL)
2206 goto doend;
2207 ea.line1 = fp->lnum;
2208 fp = getmark('>', FALSE);
2209 if (check_mark(fp) == FAIL)
2210 goto doend;
2211 ea.line2 = fp->lnum;
2212 ++ea.addr_count;
2213 }
2214 }
2215 }
2216 else
2217 ea.line2 = lnum;
2218 ea.addr_count++;
2219
2220 if (*ea.cmd == ';')
2221 {
2222 if (!ea.skip)
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 curwin->w_cursor.lnum = ea.line2;
Bram Moolenaarf1f6f3f2017-02-09 22:28:20 +01002225 /* don't leave the cursor on an illegal line or column */
2226 check_cursor();
Bram Moolenaarfe38b492016-12-11 21:34:23 +01002227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229 else if (*ea.cmd != ',')
2230 break;
2231 ++ea.cmd;
2232 }
2233
2234 /* One address given: set start and end lines */
2235 if (ea.addr_count == 1)
2236 {
2237 ea.line1 = ea.line2;
2238 /* ... but only implicit: really no address given */
2239 if (lnum == MAXLNUM)
2240 ea.addr_count = 0;
2241 }
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002244 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 */
2246
2247 /*
2248 * Skip ':' and any white space
2249 */
2250 ea.cmd = skipwhite(ea.cmd);
2251 while (*ea.cmd == ':')
2252 ea.cmd = skipwhite(ea.cmd + 1);
2253
2254 /*
2255 * If we got a line, but no command, then go to the line.
2256 * If we find a '|' or '\n' we set ea.nextcmd.
2257 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002258 if (*ea.cmd == NUL || *ea.cmd == '"'
2259 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 /*
2262 * strange vi behaviour:
2263 * ":3" jumps to line 3
2264 * ":3|..." prints line 3
2265 * ":|" prints current line
2266 */
2267 if (ea.skip) /* skip this if inside :if */
2268 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002269 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
2271 ea.cmdidx = CMD_print;
2272 ea.argt = RANGE+COUNT+TRLBAR;
2273 if ((errormsg = invalid_range(&ea)) == NULL)
2274 {
2275 correct_range(&ea);
2276 ex_print(&ea);
2277 }
2278 }
2279 else if (ea.addr_count != 0)
2280 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002281 if (ea.line2 > curbuf->b_ml.ml_line_count)
2282 {
2283 /* With '-' in 'cpoptions' a line number past the file is an
2284 * error, otherwise put it at the end of the file. */
2285 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2286 ea.line2 = -1;
2287 else
2288 ea.line2 = curbuf->b_ml.ml_line_count;
2289 }
2290
2291 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002292 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 else
2294 {
2295 if (ea.line2 == 0)
2296 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 else
2298 curwin->w_cursor.lnum = ea.line2;
2299 beginline(BL_SOL | BL_FIX);
2300 }
2301 }
2302 goto doend;
2303 }
2304
Bram Moolenaard5005162014-08-22 23:05:54 +02002305#ifdef FEAT_AUTOCMD
2306 /* If this looks like an undefined user command and there are CmdUndefined
2307 * autocommands defined, trigger the matching autocommands. */
2308 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2309 && ASCII_ISUPPER(*ea.cmd)
2310 && has_cmdundefined())
2311 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002312 int ret;
2313
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002314 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002315 while (ASCII_ISALNUM(*p))
2316 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002317 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002318 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2319 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02002320 /* If the autocommands did something and didn't cause an error, try
2321 * finding the command again. */
Bram Moolenaareac784e2016-07-28 22:08:24 +02002322 p = (ret && !aborting()) ? find_command(&ea, NULL) : ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002323 }
2324#endif
2325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#ifdef FEAT_USR_CMDS
2327 if (p == NULL)
2328 {
2329 if (!ea.skip)
2330 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2331 goto doend;
2332 }
2333 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002334 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2335 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 errormsg = uc_fun_cmd();
2338 goto doend;
2339 }
2340#endif
2341 if (ea.cmdidx == CMD_SIZE)
2342 {
2343 if (!ea.skip)
2344 {
2345 STRCPY(IObuff, _("E492: Not an editor command"));
2346 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002347 {
2348 /* If the modifier was parsed OK the error must be in the
2349 * following command */
2350 if (after_modifier != NULL)
2351 append_command(after_modifier);
2352 else
2353 append_command(*cmdlinep);
2354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002356 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358 goto doend;
2359 }
2360
Bram Moolenaar958636c2014-10-21 20:01:58 +02002361 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2362 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002363#ifdef HAVE_EX_SCRIPT_NI
2364 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2365#endif
2366 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368#ifndef FEAT_EVAL
2369 /*
2370 * When the expression evaluation is disabled, recognize the ":if" and
2371 * ":endif" commands and ignore everything in between it.
2372 */
2373 if (ea.cmdidx == CMD_if)
2374 ++if_level;
2375 if (if_level)
2376 {
2377 if (ea.cmdidx == CMD_endif)
2378 --if_level;
2379 goto doend;
2380 }
2381
2382#endif
2383
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002384 /* forced commands */
2385 if (*p == '!' && ea.cmdidx != CMD_substitute
2386 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
2388 ++p;
2389 ea.forceit = TRUE;
2390 }
2391 else
2392 ea.forceit = FALSE;
2393
2394/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002395 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002397 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002398 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400 if (!ea.skip)
2401 {
2402#ifdef HAVE_SANDBOX
2403 if (sandbox != 0 && !(ea.argt & SBOXOK))
2404 {
2405 /* Command not allowed in sandbox. */
2406 errormsg = (char_u *)_(e_sandbox);
2407 goto doend;
2408 }
2409#endif
2410 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2411 {
2412 /* Command not allowed in non-'modifiable' buffer */
2413 errormsg = (char_u *)_(e_modifiable);
2414 goto doend;
2415 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002416
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002417 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002418 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002420 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002421 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 goto doend;
2423 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002424#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002425 /* Disallow editing another buffer when "curbuf_lock" is set.
2426 * Do allow ":edit" (check for argument later).
2427 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002428 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002429 && ea.cmdidx != CMD_edit
2430 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002431 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002432 && curbuf_locked())
2433 goto doend;
2434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2437 {
2438 /* no range allowed */
2439 errormsg = (char_u *)_(e_norange);
2440 goto doend;
2441 }
2442 }
2443
2444 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2445 {
2446 errormsg = (char_u *)_(e_nobang);
2447 goto doend;
2448 }
2449
2450 /*
2451 * Don't complain about the range if it is not used
2452 * (could happen if line_count is accidentally set to 0).
2453 */
2454 if (!ea.skip && !ni)
2455 {
2456 /*
2457 * If the range is backwards, ask for confirmation and, if given, swap
2458 * ea.line1 & ea.line2 so it's forwards again.
2459 * When global command is busy, don't ask, will fail below.
2460 */
2461 if (!global_busy && ea.line1 > ea.line2)
2462 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002463 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002465 if (sourcing || exmode_active)
2466 {
2467 errormsg = (char_u *)_("E493: Backwards range given");
2468 goto doend;
2469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 if (ask_yesno((char_u *)
2471 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002472 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474 lnum = ea.line1;
2475 ea.line1 = ea.line2;
2476 ea.line2 = lnum;
2477 }
2478 if ((errormsg = invalid_range(&ea)) != NULL)
2479 goto doend;
2480 }
2481
2482 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2483 ea.line2 = 1;
2484
2485 correct_range(&ea);
2486
2487#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002488 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2489 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 /* Put the first line at the start of a closed fold, put the last line
2492 * at the end of a closed fold. */
2493 (void)hasFolding(ea.line1, &ea.line1, NULL);
2494 (void)hasFolding(ea.line2, NULL, &ea.line2);
2495 }
2496#endif
2497
2498#ifdef FEAT_QUICKFIX
2499 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002500 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 * option here, so things like % get expanded.
2502 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002503 p = replace_makeprg(&ea, p, cmdlinep);
2504 if (p == NULL)
2505 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506#endif
2507
2508 /*
2509 * Skip to start of argument.
2510 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2511 */
2512 if (ea.cmdidx == CMD_bang)
2513 ea.arg = p;
2514 else
2515 ea.arg = skipwhite(p);
2516
2517 /*
2518 * Check for "++opt=val" argument.
2519 * Must be first, allow ":w ++enc=utf8 !cmd"
2520 */
2521 if (ea.argt & ARGOPT)
2522 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2523 if (getargopt(&ea) == FAIL && !ni)
2524 {
2525 errormsg = (char_u *)_(e_invarg);
2526 goto doend;
2527 }
2528
2529 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2530 {
2531 if (*ea.arg == '>') /* append */
2532 {
2533 if (*++ea.arg != '>') /* typed wrong */
2534 {
2535 errormsg = (char_u *)_("E494: Use w or w>>");
2536 goto doend;
2537 }
2538 ea.arg = skipwhite(ea.arg + 1);
2539 ea.append = TRUE;
2540 }
2541 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2542 {
2543 ++ea.arg;
2544 ea.usefilter = TRUE;
2545 }
2546 }
2547
2548 if (ea.cmdidx == CMD_read)
2549 {
2550 if (ea.forceit)
2551 {
2552 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2553 ea.forceit = FALSE;
2554 }
2555 else if (*ea.arg == '!') /* :r !filter */
2556 {
2557 ++ea.arg;
2558 ea.usefilter = TRUE;
2559 }
2560 }
2561
2562 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2563 {
2564 ea.amount = 1;
2565 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2566 {
2567 ++ea.arg;
2568 ++ea.amount;
2569 }
2570 ea.arg = skipwhite(ea.arg);
2571 }
2572
2573 /*
2574 * Check for "+command" argument, before checking for next command.
2575 * Don't do this for ":read !cmd" and ":write !cmd".
2576 */
2577 if ((ea.argt & EDITCMD) && !ea.usefilter)
2578 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2579
2580 /*
2581 * Check for '|' to separate commands and '"' to start comments.
2582 * Don't do this for ":read !cmd" and ":write !cmd".
2583 */
2584 if ((ea.argt & TRLBAR) && !ea.usefilter)
2585 separate_nextcmd(&ea);
2586
2587 /*
2588 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002589 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2590 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002592 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002593 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002594 || ea.cmdidx == CMD_global
2595 || ea.cmdidx == CMD_vglobal
2596 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
2598 for (p = ea.arg; *p; ++p)
2599 {
2600 /* Remove one backslash before a newline, so that it's possible to
2601 * pass a newline to the shell and also a newline that is preceded
2602 * with a backslash. This makes it impossible to end a shell
2603 * command in a backslash, but that doesn't appear useful.
2604 * Halving the number of backslashes is incompatible with previous
2605 * versions. */
2606 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002607 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 else if (*p == '\n')
2609 {
2610 ea.nextcmd = p + 1;
2611 *p = NUL;
2612 break;
2613 }
2614 }
2615 }
2616
2617 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2618 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002619 buf_T *buf;
2620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002622 switch (ea.addr_type)
2623 {
2624 case ADDR_LINES:
2625 ea.line2 = curbuf->b_ml.ml_line_count;
2626 break;
2627 case ADDR_LOADED_BUFFERS:
2628 buf = firstbuf;
2629 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2630 buf = buf->b_next;
2631 ea.line1 = buf->b_fnum;
2632 buf = lastbuf;
2633 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2634 buf = buf->b_prev;
2635 ea.line2 = buf->b_fnum;
2636 break;
2637 case ADDR_BUFFERS:
2638 ea.line1 = firstbuf->b_fnum;
2639 ea.line2 = lastbuf->b_fnum;
2640 break;
2641 case ADDR_WINDOWS:
2642 ea.line2 = LAST_WIN_NR;
2643 break;
2644 case ADDR_TABS:
2645 ea.line2 = LAST_TAB_NR;
2646 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002647 case ADDR_TABS_RELATIVE:
2648 ea.line2 = 1;
2649 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002650 case ADDR_ARGUMENTS:
2651 if (ARGCOUNT == 0)
2652 ea.line1 = ea.line2 = 0;
2653 else
2654 ea.line2 = ARGCOUNT;
2655 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002656#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002657 case ADDR_QUICKFIX:
2658 ea.line2 = qf_get_size(&ea);
2659 if (ea.line2 == 0)
2660 ea.line2 = 1;
2661 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002662#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665
2666 /* accept numbered register only when no count allowed (:put) */
2667 if ( (ea.argt & REGSTR)
2668 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002669 /* Do not allow register = for user commands */
2670 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2672 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002673#ifndef FEAT_CLIPBOARD
2674 /* check these explicitly for a more specific error message */
2675 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002677 errormsg = (char_u *)_(e_invalidreg);
2678 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
2680#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002681 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2682 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002683 {
2684 ea.regname = *ea.arg++;
2685#ifdef FEAT_EVAL
2686 /* for '=' register: accept the rest of the line as an expression */
2687 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2688 {
2689 set_expr_line(vim_strsave(ea.arg));
2690 ea.arg += STRLEN(ea.arg);
2691 }
2692#endif
2693 ea.arg = skipwhite(ea.arg);
2694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696
2697 /*
2698 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2699 * count, it's a buffer name.
2700 */
2701 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2702 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002703 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
2705 n = getdigits(&ea.arg);
2706 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002707 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
2709 errormsg = (char_u *)_(e_zerocount);
2710 goto doend;
2711 }
2712 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2713 {
2714 ea.line2 = n;
2715 if (ea.addr_count == 0)
2716 ea.addr_count = 1;
2717 }
2718 else
2719 {
2720 ea.line1 = ea.line2;
2721 ea.line2 += n - 1;
2722 ++ea.addr_count;
2723 /*
2724 * Be vi compatible: no error message for out of range.
2725 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002726 if (ea.addr_type == ADDR_LINES
2727 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 ea.line2 = curbuf->b_ml.ml_line_count;
2729 }
2730 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002731
2732 /*
2733 * Check for flags: 'l', 'p' and '#'.
2734 */
2735 if (ea.argt & EXFLAGS)
2736 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002738 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002739 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
2741 errormsg = (char_u *)_(e_trailing);
2742 goto doend;
2743 }
2744
2745 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2746 {
2747 errormsg = (char_u *)_(e_argreq);
2748 goto doend;
2749 }
2750
2751#ifdef FEAT_EVAL
2752 /*
2753 * Skip the command when it's not going to be executed.
2754 * The commands like :if, :endif, etc. always need to be executed.
2755 * Also make an exception for commands that handle a trailing command
2756 * themselves.
2757 */
2758 if (ea.skip)
2759 {
2760 switch (ea.cmdidx)
2761 {
2762 /* commands that need evaluation */
2763 case CMD_while:
2764 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002765 case CMD_for:
2766 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 case CMD_if:
2768 case CMD_elseif:
2769 case CMD_else:
2770 case CMD_endif:
2771 case CMD_try:
2772 case CMD_catch:
2773 case CMD_finally:
2774 case CMD_endtry:
2775 case CMD_function:
2776 break;
2777
2778 /* Commands that handle '|' themselves. Check: A command should
2779 * either have the TRLBAR flag, appear in this list or appear in
2780 * the list at ":help :bar". */
2781 case CMD_aboveleft:
2782 case CMD_and:
2783 case CMD_belowright:
2784 case CMD_botright:
2785 case CMD_browse:
2786 case CMD_call:
2787 case CMD_confirm:
2788 case CMD_delfunction:
2789 case CMD_djump:
2790 case CMD_dlist:
2791 case CMD_dsearch:
2792 case CMD_dsplit:
2793 case CMD_echo:
2794 case CMD_echoerr:
2795 case CMD_echomsg:
2796 case CMD_echon:
2797 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002798 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 case CMD_help:
2800 case CMD_hide:
2801 case CMD_ijump:
2802 case CMD_ilist:
2803 case CMD_isearch:
2804 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002805 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 case CMD_keepjumps:
2807 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002808 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 case CMD_leftabove:
2810 case CMD_let:
2811 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002812 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002814 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002815 case CMD_noautocmd:
2816 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 case CMD_perl:
2818 case CMD_psearch:
2819 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002820 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002821 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 case CMD_return:
2823 case CMD_rightbelow:
2824 case CMD_ruby:
2825 case CMD_silent:
2826 case CMD_smagic:
2827 case CMD_snomagic:
2828 case CMD_substitute:
2829 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002830 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 case CMD_tcl:
2832 case CMD_throw:
2833 case CMD_tilde:
2834 case CMD_topleft:
2835 case CMD_unlet:
2836 case CMD_verbose:
2837 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002838 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 break;
2840
2841 default: goto doend;
2842 }
2843 }
2844#endif
2845
2846 if (ea.argt & XFILE)
2847 {
2848 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2849 goto doend;
2850 }
2851
2852#ifdef FEAT_LISTCMDS
2853 /*
2854 * Accept buffer name. Cannot be used at the same time with a buffer
2855 * number. Don't do this for a user command.
2856 */
2857 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002858 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
2860 /*
2861 * :bdelete, :bwipeout and :bunload take several arguments, separated
2862 * by spaces: find next space (skipping over escaped characters).
2863 * The others take one argument: ignore trailing spaces.
2864 */
2865 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2866 || ea.cmdidx == CMD_bunload)
2867 p = skiptowhite_esc(ea.arg);
2868 else
2869 {
2870 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002871 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 --p;
2873 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002874 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2875 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (ea.line2 < 0) /* failed */
2877 goto doend;
2878 ea.addr_count = 1;
2879 ea.arg = skipwhite(p);
2880 }
2881#endif
2882
2883/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002884 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 *
2886 * The "ea" structure holds the arguments that can be used.
2887 */
2888 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002889 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 ea.cookie = cookie;
2891#ifdef FEAT_EVAL
2892 ea.cstack = cstack;
2893#endif
2894
2895#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002896 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
2898 /*
2899 * Execute a user-defined command.
2900 */
2901 do_ucmd(&ea);
2902 }
2903 else
2904#endif
2905 {
2906 /*
2907 * Call the function to execute the command.
2908 */
2909 ea.errmsg = NULL;
2910 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2911 if (ea.errmsg != NULL)
2912 errormsg = (char_u *)_(ea.errmsg);
2913 }
2914
2915#ifdef FEAT_EVAL
2916 /*
2917 * If the command just executed called do_cmdline(), any throw or ":return"
2918 * or ":finish" encountered there must also check the cstack of the still
2919 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2920 * exception, or reanimate a returned function or finished script file and
2921 * return or finish it again.
2922 */
2923 if (need_rethrow)
2924 do_throw(cstack);
2925 else if (check_cstack)
2926 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002927 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002929 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 && current_func_returned())
2931 do_return(&ea, TRUE, FALSE, NULL);
2932 }
2933 need_rethrow = check_cstack = FALSE;
2934#endif
2935
2936doend:
2937 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002940 curwin->w_cursor.col = 0;
2941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
2943 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2944 {
2945 if (sourcing)
2946 {
2947 if (errormsg != IObuff)
2948 {
2949 STRCPY(IObuff, errormsg);
2950 errormsg = IObuff;
2951 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002952 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
2954 emsg(errormsg);
2955 }
2956#ifdef FEAT_EVAL
2957 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002958 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2959 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960#endif
2961
2962 if (verbose_save >= 0)
2963 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002964#ifdef FEAT_AUTOCMD
2965 if (cmdmod.save_ei != NULL)
2966 {
2967 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002968 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2969 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002970 free_string_option(cmdmod.save_ei);
2971 }
2972#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002973 if (cmdmod.filter_regmatch.regprog != NULL)
2974 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 cmdmod = save_cmdmod;
2977
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002978 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
2980 /* messages could be enabled for a serious error, need to check if the
2981 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002982 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002983 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 emsg_silent -= did_esilent;
2985 if (emsg_silent < 0)
2986 emsg_silent = 0;
2987 /* Restore msg_scroll, it's set by file I/O commands, even when no
2988 * message is actually displayed. */
2989 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002990
2991 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2992 * somewhere in the line. Put it back in the first column. */
2993 if (redirecting())
2994 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002997#ifdef HAVE_SANDBOX
2998 if (did_sandbox)
2999 --sandbox;
3000#endif
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
3003 ea.nextcmd = NULL;
3004
3005#ifdef FEAT_EVAL
3006 --ex_nesting_level;
3007#endif
3008
3009 return ea.nextcmd;
3010}
3011#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00003012 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013#endif
3014
3015/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003016 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 * If there is a match advance "pp" to the argument and return TRUE.
3018 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003019 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003020checkforcmd(
3021 char_u **pp, /* start of command */
3022 char *cmd, /* name of command */
3023 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 int i;
3026
3027 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003028 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 break;
3030 if (i >= len && !isalpha((*pp)[i]))
3031 {
3032 *pp = skipwhite(*pp + i);
3033 return TRUE;
3034 }
3035 return FALSE;
3036}
3037
3038/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003039 * Append "cmd" to the error message in IObuff.
3040 * Takes care of limiting the length and handling 0xa0, which would be
3041 * invisible otherwise.
3042 */
3043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003044append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003045{
3046 char_u *s = cmd;
3047 char_u *d;
3048
3049 STRCAT(IObuff, ": ");
3050 d = IObuff + STRLEN(IObuff);
3051 while (*s != NUL && d - IObuff < IOSIZE - 7)
3052 {
3053 if (
3054#ifdef FEAT_MBYTE
3055 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3056#endif
3057 *s == 0xa0)
3058 {
3059 s +=
3060#ifdef FEAT_MBYTE
3061 enc_utf8 ? 2 :
3062#endif
3063 1;
3064 STRCPY(d, "<a0>");
3065 d += 4;
3066 }
3067 else
3068 MB_COPY_CHAR(s, d);
3069 }
3070 *d = NUL;
3071}
3072
3073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003075 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003077 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 * Returns NULL for an ambiguous user command.
3079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003081find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
3083 int len;
3084 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003085 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
3087 /*
3088 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003089 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 * - the 'k' command can directly be followed by any character.
3091 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003092 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003094 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 */
3096 p = eap->cmd;
3097 if (*p == 'k')
3098 {
3099 eap->cmdidx = CMD_k;
3100 ++p;
3101 }
3102 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003103 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3104 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 || p[1] == 'g'
3106 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3107 || p[1] == 'I'
3108 || (p[1] == 'r' && p[2] != 'e')))
3109 {
3110 eap->cmdidx = CMD_substitute;
3111 ++p;
3112 }
3113 else
3114 {
3115 while (ASCII_ISALPHA(*p))
3116 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003117 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003118 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003119 while (ASCII_ISALNUM(*p))
3120 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003121
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 /* check for non-alpha command */
3123 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3124 ++p;
3125 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003126 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3127 {
3128 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3129 * :delete with the 'l' flag. Same for 'p'. */
3130 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003131 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003132 break;
3133 if (i == len - 1)
3134 {
3135 --len;
3136 if (p[-1] == 'l')
3137 eap->flags |= EXFLAG_LIST;
3138 else
3139 eap->flags |= EXFLAG_PRINT;
3140 }
3141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003143 if (ASCII_ISLOWER(eap->cmd[0]))
3144 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003145 int c1 = eap->cmd[0];
3146 int c2 = eap->cmd[1];
3147
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003148 if (command_count != (int)CMD_SIZE)
3149 {
3150 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3151 getout(1);
3152 }
3153
3154 /* Use a precomputed index for fast look-up in cmdnames[]
3155 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003156 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3157 if (ASCII_ISLOWER(c2))
3158 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003161 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
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},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003324 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003325 {"hide", 3, FALSE},
3326 {"keepalt", 5, FALSE},
3327 {"keepjumps", 5, FALSE},
3328 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003329 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003330 {"leftabove", 5, FALSE},
3331 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003332 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003333 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003334 {"rightbelow", 6, FALSE},
3335 {"sandbox", 3, FALSE},
3336 {"silent", 3, FALSE},
3337 {"tab", 3, TRUE},
3338 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003339 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003340 {"verbose", 4, TRUE},
3341 {"vertical", 4, FALSE},
3342};
3343
3344/*
3345 * Return length of a command modifier (including optional count).
3346 * Return zero when it's not a modifier.
3347 */
3348 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003349modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350{
3351 int i, j;
3352 char_u *p = cmd;
3353
3354 if (VIM_ISDIGIT(*cmd))
3355 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003356 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003357 {
3358 for (j = 0; p[j] != NUL; ++j)
3359 if (p[j] != cmdmods[i].name[j])
3360 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003361 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003363 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003364 }
3365 return 0;
3366}
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368/*
3369 * Return > 0 if an Ex command "name" exists.
3370 * Return 2 if there is an exact match.
3371 * Return 3 if there is an ambiguous match.
3372 */
3373 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003374cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 exarg_T ea;
3377 int full = FALSE;
3378 int i;
3379 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003380 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003383 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
3385 for (j = 0; name[j] != NUL; ++j)
3386 if (name[j] != cmdmods[i].name[j])
3387 break;
3388 if (name[j] == NUL && j >= cmdmods[i].minlen)
3389 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3390 }
3391
Bram Moolenaara9587612006-05-04 21:47:50 +00003392 /* Check built-in commands and user defined commands.
3393 * For ":2match" and ":3match" we need to skip the number. */
3394 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003396 p = find_command(&ea, &full);
3397 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003399 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3400 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003401 if (*skipwhite(p) != NUL)
3402 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3404}
3405#endif
3406
3407/*
3408 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3409 * we don't need/want deleted. Maybe this could be done better if we didn't
3410 * repeat all this stuff. The only problem is that they may not stay
3411 * perfectly compatible with each other, but then the command line syntax
3412 * probably won't change that much -- webb.
3413 */
3414 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003415set_one_cmd_context(
3416 expand_T *xp,
3417 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 char_u *p;
3420 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003421 int len = 0;
3422 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3424 int compl = EXPAND_NOTHING;
3425#endif
3426#ifdef FEAT_CMDL_COMPL
3427 int delim;
3428#endif
3429 int forceit = FALSE;
3430 int usefilter = FALSE; /* filter instead of file name */
3431
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003432 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 xp->xp_pattern = buff;
3434 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003438 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 */
3440 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3441 ;
3442 xp->xp_pattern = cmd;
3443
3444 if (*cmd == NUL)
3445 return NULL;
3446 if (*cmd == '"') /* ignore comment lines */
3447 {
3448 xp->xp_context = EXPAND_NOTHING;
3449 return NULL;
3450 }
3451
3452/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003453 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
3455 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 xp->xp_pattern = cmd;
3457 if (*cmd == NUL)
3458 return NULL;
3459 if (*cmd == '"')
3460 {
3461 xp->xp_context = EXPAND_NOTHING;
3462 return NULL;
3463 }
3464
3465 if (*cmd == '|' || *cmd == '\n')
3466 return cmd + 1; /* There's another command */
3467
3468 /*
3469 * Isolate the command and search for it in the command table.
3470 * Exceptions:
3471 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003472 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3474 */
3475 if (*cmd == 'k' && cmd[1] != 'e')
3476 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 p = cmd + 1;
3479 }
3480 else
3481 {
3482 p = cmd;
3483 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3484 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003485 /* a user command may contain digits */
3486 if (ASCII_ISUPPER(cmd[0]))
3487 while (ASCII_ISALNUM(*p) || *p == '*')
3488 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003489 /* for python 3.x: ":py3*" commands completion */
3490 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003491 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003492 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003493 while (ASCII_ISALPHA(*p) || *p == '*')
3494 ++p;
3495 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003496 /* check for non-alpha command */
3497 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3498 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003501 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
3503 xp->xp_context = EXPAND_UNSUCCESSFUL;
3504 return NULL;
3505 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003506 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003507 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3508 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3509 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 break;
3511
3512#ifdef FEAT_USR_CMDS
3513 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3515 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516#endif
3517 }
3518
3519 /*
3520 * If the cursor is touching the command, and it ends in an alpha-numeric
3521 * character, complete the command name.
3522 */
3523 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3524 return NULL;
3525
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003526 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3529 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 p = cmd + 1;
3532 }
3533#ifdef FEAT_USR_CMDS
3534 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3535 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003536 ea.cmd = cmd;
3537 p = find_ucmd(&ea, p, NULL, xp,
3538# if defined(FEAT_CMDL_COMPL)
3539 &compl
3540# else
3541 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003544 if (p == NULL)
3545 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547#endif
3548 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
3551 /* Not still touching the command and it was an illegal one */
3552 xp->xp_context = EXPAND_UNSUCCESSFUL;
3553 return NULL;
3554 }
3555
3556 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3557
3558 if (*p == '!') /* forced commands */
3559 {
3560 forceit = TRUE;
3561 ++p;
3562 }
3563
3564/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003565 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003567 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003568 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
3570 arg = skipwhite(p);
3571
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003572 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
3574 if (*arg == '>') /* append */
3575 {
3576 if (*++arg == '>')
3577 ++arg;
3578 arg = skipwhite(arg);
3579 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582 ++arg;
3583 usefilter = TRUE;
3584 }
3585 }
3586
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003587 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 usefilter = forceit; /* :r! filter if forced */
3590 if (*arg == '!') /* :r !filter */
3591 {
3592 ++arg;
3593 usefilter = TRUE;
3594 }
3595 }
3596
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003597 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 while (*arg == *cmd) /* allow any number of '>' or '<' */
3600 ++arg;
3601 arg = skipwhite(arg);
3602 }
3603
3604 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003605 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 /* Check if we're in the +command */
3608 p = arg + 1;
3609 arg = skip_cmd_arg(arg, FALSE);
3610
3611 /* Still touching the command after '+'? */
3612 if (*arg == NUL)
3613 return p;
3614
3615 /* Skip space(s) after +command to get to the real argument */
3616 arg = skipwhite(arg);
3617 }
3618
3619 /*
3620 * Check for '|' to separate commands and '"' to start comments.
3621 * Don't do this for ":read !cmd" and ":write !cmd".
3622 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003623 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 p = arg;
3626 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003627 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 p += 2;
3629 while (*p)
3630 {
3631 if (*p == Ctrl_V)
3632 {
3633 if (p[1] != NUL)
3634 ++p;
3635 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003636 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 || *p == '|' || *p == '\n')
3638 {
3639 if (*(p - 1) != '\\')
3640 {
3641 if (*p == '|' || *p == '\n')
3642 return p + 1;
3643 return NULL; /* It's a comment */
3644 }
3645 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003646 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
3648 }
3649
3650 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003651 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 vim_strchr((char_u *)"|\"", *arg) == NULL)
3653 return NULL;
3654
3655 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003656 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003658 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003659 while (*p && p < buff + len)
3660 {
3661 if (*p == ' ' || *p == TAB)
3662 {
3663 /* argument starts after a space */
3664 xp->xp_pattern = ++p;
3665 }
3666 else
3667 {
3668 if (*p == '\\' && *(p + 1) != NUL)
3669 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003670 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003671 }
3672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003674 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003676 int c;
3677 int in_quote = FALSE;
3678 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 /*
3681 * Allow spaces within back-quotes to count as part of the argument
3682 * being expanded.
3683 */
3684 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003685 p = xp->xp_pattern;
3686 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003688#ifdef FEAT_MBYTE
3689 if (has_mbyte)
3690 c = mb_ptr2char(p);
3691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003693 c = *p;
3694 if (c == '\\' && p[1] != NUL)
3695 ++p;
3696 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
3698 if (!in_quote)
3699 {
3700 xp->xp_pattern = p;
3701 bow = p + 1;
3702 }
3703 in_quote = !in_quote;
3704 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003705 /* An argument can contain just about everything, except
3706 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003707 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003708#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003709 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003710#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003711 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003712 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003713 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003714 while (*p != NUL)
3715 {
3716#ifdef FEAT_MBYTE
3717 if (has_mbyte)
3718 c = mb_ptr2char(p);
3719 else
3720#endif
3721 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003722 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003723 break;
3724#ifdef FEAT_MBYTE
3725 if (has_mbyte)
3726 len = (*mb_ptr2len)(p);
3727 else
3728#endif
3729 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003730 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003731 }
3732 if (in_quote)
3733 bow = p;
3734 else
3735 xp->xp_pattern = p;
3736 p -= len;
3737 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003738 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740
3741 /*
3742 * If we are still inside the quotes, and we passed a space, just
3743 * expand from there.
3744 */
3745 if (bow != NULL && in_quote)
3746 xp->xp_pattern = bow;
3747 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003748
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003749 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003750 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003751 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003752#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003753 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003754#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003755 /* When still after the command name expand executables. */
3756 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003757 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
3760 /* Check for environment variable */
3761 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003762#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 || *xp->xp_pattern == '%'
3764#endif
3765 )
3766 {
3767 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3768 if (!vim_isIDc(*p))
3769 break;
3770 if (*p == NUL)
3771 {
3772 xp->xp_context = EXPAND_ENV_VARS;
3773 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003774#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3775 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003776 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003777 compl = EXPAND_ENV_VARS;
3778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003781#if defined(FEAT_CMDL_COMPL)
3782 /* Check for user names */
3783 if (*xp->xp_pattern == '~')
3784 {
3785 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3786 ;
3787 /* Complete ~user only if it partially matches a user name.
3788 * A full match ~user<Tab> will be replaced by user's home
3789 * directory i.e. something like ~user<Tab> -> /home/user/ */
3790 if (*p == NUL && p > xp->xp_pattern + 1
3791 && match_user(xp->xp_pattern + 1) == 1)
3792 {
3793 xp->xp_context = EXPAND_USER;
3794 ++xp->xp_pattern;
3795 }
3796 }
3797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799
3800/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003801 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003803 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003805 case CMD_find:
3806 case CMD_sfind:
3807 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003808 if (xp->xp_context == EXPAND_FILES)
3809 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003810 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 case CMD_cd:
3812 case CMD_chdir:
3813 case CMD_lcd:
3814 case CMD_lchdir:
3815 if (xp->xp_context == EXPAND_FILES)
3816 xp->xp_context = EXPAND_DIRECTORIES;
3817 break;
3818 case CMD_help:
3819 xp->xp_context = EXPAND_HELP;
3820 xp->xp_pattern = arg;
3821 break;
3822
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003823 /* Command modifiers: return the argument.
3824 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003826 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 case CMD_belowright:
3828 case CMD_botright:
3829 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003830 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003831 case CMD_cdo:
3832 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003834 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 case CMD_folddoclosed:
3836 case CMD_folddoopen:
3837 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003838 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 case CMD_keepjumps:
3840 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003841 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003842 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003844 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003846 case CMD_noautocmd:
3847 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003849 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003851 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003852 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 case CMD_topleft:
3854 case CMD_verbose:
3855 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003856 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return arg;
3858
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003859 case CMD_filter:
3860 if (*arg != NUL)
3861 arg = skip_vimgrep_pat(arg, NULL, NULL);
3862 if (arg == NULL || *arg == NUL)
3863 {
3864 xp->xp_context = EXPAND_NOTHING;
3865 return NULL;
3866 }
3867 return skipwhite(arg);
3868
Bram Moolenaar4f688582007-07-24 12:34:30 +00003869#ifdef FEAT_CMDL_COMPL
3870# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 case CMD_match:
3872 if (*arg == NUL || !ends_excmd(*arg))
3873 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003874 /* also complete "None" */
3875 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 arg = skipwhite(skiptowhite(arg));
3877 if (*arg != NUL)
3878 {
3879 xp->xp_context = EXPAND_NOTHING;
3880 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3881 }
3882 }
3883 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003884# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886/*
3887 * All completion for the +cmdline_compl feature goes here.
3888 */
3889
3890# ifdef FEAT_USR_CMDS
3891 case CMD_command:
3892 /* Check for attributes */
3893 while (*arg == '-')
3894 {
3895 arg++; /* Skip "-" */
3896 p = skiptowhite(arg);
3897 if (*p == NUL)
3898 {
3899 /* Cursor is still in the attribute */
3900 p = vim_strchr(arg, '=');
3901 if (p == NULL)
3902 {
3903 /* No "=", so complete attribute names */
3904 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3905 xp->xp_pattern = arg;
3906 return NULL;
3907 }
3908
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003909 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 * their arguments as well.
3911 */
3912 if (STRNICMP(arg, "complete", p - arg) == 0)
3913 {
3914 xp->xp_context = EXPAND_USER_COMPLETE;
3915 xp->xp_pattern = p + 1;
3916 return NULL;
3917 }
3918 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3919 {
3920 xp->xp_context = EXPAND_USER_NARGS;
3921 xp->xp_pattern = p + 1;
3922 return NULL;
3923 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003924 else if (STRNICMP(arg, "addr", p - arg) == 0)
3925 {
3926 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3927 xp->xp_pattern = p + 1;
3928 return NULL;
3929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 return NULL;
3931 }
3932 arg = skipwhite(p);
3933 }
3934
3935 /* After the attributes comes the new command name */
3936 p = skiptowhite(arg);
3937 if (*p == NUL)
3938 {
3939 xp->xp_context = EXPAND_USER_COMMANDS;
3940 xp->xp_pattern = arg;
3941 break;
3942 }
3943
3944 /* And finally comes a normal command */
3945 return skipwhite(p);
3946
3947 case CMD_delcommand:
3948 xp->xp_context = EXPAND_USER_COMMANDS;
3949 xp->xp_pattern = arg;
3950 break;
3951# endif
3952
3953 case CMD_global:
3954 case CMD_vglobal:
3955 delim = *arg; /* get the delimiter */
3956 if (delim)
3957 ++arg; /* skip delimiter if there is one */
3958
3959 while (arg[0] != NUL && arg[0] != delim)
3960 {
3961 if (arg[0] == '\\' && arg[1] != NUL)
3962 ++arg;
3963 ++arg;
3964 }
3965 if (arg[0] != NUL)
3966 return arg + 1;
3967 break;
3968 case CMD_and:
3969 case CMD_substitute:
3970 delim = *arg;
3971 if (delim)
3972 {
3973 /* skip "from" part */
3974 ++arg;
3975 arg = skip_regexp(arg, delim, p_magic, NULL);
3976 }
3977 /* skip "to" part */
3978 while (arg[0] != NUL && arg[0] != delim)
3979 {
3980 if (arg[0] == '\\' && arg[1] != NUL)
3981 ++arg;
3982 ++arg;
3983 }
3984 if (arg[0] != NUL) /* skip delimiter */
3985 ++arg;
3986 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3987 ++arg;
3988 if (arg[0] != NUL)
3989 return arg;
3990 break;
3991 case CMD_isearch:
3992 case CMD_dsearch:
3993 case CMD_ilist:
3994 case CMD_dlist:
3995 case CMD_ijump:
3996 case CMD_psearch:
3997 case CMD_djump:
3998 case CMD_isplit:
3999 case CMD_dsplit:
4000 arg = skipwhite(skipdigits(arg)); /* skip count */
4001 if (*arg == '/') /* Match regexp, not just whole words */
4002 {
4003 for (++arg; *arg && *arg != '/'; arg++)
4004 if (*arg == '\\' && arg[1] != NUL)
4005 arg++;
4006 if (*arg)
4007 {
4008 arg = skipwhite(arg + 1);
4009
4010 /* Check for trailing illegal characters */
4011 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4012 xp->xp_context = EXPAND_NOTHING;
4013 else
4014 return arg;
4015 }
4016 }
4017 break;
4018#ifdef FEAT_AUTOCMD
4019 case CMD_autocmd:
4020 return set_context_in_autocmd(xp, arg, FALSE);
4021
4022 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004023 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 return set_context_in_autocmd(xp, arg, TRUE);
4025#endif
4026 case CMD_set:
4027 set_context_in_set_cmd(xp, arg, 0);
4028 break;
4029 case CMD_setglobal:
4030 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4031 break;
4032 case CMD_setlocal:
4033 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4034 break;
4035 case CMD_tag:
4036 case CMD_stag:
4037 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004038 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 case CMD_tselect:
4040 case CMD_stselect:
4041 case CMD_ptselect:
4042 case CMD_tjump:
4043 case CMD_stjump:
4044 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004045 if (*p_wop != NUL)
4046 xp->xp_context = EXPAND_TAGS_LISTFILES;
4047 else
4048 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 xp->xp_pattern = arg;
4050 break;
4051 case CMD_augroup:
4052 xp->xp_context = EXPAND_AUGROUP;
4053 xp->xp_pattern = arg;
4054 break;
4055#ifdef FEAT_SYN_HL
4056 case CMD_syntax:
4057 set_context_in_syntax_cmd(xp, arg);
4058 break;
4059#endif
4060#ifdef FEAT_EVAL
4061 case CMD_let:
4062 case CMD_if:
4063 case CMD_elseif:
4064 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004065 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 case CMD_echo:
4067 case CMD_echon:
4068 case CMD_execute:
4069 case CMD_echomsg:
4070 case CMD_echoerr:
4071 case CMD_call:
4072 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004073 case CMD_cexpr:
4074 case CMD_caddexpr:
4075 case CMD_cgetexpr:
4076 case CMD_lexpr:
4077 case CMD_laddexpr:
4078 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004079 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 break;
4081
4082 case CMD_unlet:
4083 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4084 arg = xp->xp_pattern + 1;
4085 xp->xp_context = EXPAND_USER_VARS;
4086 xp->xp_pattern = arg;
4087 break;
4088
4089 case CMD_function:
4090 case CMD_delfunction:
4091 xp->xp_context = EXPAND_USER_FUNC;
4092 xp->xp_pattern = arg;
4093 break;
4094
4095 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004096 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 break;
4098#endif
4099 case CMD_highlight:
4100 set_context_in_highlight_cmd(xp, arg);
4101 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004102#ifdef FEAT_CSCOPE
4103 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004104 case CMD_lcscope:
4105 case CMD_scscope:
4106 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004107 break;
4108#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004109#ifdef FEAT_SIGNS
4110 case CMD_sign:
4111 set_context_in_sign_cmd(xp, arg);
4112 break;
4113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114#ifdef FEAT_LISTCMDS
4115 case CMD_bdelete:
4116 case CMD_bwipeout:
4117 case CMD_bunload:
4118 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4119 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004120 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 case CMD_buffer:
4122 case CMD_sbuffer:
4123 case CMD_checktime:
4124 xp->xp_context = EXPAND_BUFFERS;
4125 xp->xp_pattern = arg;
4126 break;
4127#endif
4128#ifdef FEAT_USR_CMDS
4129 case CMD_USER:
4130 case CMD_USER_BUF:
4131 if (compl != EXPAND_NOTHING)
4132 {
4133 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004134 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
4136# ifdef FEAT_MENU
4137 if (compl == EXPAND_MENUS)
4138 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4139# endif
4140 if (compl == EXPAND_COMMANDS)
4141 return arg;
4142 if (compl == EXPAND_MAPPINGS)
4143 return set_context_in_map_cmd(xp, (char_u *)"map",
4144 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004145 /* Find start of last argument. */
4146 p = arg;
4147 while (*p)
4148 {
4149 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004150 /* argument starts after a space */
4151 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004152 else if (*p == '\\' && *(p + 1) != NUL)
4153 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004154 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 xp->xp_pattern = arg;
4157 }
4158 xp->xp_context = compl;
4159 }
4160 break;
4161#endif
4162 case CMD_map: case CMD_noremap:
4163 case CMD_nmap: case CMD_nnoremap:
4164 case CMD_vmap: case CMD_vnoremap:
4165 case CMD_omap: case CMD_onoremap:
4166 case CMD_imap: case CMD_inoremap:
4167 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004168 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004169 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004170 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004171 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004173 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 case CMD_unmap:
4175 case CMD_nunmap:
4176 case CMD_vunmap:
4177 case CMD_ounmap:
4178 case CMD_iunmap:
4179 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004180 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004181 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004182 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004183 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004185 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004186 case CMD_mapclear:
4187 case CMD_nmapclear:
4188 case CMD_vmapclear:
4189 case CMD_omapclear:
4190 case CMD_imapclear:
4191 case CMD_cmapclear:
4192 case CMD_lmapclear:
4193 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004194 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004195 case CMD_xmapclear:
4196 xp->xp_context = EXPAND_MAPCLEAR;
4197 xp->xp_pattern = arg;
4198 break;
4199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 case CMD_abbreviate: case CMD_noreabbrev:
4201 case CMD_cabbrev: case CMD_cnoreabbrev:
4202 case CMD_iabbrev: case CMD_inoreabbrev:
4203 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004204 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 case CMD_unabbreviate:
4206 case CMD_cunabbrev:
4207 case CMD_iunabbrev:
4208 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004209 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210#ifdef FEAT_MENU
4211 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4212 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4213 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4214 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4215 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4216 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4217 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4218 case CMD_tmenu: case CMD_tunmenu:
4219 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4220 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4221#endif
4222
4223 case CMD_colorscheme:
4224 xp->xp_context = EXPAND_COLORS;
4225 xp->xp_pattern = arg;
4226 break;
4227
4228 case CMD_compiler:
4229 xp->xp_context = EXPAND_COMPILER;
4230 xp->xp_pattern = arg;
4231 break;
4232
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004233 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004234 xp->xp_context = EXPAND_OWNSYNTAX;
4235 xp->xp_pattern = arg;
4236 break;
4237
4238 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004239 xp->xp_context = EXPAND_FILETYPE;
4240 xp->xp_pattern = arg;
4241 break;
4242
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004243 case CMD_packadd:
4244 xp->xp_context = EXPAND_PACKADD;
4245 xp->xp_pattern = arg;
4246 break;
4247
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4249 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4250 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004251 p = skiptowhite(arg);
4252 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
4254 xp->xp_context = EXPAND_LANGUAGE;
4255 xp->xp_pattern = arg;
4256 }
4257 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004258 {
4259 if ( STRNCMP(arg, "messages", p - arg) == 0
4260 || STRNCMP(arg, "ctype", p - arg) == 0
4261 || STRNCMP(arg, "time", p - arg) == 0)
4262 {
4263 xp->xp_context = EXPAND_LOCALES;
4264 xp->xp_pattern = skipwhite(p);
4265 }
4266 else
4267 xp->xp_context = EXPAND_NOTHING;
4268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 break;
4270#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004271#if defined(FEAT_PROFILE)
4272 case CMD_profile:
4273 set_context_in_profile_cmd(xp, arg);
4274 break;
4275#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004276 case CMD_behave:
4277 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004278 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004281 case CMD_messages:
4282 xp->xp_context = EXPAND_MESSAGES;
4283 xp->xp_pattern = arg;
4284 break;
4285
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004286#if defined(FEAT_CMDHIST)
4287 case CMD_history:
4288 xp->xp_context = EXPAND_HISTORY;
4289 xp->xp_pattern = arg;
4290 break;
4291#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004292#if defined(FEAT_PROFILE)
4293 case CMD_syntime:
4294 xp->xp_context = EXPAND_SYNTIME;
4295 xp->xp_pattern = arg;
4296 break;
4297#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004298
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299#endif /* FEAT_CMDL_COMPL */
4300
4301 default:
4302 break;
4303 }
4304 return NULL;
4305}
4306
4307/*
4308 * skip a range specifier of the form: addr [,addr] [;addr] ..
4309 *
4310 * Backslashed delimiters after / or ? will be skipped, and commands will
4311 * not be expanded between /'s and ?'s or after "'".
4312 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004313 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 * Returns the "cmd" pointer advanced to beyond the range.
4315 */
4316 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004317skip_range(
4318 char_u *cmd,
4319 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004321 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004323 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004325 if (*cmd == '\\')
4326 {
4327 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4328 ++cmd;
4329 else
4330 break;
4331 }
4332 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 {
4334 if (*++cmd == NUL && ctx != NULL)
4335 *ctx = EXPAND_NOTHING;
4336 }
4337 else if (*cmd == '/' || *cmd == '?')
4338 {
4339 delim = *cmd++;
4340 while (*cmd != NUL && *cmd != delim)
4341 if (*cmd++ == '\\' && *cmd != NUL)
4342 ++cmd;
4343 if (*cmd == NUL && ctx != NULL)
4344 *ctx = EXPAND_NOTHING;
4345 }
4346 if (*cmd != NUL)
4347 ++cmd;
4348 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004349
4350 /* Skip ":" and white space. */
4351 while (*cmd == ':')
4352 cmd = skipwhite(cmd + 1);
4353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 return cmd;
4355}
4356
4357/*
4358 * get a single EX address
4359 *
4360 * Set ptr to the next character after the part that was interpreted.
4361 * Set ptr to NULL when an error is encountered.
4362 *
4363 * Return MAXLNUM when no Ex address was found.
4364 */
4365 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004366get_address(
4367 exarg_T *eap UNUSED,
4368 char_u **ptr,
4369 int addr_type, /* flag: one of ADDR_LINES, ... */
4370 int skip, /* only skip the address, don't use it */
Bram Moolenaarded27822017-01-02 14:27:34 +01004371 int to_other_file, /* flag: may jump to other file */
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01004372 int address_count UNUSED) /* 1 for first address, >1 after comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
4374 int c;
4375 int i;
4376 long n;
4377 char_u *cmd;
4378 pos_T pos;
4379 pos_T *fp;
4380 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004381 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383 cmd = skipwhite(*ptr);
4384 lnum = MAXLNUM;
4385 do
4386 {
4387 switch (*cmd)
4388 {
4389 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004390 ++cmd;
4391 switch (addr_type)
4392 {
4393 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 lnum = curwin->w_cursor.lnum;
4395 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004397 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 break;
4399 case ADDR_ARGUMENTS:
4400 lnum = curwin->w_arg_idx + 1;
4401 break;
4402 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004403 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 lnum = curbuf->b_fnum;
4405 break;
4406 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004407 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004409 case ADDR_TABS_RELATIVE:
4410 EMSG(_(e_invrange));
4411 cmd = NULL;
4412 goto error;
4413 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004414#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004415 case ADDR_QUICKFIX:
4416 lnum = qf_get_cur_valid_idx(eap);
4417 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004418#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 }
4420 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 ++cmd;
4424 switch (addr_type)
4425 {
4426 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 lnum = curbuf->b_ml.ml_line_count;
4428 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004430 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004431 break;
4432 case ADDR_ARGUMENTS:
4433 lnum = ARGCOUNT;
4434 break;
4435 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004436 buf = lastbuf;
4437 while (buf->b_ml.ml_mfp == NULL)
4438 {
4439 if (buf->b_prev == NULL)
4440 break;
4441 buf = buf->b_prev;
4442 }
4443 lnum = buf->b_fnum;
4444 break;
4445 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 lnum = lastbuf->b_fnum;
4447 break;
4448 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004449 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004451 case ADDR_TABS_RELATIVE:
4452 EMSG(_(e_invrange));
4453 cmd = NULL;
4454 goto error;
4455 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004456#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004457 case ADDR_QUICKFIX:
4458 lnum = qf_get_size(eap);
4459 if (lnum == 0)
4460 lnum = 1;
4461 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004462#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 }
4464 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 if (*++cmd == NUL)
4468 {
4469 cmd = NULL;
4470 goto error;
4471 }
4472 if (addr_type != ADDR_LINES)
4473 {
4474 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004475 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 goto error;
4477 }
4478 if (skip)
4479 ++cmd;
4480 else
4481 {
4482 /* Only accept a mark in another file when it is
4483 * used by itself: ":'M". */
4484 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4485 ++cmd;
4486 if (fp == (pos_T *)-1)
4487 /* Jumped to another file. */
4488 lnum = curwin->w_cursor.lnum;
4489 else
4490 {
4491 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
4493 cmd = NULL;
4494 goto error;
4495 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 lnum = fp->lnum;
4497 }
4498 }
4499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 case '/':
4502 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 c = *cmd++;
4504 if (addr_type != ADDR_LINES)
4505 {
4506 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004507 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 goto error;
4509 }
4510 if (skip) /* skip "/pat/" */
4511 {
4512 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4513 if (*cmd == c)
4514 ++cmd;
4515 }
4516 else
4517 {
4518 pos = curwin->w_cursor; /* save curwin->w_cursor */
4519 /*
4520 * When '/' or '?' follows another address, start
4521 * from there.
4522 */
4523 if (lnum != MAXLNUM)
4524 curwin->w_cursor.lnum = lnum;
4525 /*
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004526 * Start a forward search at the end of the line (unless
4527 * before the first line).
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004528 * Start a backward search at the start of the line.
4529 * This makes sure we never match in the current
4530 * line, and can match anywhere in the
4531 * next/previous line.
4532 */
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004533 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004534 curwin->w_cursor.col = MAXCOL;
4535 else
4536 curwin->w_cursor.col = 0;
4537 searchcmdlen = 0;
4538 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004539 SEARCH_HIS | SEARCH_MSG, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004540 {
4541 curwin->w_cursor = pos;
4542 cmd = NULL;
4543 goto error;
4544 }
4545 lnum = curwin->w_cursor.lnum;
4546 curwin->w_cursor = pos;
4547 /* adjust command string pointer */
4548 cmd += searchcmdlen;
4549 }
4550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004553 ++cmd;
4554 if (addr_type != ADDR_LINES)
4555 {
4556 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004557 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004558 goto error;
4559 }
4560 if (*cmd == '&')
4561 i = RE_SUBST;
4562 else if (*cmd == '?' || *cmd == '/')
4563 i = RE_SEARCH;
4564 else
4565 {
4566 EMSG(_(e_backslash));
4567 cmd = NULL;
4568 goto error;
4569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004571 if (!skip)
4572 {
4573 /*
4574 * When search follows another address, start from
4575 * there.
4576 */
4577 if (lnum != MAXLNUM)
4578 pos.lnum = lnum;
4579 else
4580 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 /*
4583 * Start the search just like for the above
4584 * do_search().
4585 */
4586 if (*cmd != '?')
4587 pos.col = MAXCOL;
4588 else
4589 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004590#ifdef FEAT_VIRTUALEDIT
4591 pos.coladd = 0;
4592#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 if (searchit(curwin, curbuf, &pos,
4594 *cmd == '?' ? BACKWARD : FORWARD,
4595 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004596 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004597 lnum = pos.lnum;
4598 else
4599 {
4600 cmd = NULL;
4601 goto error;
4602 }
4603 }
4604 ++cmd;
4605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004608 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4609 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611
4612 for (;;)
4613 {
4614 cmd = skipwhite(cmd);
4615 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4616 break;
4617
4618 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004619 {
4620 switch (addr_type)
4621 {
4622 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004623 /* "+1" is same as ".+1" */
4624 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004625 break;
4626 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004627 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004628 break;
4629 case ADDR_ARGUMENTS:
4630 lnum = curwin->w_arg_idx + 1;
4631 break;
4632 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004633 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 lnum = curbuf->b_fnum;
4635 break;
4636 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004637 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004639 case ADDR_TABS_RELATIVE:
4640 lnum = 1;
4641 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004642#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004643 case ADDR_QUICKFIX:
4644 lnum = qf_get_cur_valid_idx(eap);
4645 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004646#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004647 }
4648 }
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (VIM_ISDIGIT(*cmd))
4651 i = '+'; /* "number" is same as "+number" */
4652 else
4653 i = *cmd++;
4654 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4655 n = 1;
4656 else
4657 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004658
4659 if (addr_type == ADDR_TABS_RELATIVE)
4660 {
4661 EMSG(_(e_invrange));
4662 cmd = NULL;
4663 goto error;
4664 }
4665 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004666 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004667 lnum = compute_buffer_local_count(
4668 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004670 {
4671#ifdef FEAT_FOLDING
4672 /* Relative line addressing, need to adjust for folded lines
4673 * now, but only do it after the first address. */
4674 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4675 && address_count >= 2)
4676 (void)hasFolding(lnum, NULL, &lnum);
4677#endif
4678 if (i == '-')
4679 lnum -= n;
4680 else
4681 lnum += n;
4682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
4684 } while (*cmd == '/' || *cmd == '?');
4685
4686error:
4687 *ptr = cmd;
4688 return lnum;
4689}
4690
4691/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004692 * Get flags from an Ex command argument.
4693 */
4694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004695get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004696{
4697 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4698 {
4699 if (*eap->arg == 'l')
4700 eap->flags |= EXFLAG_LIST;
4701 else if (*eap->arg == 'p')
4702 eap->flags |= EXFLAG_PRINT;
4703 else
4704 eap->flags |= EXFLAG_NR;
4705 eap->arg = skipwhite(eap->arg + 1);
4706 }
4707}
4708
4709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 * Function called for command which is Not Implemented. NI!
4711 */
4712 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004713ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 if (!eap->skip)
4716 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4717}
4718
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004719#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720/*
4721 * Function called for script command which is Not Implemented. NI!
4722 * Skips over ":perl <<EOF" constructs.
4723 */
4724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004725ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
4727 if (!eap->skip)
4728 ex_ni(eap);
4729 else
4730 vim_free(script_get(eap, eap->arg));
4731}
4732#endif
4733
4734/*
4735 * Check range in Ex command for validity.
4736 * Return NULL when valid, error message when invalid.
4737 */
4738 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004739invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004741 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if ( eap->line1 < 0
4743 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004744 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004746
4747 if (eap->argt & RANGE)
4748 {
4749 switch(eap->addr_type)
4750 {
4751 case ADDR_LINES:
4752 if (!(eap->argt & NOTADR)
4753 && eap->line2 > curbuf->b_ml.ml_line_count
4754#ifdef FEAT_DIFF
4755 + (eap->cmdidx == CMD_diffget)
4756#endif
4757 )
4758 return (char_u *)_(e_invrange);
4759 break;
4760 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004761 /* add 1 if ARGCOUNT is 0 */
4762 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004763 return (char_u *)_(e_invrange);
4764 break;
4765 case ADDR_BUFFERS:
4766 if (eap->line1 < firstbuf->b_fnum
4767 || eap->line2 > lastbuf->b_fnum)
4768 return (char_u *)_(e_invrange);
4769 break;
4770 case ADDR_LOADED_BUFFERS:
4771 buf = firstbuf;
4772 while (buf->b_ml.ml_mfp == NULL)
4773 {
4774 if (buf->b_next == NULL)
4775 return (char_u *)_(e_invrange);
4776 buf = buf->b_next;
4777 }
4778 if (eap->line1 < buf->b_fnum)
4779 return (char_u *)_(e_invrange);
4780 buf = lastbuf;
4781 while (buf->b_ml.ml_mfp == NULL)
4782 {
4783 if (buf->b_prev == NULL)
4784 return (char_u *)_(e_invrange);
4785 buf = buf->b_prev;
4786 }
4787 if (eap->line2 > buf->b_fnum)
4788 return (char_u *)_(e_invrange);
4789 break;
4790 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004791 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004792 return (char_u *)_(e_invrange);
4793 break;
4794 case ADDR_TABS:
4795 if (eap->line2 > LAST_TAB_NR)
4796 return (char_u *)_(e_invrange);
4797 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004798 case ADDR_TABS_RELATIVE:
4799 /* Do nothing */
4800 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004801#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004802 case ADDR_QUICKFIX:
4803 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4804 return (char_u *)_(e_invrange);
4805 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004806#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004807 }
4808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return NULL;
4810}
4811
4812/*
4813 * Correct the range for zero line number, if required.
4814 */
4815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004816correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817{
4818 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4819 {
4820 if (eap->line1 == 0)
4821 eap->line1 = 1;
4822 if (eap->line2 == 0)
4823 eap->line2 = 1;
4824 }
4825}
4826
Bram Moolenaar748bf032005-02-02 23:04:36 +00004827#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004828static char_u *skip_grep_pat(exarg_T *eap);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004829
4830/*
4831 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4832 * pattern. Otherwise return eap->arg.
4833 */
4834 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004835skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004836{
4837 char_u *p = eap->arg;
4838
Bram Moolenaara37420f2006-02-04 22:37:47 +00004839 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4840 || eap->cmdidx == CMD_vimgrepadd
4841 || eap->cmdidx == CMD_lvimgrepadd
4842 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004843 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004844 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004845 if (p == NULL)
4846 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004847 }
4848 return p;
4849}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004850
4851/*
4852 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4853 * in the command line, so that things like % get expanded.
4854 */
4855 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004856replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004857{
4858 char_u *new_cmdline;
4859 char_u *program;
4860 char_u *pos;
4861 char_u *ptr;
4862 int len;
4863 int i;
4864
4865 /*
4866 * Don't do it when ":vimgrep" is used for ":grep".
4867 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004868 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4869 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4870 || eap->cmdidx == CMD_grepadd
4871 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004872 && !grep_internal(eap->cmdidx))
4873 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004874 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4875 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004876 {
4877 if (*curbuf->b_p_gp == NUL)
4878 program = p_gp;
4879 else
4880 program = curbuf->b_p_gp;
4881 }
4882 else
4883 {
4884 if (*curbuf->b_p_mp == NUL)
4885 program = p_mp;
4886 else
4887 program = curbuf->b_p_mp;
4888 }
4889
4890 p = skipwhite(p);
4891
4892 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4893 {
4894 /* replace $* by given arguments */
4895 i = 1;
4896 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4897 ++i;
4898 len = (int)STRLEN(p);
4899 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4900 if (new_cmdline == NULL)
4901 return NULL; /* out of memory */
4902 ptr = new_cmdline;
4903 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4904 {
4905 i = (int)(pos - program);
4906 STRNCPY(ptr, program, i);
4907 STRCPY(ptr += i, p);
4908 ptr += len;
4909 program = pos + 2;
4910 }
4911 STRCPY(ptr, program);
4912 }
4913 else
4914 {
4915 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4916 if (new_cmdline == NULL)
4917 return NULL; /* out of memory */
4918 STRCPY(new_cmdline, program);
4919 STRCAT(new_cmdline, " ");
4920 STRCAT(new_cmdline, p);
4921 }
4922 msg_make(p);
4923
4924 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4925 vim_free(*cmdlinep);
4926 *cmdlinep = new_cmdline;
4927 p = new_cmdline;
4928 }
4929 return p;
4930}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004931#endif
4932
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933/*
4934 * Expand file name in Ex command argument.
4935 * Return FAIL for failure, OK otherwise.
4936 */
4937 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004938expand_filename(
4939 exarg_T *eap,
4940 char_u **cmdlinep,
4941 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942{
4943 int has_wildcards; /* need to expand wildcards */
4944 char_u *repl;
4945 int srclen;
4946 char_u *p;
4947 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004948 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
Bram Moolenaar748bf032005-02-02 23:04:36 +00004950#ifdef FEAT_QUICKFIX
4951 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4952 p = skip_grep_pat(eap);
4953#else
4954 p = eap->arg;
4955#endif
4956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 /*
4958 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4959 * the file name contains a wildcard it should not cause expanding.
4960 * (it will be expanded anyway if there is a wildcard before replacing).
4961 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004962 has_wildcards = mch_has_wildcard(p);
4963 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004965#ifdef FEAT_EVAL
4966 /* Skip over `=expr`, wildcards in it are not expanded. */
4967 if (p[0] == '`' && p[1] == '=')
4968 {
4969 p += 2;
4970 (void)skip_expr(&p);
4971 if (*p == '`')
4972 ++p;
4973 continue;
4974 }
4975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 /*
4977 * Quick check if this cannot be the start of a special string.
4978 * Also removes backslash before '%', '#' and '<'.
4979 */
4980 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4981 {
4982 ++p;
4983 continue;
4984 }
4985
4986 /*
4987 * Try to find a match at this position.
4988 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004989 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4990 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (*errormsgp != NULL) /* error detected */
4992 return FAIL;
4993 if (repl == NULL) /* no match found */
4994 {
4995 p += srclen;
4996 continue;
4997 }
4998
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004999 /* Wildcards won't be expanded below, the replacement is taken
5000 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5001 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5002 {
5003 char_u *l = repl;
5004
5005 repl = expand_env_save(repl);
5006 vim_free(l);
5007 }
5008
Bram Moolenaar63b92542007-03-27 14:57:09 +00005009 /* Need to escape white space et al. with a backslash.
5010 * Don't do this for:
5011 * - replacement that already has been escaped: "##"
5012 * - shell commands (may have to use quotes instead).
5013 * - non-unix systems when there is a single argument (spaces don't
5014 * separate arguments then).
5015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005017 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 && eap->cmdidx != CMD_grep
5020 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005021 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005022 && eap->cmdidx != CMD_lgrep
5023 && eap->cmdidx != CMD_lgrepadd
5024 && eap->cmdidx != CMD_lmake
5025 && eap->cmdidx != CMD_make
5026 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027#ifndef UNIX
5028 && !(eap->argt & NOSPC)
5029#endif
5030 )
5031 {
5032 char_u *l;
5033#ifdef BACKSLASH_IN_FILENAME
5034 /* Don't escape a backslash here, because rem_backslash() doesn't
5035 * remove it later. */
5036 static char_u *nobslash = (char_u *)" \t\"|";
5037# define ESCAPE_CHARS nobslash
5038#else
5039# define ESCAPE_CHARS escape_chars
5040#endif
5041
5042 for (l = repl; *l; ++l)
5043 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5044 {
5045 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5046 if (l != NULL)
5047 {
5048 vim_free(repl);
5049 repl = l;
5050 }
5051 break;
5052 }
5053 }
5054
5055 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005056 if ((eap->usefilter || eap->cmdidx == CMD_bang
5057 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005058 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 char_u *l;
5061
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005062 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (l != NULL)
5064 {
5065 vim_free(repl);
5066 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068 }
5069
5070 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5071 vim_free(repl);
5072 if (p == NULL)
5073 return FAIL;
5074 }
5075
5076 /*
5077 * One file argument: Expand wildcards.
5078 * Don't do this with ":r !command" or ":w !command".
5079 */
5080 if ((eap->argt & NOSPC) && !eap->usefilter)
5081 {
5082 /*
5083 * May do this twice:
5084 * 1. Replace environment variables.
5085 * 2. Replace any other wildcards, remove backslashes.
5086 */
5087 for (n = 1; n <= 2; ++n)
5088 {
5089 if (n == 2)
5090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 /*
5092 * Halve the number of backslashes (this is Vi compatible).
5093 * For Unix and OS/2, when wildcards are expanded, this is
5094 * done by ExpandOne() below.
5095 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005096#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (!has_wildcards)
5098#endif
5099 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101
5102 if (has_wildcards)
5103 {
5104 if (n == 1)
5105 {
5106 /*
5107 * First loop: May expand environment variables. This
5108 * can be done much faster with expand_env() than with
5109 * something else (e.g., calling a shell).
5110 * After expanding environment variables, check again
5111 * if there are still wildcards present.
5112 */
5113 if (vim_strchr(eap->arg, '$') != NULL
5114 || vim_strchr(eap->arg, '~') != NULL)
5115 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005116 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005117 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 has_wildcards = mch_has_wildcard(NameBuff);
5119 p = NameBuff;
5120 }
5121 else
5122 p = NULL;
5123 }
5124 else /* n == 2 */
5125 {
5126 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005127 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 ExpandInit(&xpc);
5130 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005131 if (p_wic)
5132 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005134 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 if (p == NULL)
5136 return FAIL;
5137 }
5138 if (p != NULL)
5139 {
5140 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5141 p, cmdlinep);
5142 if (n == 2) /* p came from ExpandOne() */
5143 vim_free(p);
5144 }
5145 }
5146 }
5147 }
5148 return OK;
5149}
5150
5151/*
5152 * Replace part of the command line, keeping eap->cmd, eap->arg and
5153 * eap->nextcmd correct.
5154 * "src" points to the part that is to be replaced, of length "srclen".
5155 * "repl" is the replacement string.
5156 * Returns a pointer to the character after the replaced string.
5157 * Returns NULL for failure.
5158 */
5159 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005160repl_cmdline(
5161 exarg_T *eap,
5162 char_u *src,
5163 int srclen,
5164 char_u *repl,
5165 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 int len;
5168 int i;
5169 char_u *new_cmdline;
5170
5171 /*
5172 * The new command line is build in new_cmdline[].
5173 * First allocate it.
5174 * Careful: a "+cmd" argument may have been NUL terminated.
5175 */
5176 len = (int)STRLEN(repl);
5177 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005178 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5180 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5181 return NULL; /* out of memory! */
5182
5183 /*
5184 * Copy the stuff before the expanded part.
5185 * Copy the expanded stuff.
5186 * Copy what came after the expanded part.
5187 * Copy the next commands, if there are any.
5188 */
5189 i = (int)(src - *cmdlinep); /* length of part before match */
5190 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 mch_memmove(new_cmdline + i, repl, (size_t)len);
5193 i += len; /* remember the end of the string */
5194 STRCPY(new_cmdline + i, src + srclen);
5195 src = new_cmdline + i; /* remember where to continue */
5196
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005197 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
5199 i = (int)STRLEN(new_cmdline) + 1;
5200 STRCPY(new_cmdline + i, eap->nextcmd);
5201 eap->nextcmd = new_cmdline + i;
5202 }
5203 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5204 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5205 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5206 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5207 vim_free(*cmdlinep);
5208 *cmdlinep = new_cmdline;
5209
5210 return src;
5211}
5212
5213/*
5214 * Check for '|' to separate commands and '"' to start comments.
5215 */
5216 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005217separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218{
5219 char_u *p;
5220
Bram Moolenaar86b68352004-12-27 21:59:20 +00005221#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005222 p = skip_grep_pat(eap);
5223#else
5224 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005225#endif
5226
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005227 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
5229 if (*p == Ctrl_V)
5230 {
5231 if (eap->argt & (USECTRLV | XFILE))
5232 ++p; /* skip CTRL-V and next char */
5233 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005234 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005235 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (*p == NUL) /* stop at NUL after CTRL-V */
5237 break;
5238 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005239
5240#ifdef FEAT_EVAL
5241 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005242 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005243 {
5244 p += 2;
5245 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005246 }
5247#endif
5248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 /* Check for '"': start of comment or '|': next command */
5250 /* :@" and :*" do not start a comment!
5251 * :redir @" doesn't either. */
5252 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5253 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5254 || p != eap->arg)
5255 && (eap->cmdidx != CMD_redir
5256 || p != eap->arg + 1 || p[-1] != '@'))
5257 || *p == '|' || *p == '\n')
5258 {
5259 /*
5260 * We remove the '\' before the '|', unless USECTRLV is used
5261 * AND 'b' is present in 'cpoptions'.
5262 */
5263 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5264 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5265 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005266 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 --p;
5268 }
5269 else
5270 {
5271 eap->nextcmd = check_nextcmd(p);
5272 *p = NUL;
5273 break;
5274 }
5275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5279 del_trailing_spaces(eap->arg);
5280}
5281
5282/*
5283 * get + command from ex argument
5284 */
5285 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005286getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 char_u *arg = *argp;
5289 char_u *command = NULL;
5290
5291 if (*arg == '+') /* +[command] */
5292 {
5293 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005294 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 command = dollar_command;
5296 else
5297 {
5298 command = arg;
5299 arg = skip_cmd_arg(command, TRUE);
5300 if (*arg != NUL)
5301 *arg++ = NUL; /* terminate command with NUL */
5302 }
5303
5304 arg = skipwhite(arg); /* skip over spaces */
5305 *argp = arg;
5306 }
5307 return command;
5308}
5309
5310/*
5311 * Find end of "+command" argument. Skip over "\ " and "\\".
5312 */
5313 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005314skip_cmd_arg(
5315 char_u *p,
5316 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 while (*p && !vim_isspace(*p))
5319 {
5320 if (*p == '\\' && p[1] != NUL)
5321 {
5322 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005323 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 else
5325 ++p;
5326 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005327 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329 return p;
5330}
5331
5332/*
5333 * Get "++opt=arg" argument.
5334 * Return FAIL or OK.
5335 */
5336 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005337getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 char_u *arg = eap->arg + 2;
5340 int *pp = NULL;
5341#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005342 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 char_u *p;
5344#endif
5345
5346 /* ":edit ++[no]bin[ary] file" */
5347 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5348 {
5349 if (*arg == 'n')
5350 {
5351 arg += 2;
5352 eap->force_bin = FORCE_NOBIN;
5353 }
5354 else
5355 eap->force_bin = FORCE_BIN;
5356 if (!checkforcmd(&arg, "binary", 3))
5357 return FAIL;
5358 eap->arg = skipwhite(arg);
5359 return OK;
5360 }
5361
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005362 /* ":read ++edit file" */
5363 if (STRNCMP(arg, "edit", 4) == 0)
5364 {
5365 eap->read_edit = TRUE;
5366 eap->arg = skipwhite(arg + 4);
5367 return OK;
5368 }
5369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (STRNCMP(arg, "ff", 2) == 0)
5371 {
5372 arg += 2;
5373 pp = &eap->force_ff;
5374 }
5375 else if (STRNCMP(arg, "fileformat", 10) == 0)
5376 {
5377 arg += 10;
5378 pp = &eap->force_ff;
5379 }
5380#ifdef FEAT_MBYTE
5381 else if (STRNCMP(arg, "enc", 3) == 0)
5382 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005383 if (STRNCMP(arg, "encoding", 8) == 0)
5384 arg += 8;
5385 else
5386 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 pp = &eap->force_enc;
5388 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005389 else if (STRNCMP(arg, "bad", 3) == 0)
5390 {
5391 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005392 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#endif
5395
5396 if (pp == NULL || *arg != '=')
5397 return FAIL;
5398
5399 ++arg;
5400 *pp = (int)(arg - eap->cmd);
5401 arg = skip_cmd_arg(arg, FALSE);
5402 eap->arg = skipwhite(arg);
5403 *arg = NUL;
5404
5405#ifdef FEAT_MBYTE
5406 if (pp == &eap->force_ff)
5407 {
5408#endif
5409 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5410 return FAIL;
5411#ifdef FEAT_MBYTE
5412 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005413 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
5415 /* Make 'fileencoding' lower case. */
5416 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5417 *p = TOLOWER_ASC(*p);
5418 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005419 else
5420 {
5421 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5422 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005423 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005424 if (STRICMP(p, "keep") == 0)
5425 eap->bad_char = BAD_KEEP;
5426 else if (STRICMP(p, "drop") == 0)
5427 eap->bad_char = BAD_DROP;
5428 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5429 eap->bad_char = *p;
5430 else
5431 return FAIL;
5432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433#endif
5434
5435 return OK;
5436}
5437
5438/*
5439 * ":abbreviate" and friends.
5440 */
5441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005442ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444 do_exmap(eap, TRUE); /* almost the same as mapping */
5445}
5446
5447/*
5448 * ":map" and friends.
5449 */
5450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005451ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 /*
5454 * If we are sourcing .exrc or .vimrc in current directory we
5455 * print the mappings for security reasons.
5456 */
5457 if (secure)
5458 {
5459 secure = 2;
5460 msg_outtrans(eap->cmd);
5461 msg_putchar('\n');
5462 }
5463 do_exmap(eap, FALSE);
5464}
5465
5466/*
5467 * ":unmap" and friends.
5468 */
5469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005470ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471{
5472 do_exmap(eap, FALSE);
5473}
5474
5475/*
5476 * ":mapclear" and friends.
5477 */
5478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005479ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480{
5481 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5482}
5483
5484/*
5485 * ":abclear" and friends.
5486 */
5487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005488ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5491}
5492
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005493#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005495ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
5497 /*
5498 * Disallow auto commands from .exrc and .vimrc in current
5499 * directory for security reasons.
5500 */
5501 if (secure)
5502 {
5503 secure = 2;
5504 eap->errmsg = e_curdir;
5505 }
5506 else if (eap->cmdidx == CMD_autocmd)
5507 do_autocmd(eap->arg, eap->forceit);
5508 else
5509 do_augroup(eap->arg, eap->forceit);
5510}
5511
5512/*
5513 * ":doautocmd": Apply the automatic commands to the current buffer.
5514 */
5515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005516ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005518 char_u *arg = eap->arg;
5519 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005520 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005521
Bram Moolenaar1610d052016-06-09 22:53:01 +02005522 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5523 /* Only when there is no <nomodeline>. */
5524 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005525 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526}
5527#endif
5528
5529#ifdef FEAT_LISTCMDS
5530/*
5531 * :[N]bunload[!] [N] [bufname] unload buffer
5532 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5533 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5534 */
5535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005536ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 eap->errmsg = do_bufdel(
5539 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5540 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5541 : DOBUF_UNLOAD, eap->arg,
5542 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5543}
5544
5545/*
5546 * :[N]buffer [N] to buffer N
5547 * :[N]sbuffer [N] to buffer N
5548 */
5549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005550ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551{
5552 if (*eap->arg)
5553 eap->errmsg = e_trailing;
5554 else
5555 {
5556 if (eap->addr_count == 0) /* default is current buffer */
5557 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5558 else
5559 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005560 if (eap->do_ecmd_cmd != NULL)
5561 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563}
5564
5565/*
5566 * :[N]bmodified [N] to next mod. buffer
5567 * :[N]sbmodified [N] to next mod. buffer
5568 */
5569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005570ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571{
5572 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005573 if (eap->do_ecmd_cmd != NULL)
5574 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575}
5576
5577/*
5578 * :[N]bnext [N] to next buffer
5579 * :[N]sbnext [N] split and to next buffer
5580 */
5581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005582ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005585 if (eap->do_ecmd_cmd != NULL)
5586 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587}
5588
5589/*
5590 * :[N]bNext [N] to previous buffer
5591 * :[N]bprevious [N] to previous buffer
5592 * :[N]sbNext [N] split and to previous buffer
5593 * :[N]sbprevious [N] split and to previous buffer
5594 */
5595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005596ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005599 if (eap->do_ecmd_cmd != NULL)
5600 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601}
5602
5603/*
5604 * :brewind to first buffer
5605 * :bfirst to first buffer
5606 * :sbrewind split and to first buffer
5607 * :sbfirst split and to first buffer
5608 */
5609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
5612 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005613 if (eap->do_ecmd_cmd != NULL)
5614 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616
5617/*
5618 * :blast to last buffer
5619 * :sblast split and to last buffer
5620 */
5621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005625 if (eap->do_ecmd_cmd != NULL)
5626 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627}
5628#endif
5629
5630 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005631ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 return (c == NUL || c == '|' || c == '"' || c == '\n');
5634}
5635
5636#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5637 || defined(PROTO)
5638/*
5639 * Return the next command, after the first '|' or '\n'.
5640 * Return NULL if not found.
5641 */
5642 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005643find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
5645 while (*p != '|' && *p != '\n')
5646 {
5647 if (*p == NUL)
5648 return NULL;
5649 ++p;
5650 }
5651 return (p + 1);
5652}
5653#endif
5654
5655/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005656 * Check if *p is a separator between Ex commands, skipping over white space.
5657 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 */
5659 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005660check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005662 char_u *s = skipwhite(p);
5663
5664 if (*s == '|' || *s == '\n')
5665 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 else
5667 return NULL;
5668}
5669
5670/*
5671 * - if there are more files to edit
5672 * - and this is the last window
5673 * - and forceit not used
5674 * - and not repeated twice on a row
5675 * return FAIL and give error message if 'message' TRUE
5676 * return OK otherwise
5677 */
5678 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005679check_more(
5680 int message, /* when FALSE check only, no messages */
5681 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5684
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005685 if (!forceit && only_one_window()
5686 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 {
5688 if (message)
5689 {
5690#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5691 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5692 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005693 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
5695 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005696 vim_strncpy(buff,
5697 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005698 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005700 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 _("%d more files to edit. Quit anyway?"), n);
5702 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5703 return OK;
5704 return FAIL;
5705 }
5706#endif
5707 if (n == 1)
5708 EMSG(_("E173: 1 more file to edit"));
5709 else
5710 EMSGN(_("E173: %ld more files to edit"), n);
5711 quitmore = 2; /* next try to quit is allowed */
5712 }
5713 return FAIL;
5714 }
5715 return OK;
5716}
5717
5718#ifdef FEAT_CMDL_COMPL
5719/*
5720 * Function given to ExpandGeneric() to obtain the list of command names.
5721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005723get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 if (idx >= (int)CMD_SIZE)
5726# ifdef FEAT_USR_CMDS
5727 return get_user_command_name(idx);
5728# else
5729 return NULL;
5730# endif
5731 return cmdnames[idx].cmd_name;
5732}
5733#endif
5734
5735#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005736static 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);
5737static void uc_list(char_u *name, size_t name_len);
5738static 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);
5739static char_u *uc_split_args(char_u *arg, size_t *lenp);
5740static 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 +00005741
5742 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005743uc_add_command(
5744 char_u *name,
5745 size_t name_len,
5746 char_u *rep,
5747 long argt,
5748 long def,
5749 int flags,
5750 int compl,
5751 char_u *compl_arg,
5752 int addr_type,
5753 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
5755 ucmd_T *cmd = NULL;
5756 char_u *p;
5757 int i;
5758 int cmp = 1;
5759 char_u *rep_buf = NULL;
5760 garray_T *gap;
5761
Bram Moolenaar9c102382006-05-03 21:26:49 +00005762 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 if (rep_buf == NULL)
5764 {
5765 /* Can't replace termcodes - try using the string as is */
5766 rep_buf = vim_strsave(rep);
5767
5768 /* Give up if out of memory */
5769 if (rep_buf == NULL)
5770 return FAIL;
5771 }
5772
5773 /* get address of growarray: global or in curbuf */
5774 if (flags & UC_BUFFER)
5775 {
5776 gap = &curbuf->b_ucmds;
5777 if (gap->ga_itemsize == 0)
5778 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5779 }
5780 else
5781 gap = &ucmds;
5782
5783 /* Search for the command in the already defined commands. */
5784 for (i = 0; i < gap->ga_len; ++i)
5785 {
5786 size_t len;
5787
5788 cmd = USER_CMD_GA(gap, i);
5789 len = STRLEN(cmd->uc_name);
5790 cmp = STRNCMP(name, cmd->uc_name, name_len);
5791 if (cmp == 0)
5792 {
5793 if (name_len < len)
5794 cmp = -1;
5795 else if (name_len > len)
5796 cmp = 1;
5797 }
5798
5799 if (cmp == 0)
5800 {
5801 if (!force)
5802 {
5803 EMSG(_("E174: Command already exists: add ! to replace it"));
5804 goto fail;
5805 }
5806
Bram Moolenaard23a8232018-02-10 18:45:26 +01005807 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005808#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005809 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 break;
5812 }
5813
5814 /* Stop as soon as we pass the name to add */
5815 if (cmp < 0)
5816 break;
5817 }
5818
5819 /* Extend the array unless we're replacing an existing command */
5820 if (cmp != 0)
5821 {
5822 if (ga_grow(gap, 1) != OK)
5823 goto fail;
5824 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5825 goto fail;
5826
5827 cmd = USER_CMD_GA(gap, i);
5828 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5829
5830 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
5832 cmd->uc_name = p;
5833 }
5834
5835 cmd->uc_rep = rep_buf;
5836 cmd->uc_argt = argt;
5837 cmd->uc_def = def;
5838 cmd->uc_compl = compl;
5839#ifdef FEAT_EVAL
5840 cmd->uc_scriptID = current_SID;
5841# ifdef FEAT_CMDL_COMPL
5842 cmd->uc_compl_arg = compl_arg;
5843# endif
5844#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005845 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847 return OK;
5848
5849fail:
5850 vim_free(rep_buf);
5851#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5852 vim_free(compl_arg);
5853#endif
5854 return FAIL;
5855}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005858#if defined(FEAT_USR_CMDS)
5859static struct
5860{
5861 int expand;
5862 char *name;
5863} addr_type_complete[] =
5864{
5865 {ADDR_ARGUMENTS, "arguments"},
5866 {ADDR_LINES, "lines"},
5867 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5868 {ADDR_TABS, "tabs"},
5869 {ADDR_BUFFERS, "buffers"},
5870 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005871 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005872 {-1, NULL}
5873};
5874#endif
5875
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005876#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877/*
5878 * List of names for completion for ":command" with the EXPAND_ flag.
5879 * Must be alphabetical for completion.
5880 */
5881static struct
5882{
5883 int expand;
5884 char *name;
5885} command_complete[] =
5886{
5887 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005888 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005890 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005892 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005893#if defined(FEAT_CSCOPE)
5894 {EXPAND_CSCOPE, "cscope"},
5895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5897 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005898 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#endif
5900 {EXPAND_DIRECTORIES, "dir"},
5901 {EXPAND_ENV_VARS, "environment"},
5902 {EXPAND_EVENTS, "event"},
5903 {EXPAND_EXPRESSION, "expression"},
5904 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005905 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005906 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 {EXPAND_FUNCTIONS, "function"},
5908 {EXPAND_HELP, "help"},
5909 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005910#if defined(FEAT_CMDHIST)
5911 {EXPAND_HISTORY, "history"},
5912#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005913#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005914 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005915 {EXPAND_LOCALES, "locale"},
5916#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005917 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {EXPAND_MAPPINGS, "mapping"},
5919 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005920 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005921 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005922#if defined(FEAT_PROFILE)
5923 {EXPAND_SYNTIME, "syntime"},
5924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005926 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005927 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005928#if defined(FEAT_SIGNS)
5929 {EXPAND_SIGN, "sign"},
5930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {EXPAND_TAGS, "tag"},
5932 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005933 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {EXPAND_USER_VARS, "var"},
5935 {0, NULL}
5936};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005939#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005941uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
5943 int i, j;
5944 int found = FALSE;
5945 ucmd_T *cmd;
5946 int len;
5947 long a;
5948 garray_T *gap;
5949
5950 gap = &curbuf->b_ucmds;
5951 for (;;)
5952 {
5953 for (i = 0; i < gap->ga_len; ++i)
5954 {
5955 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005956 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
Bram Moolenaard29459b2016-08-26 22:29:11 +02005958 /* Skip commands which don't match the requested prefix and
5959 * commands filtered out. */
5960 if (STRNCMP(name, cmd->uc_name, name_len) != 0
5961 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 continue;
5963
5964 /* Put out the title first time */
5965 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005966 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 found = TRUE;
5968 msg_putchar('\n');
5969 if (got_int)
5970 break;
5971
5972 /* Special cases */
5973 msg_putchar(a & BANG ? '!' : ' ');
5974 msg_putchar(a & REGSTR ? '"' : ' ');
5975 msg_putchar(gap != &ucmds ? 'b' : ' ');
5976 msg_putchar(' ');
5977
Bram Moolenaar8820b482017-03-16 17:23:31 +01005978 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 len = (int)STRLEN(cmd->uc_name) + 4;
5980
5981 do {
5982 msg_putchar(' ');
5983 ++len;
5984 } while (len < 16);
5985
5986 len = 0;
5987
5988 /* Arguments */
5989 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5990 {
5991 case 0: IObuff[len++] = '0'; break;
5992 case (EXTRA): IObuff[len++] = '*'; break;
5993 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5994 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5995 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5996 }
5997
5998 do {
5999 IObuff[len++] = ' ';
6000 } while (len < 5);
6001
6002 /* Range */
6003 if (a & (RANGE|COUNT))
6004 {
6005 if (a & COUNT)
6006 {
6007 /* -count=N */
6008 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6009 len += (int)STRLEN(IObuff + len);
6010 }
6011 else if (a & DFLALL)
6012 IObuff[len++] = '%';
6013 else if (cmd->uc_def >= 0)
6014 {
6015 /* -range=N */
6016 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6017 len += (int)STRLEN(IObuff + len);
6018 }
6019 else
6020 IObuff[len++] = '.';
6021 }
6022
6023 do {
6024 IObuff[len++] = ' ';
6025 } while (len < 11);
6026
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006027 /* Address Type */
6028 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6029 if (addr_type_complete[j].expand != ADDR_LINES
6030 && addr_type_complete[j].expand == cmd->uc_addr_type)
6031 {
6032 STRCPY(IObuff + len, addr_type_complete[j].name);
6033 len += (int)STRLEN(IObuff + len);
6034 break;
6035 }
6036
6037 do {
6038 IObuff[len++] = ' ';
6039 } while (len < 21);
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 /* Completion */
6042 for (j = 0; command_complete[j].expand != 0; ++j)
6043 if (command_complete[j].expand == cmd->uc_compl)
6044 {
6045 STRCPY(IObuff + len, command_complete[j].name);
6046 len += (int)STRLEN(IObuff + len);
6047 break;
6048 }
6049
6050 do {
6051 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006052 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
6054 IObuff[len] = '\0';
6055 msg_outtrans(IObuff);
6056
6057 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006058#ifdef FEAT_EVAL
6059 if (p_verbose > 0)
6060 last_set_msg(cmd->uc_scriptID);
6061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 out_flush();
6063 ui_breakcheck();
6064 if (got_int)
6065 break;
6066 }
6067 if (gap == &ucmds || i < gap->ga_len)
6068 break;
6069 gap = &ucmds;
6070 }
6071
6072 if (!found)
6073 MSG(_("No user-defined commands found"));
6074}
6075
6076 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006077uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
6079 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6080 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6081 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6082 0xb9, 0x7f, 0};
6083 int i;
6084
6085 for (i = 0; fcmd[i]; ++i)
6086 IObuff[i] = fcmd[i] - 0x40;
6087 IObuff[i] = 0;
6088 return IObuff;
6089}
6090
6091 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006092uc_scan_attr(
6093 char_u *attr,
6094 size_t len,
6095 long *argt,
6096 long *def,
6097 int *flags,
6098 int *compl,
6099 char_u **compl_arg,
6100 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
6102 char_u *p;
6103
6104 if (len == 0)
6105 {
6106 EMSG(_("E175: No attribute specified"));
6107 return FAIL;
6108 }
6109
6110 /* First, try the simple attributes (no arguments) */
6111 if (STRNICMP(attr, "bang", len) == 0)
6112 *argt |= BANG;
6113 else if (STRNICMP(attr, "buffer", len) == 0)
6114 *flags |= UC_BUFFER;
6115 else if (STRNICMP(attr, "register", len) == 0)
6116 *argt |= REGSTR;
6117 else if (STRNICMP(attr, "bar", len) == 0)
6118 *argt |= TRLBAR;
6119 else
6120 {
6121 int i;
6122 char_u *val = NULL;
6123 size_t vallen = 0;
6124 size_t attrlen = len;
6125
6126 /* Look for the attribute name - which is the part before any '=' */
6127 for (i = 0; i < (int)len; ++i)
6128 {
6129 if (attr[i] == '=')
6130 {
6131 val = &attr[i + 1];
6132 vallen = len - i - 1;
6133 attrlen = i;
6134 break;
6135 }
6136 }
6137
6138 if (STRNICMP(attr, "nargs", attrlen) == 0)
6139 {
6140 if (vallen == 1)
6141 {
6142 if (*val == '0')
6143 /* Do nothing - this is the default */;
6144 else if (*val == '1')
6145 *argt |= (EXTRA | NOSPC | NEEDARG);
6146 else if (*val == '*')
6147 *argt |= EXTRA;
6148 else if (*val == '?')
6149 *argt |= (EXTRA | NOSPC);
6150 else if (*val == '+')
6151 *argt |= (EXTRA | NEEDARG);
6152 else
6153 goto wrong_nargs;
6154 }
6155 else
6156 {
6157wrong_nargs:
6158 EMSG(_("E176: Invalid number of arguments"));
6159 return FAIL;
6160 }
6161 }
6162 else if (STRNICMP(attr, "range", attrlen) == 0)
6163 {
6164 *argt |= RANGE;
6165 if (vallen == 1 && *val == '%')
6166 *argt |= DFLALL;
6167 else if (val != NULL)
6168 {
6169 p = val;
6170 if (*def >= 0)
6171 {
6172two_count:
6173 EMSG(_("E177: Count cannot be specified twice"));
6174 return FAIL;
6175 }
6176
6177 *def = getdigits(&p);
6178 *argt |= (ZEROR | NOTADR);
6179
6180 if (p != val + vallen || vallen == 0)
6181 {
6182invalid_count:
6183 EMSG(_("E178: Invalid default value for count"));
6184 return FAIL;
6185 }
6186 }
6187 }
6188 else if (STRNICMP(attr, "count", attrlen) == 0)
6189 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006190 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191
6192 if (val != NULL)
6193 {
6194 p = val;
6195 if (*def >= 0)
6196 goto two_count;
6197
6198 *def = getdigits(&p);
6199
6200 if (p != val + vallen)
6201 goto invalid_count;
6202 }
6203
6204 if (*def < 0)
6205 *def = 0;
6206 }
6207 else if (STRNICMP(attr, "complete", attrlen) == 0)
6208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (val == NULL)
6210 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006211 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 return FAIL;
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006215 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6216 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006219 else if (STRNICMP(attr, "addr", attrlen) == 0)
6220 {
6221 *argt |= RANGE;
6222 if (val == NULL)
6223 {
6224 EMSG(_("E179: argument required for -addr"));
6225 return FAIL;
6226 }
6227 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6228 == FAIL)
6229 return FAIL;
6230 if (addr_type_arg != ADDR_LINES)
6231 *argt |= (ZEROR | NOTADR) ;
6232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 else
6234 {
6235 char_u ch = attr[len];
6236 attr[len] = '\0';
6237 EMSG2(_("E181: Invalid attribute: %s"), attr);
6238 attr[len] = ch;
6239 return FAIL;
6240 }
6241 }
6242
6243 return OK;
6244}
6245
Bram Moolenaara850a712009-01-28 14:42:59 +00006246/*
6247 * ":command ..."
6248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 char_u *name;
6253 char_u *end;
6254 char_u *p;
6255 long argt = 0;
6256 long def = -1;
6257 int flags = 0;
6258 int compl = EXPAND_NOTHING;
6259 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006260 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006262 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
6264 p = eap->arg;
6265
6266 /* Check for attributes */
6267 while (*p == '-')
6268 {
6269 ++p;
6270 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006271 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6272 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 == FAIL)
6274 return;
6275 p = skipwhite(end);
6276 }
6277
6278 /* Get the name (if any) and skip to the following argument */
6279 name = p;
6280 if (ASCII_ISALPHA(*p))
6281 while (ASCII_ISALNUM(*p))
6282 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006283 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
6285 EMSG(_("E182: Invalid command name"));
6286 return;
6287 }
6288 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006289 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
6291 /* If there is nothing after the name, and no attributes were specified,
6292 * we are listing commands
6293 */
6294 p = skipwhite(end);
6295 if (!has_attr && ends_excmd(*p))
6296 {
6297 uc_list(name, end - name);
6298 }
6299 else if (!ASCII_ISUPPER(*name))
6300 {
6301 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6302 return;
6303 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006304 else if ((name_len == 1 && *name == 'X')
6305 || (name_len <= 4
6306 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6307 {
6308 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6309 return;
6310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 else
6312 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006313 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314}
6315
6316/*
6317 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 * Clear all user commands, global and for current buffer.
6319 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006320 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006321ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 uc_clear(&ucmds);
6324 uc_clear(&curbuf->b_ucmds);
6325}
6326
6327/*
6328 * Clear all user commands for "gap".
6329 */
6330 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006331uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 int i;
6334 ucmd_T *cmd;
6335
6336 for (i = 0; i < gap->ga_len; ++i)
6337 {
6338 cmd = USER_CMD_GA(gap, i);
6339 vim_free(cmd->uc_name);
6340 vim_free(cmd->uc_rep);
6341# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6342 vim_free(cmd->uc_compl_arg);
6343# endif
6344 }
6345 ga_clear(gap);
6346}
6347
6348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006349ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 int i = 0;
6352 ucmd_T *cmd = NULL;
6353 int cmp = -1;
6354 garray_T *gap;
6355
6356 gap = &curbuf->b_ucmds;
6357 for (;;)
6358 {
6359 for (i = 0; i < gap->ga_len; ++i)
6360 {
6361 cmd = USER_CMD_GA(gap, i);
6362 cmp = STRCMP(eap->arg, cmd->uc_name);
6363 if (cmp <= 0)
6364 break;
6365 }
6366 if (gap == &ucmds || cmp == 0)
6367 break;
6368 gap = &ucmds;
6369 }
6370
6371 if (cmp != 0)
6372 {
6373 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6374 return;
6375 }
6376
6377 vim_free(cmd->uc_name);
6378 vim_free(cmd->uc_rep);
6379# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6380 vim_free(cmd->uc_compl_arg);
6381# endif
6382
6383 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384
6385 if (i < gap->ga_len)
6386 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6387}
6388
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006389/*
6390 * split and quote args for <f-args>
6391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006393uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 char_u *buf;
6396 char_u *p;
6397 char_u *q;
6398 int len;
6399
6400 /* Precalculate length */
6401 p = arg;
6402 len = 2; /* Initial and final quotes */
6403
6404 while (*p)
6405 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006406 if (p[0] == '\\' && p[1] == '\\')
6407 {
6408 len += 2;
6409 p += 2;
6410 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006411 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 {
6413 len += 1;
6414 p += 2;
6415 }
6416 else if (*p == '\\' || *p == '"')
6417 {
6418 len += 2;
6419 p += 1;
6420 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006421 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
6423 p = skipwhite(p);
6424 if (*p == NUL)
6425 break;
6426 len += 3; /* "," */
6427 }
6428 else
6429 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006430#ifdef FEAT_MBYTE
6431 int charlen = (*mb_ptr2len)(p);
6432 len += charlen;
6433 p += charlen;
6434#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 ++len;
6436 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 }
6439 }
6440
6441 buf = alloc(len + 1);
6442 if (buf == NULL)
6443 {
6444 *lenp = 0;
6445 return buf;
6446 }
6447
6448 p = arg;
6449 q = buf;
6450 *q++ = '"';
6451 while (*p)
6452 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006453 if (p[0] == '\\' && p[1] == '\\')
6454 {
6455 *q++ = '\\';
6456 *q++ = '\\';
6457 p += 2;
6458 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006459 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 {
6461 *q++ = p[1];
6462 p += 2;
6463 }
6464 else if (*p == '\\' || *p == '"')
6465 {
6466 *q++ = '\\';
6467 *q++ = *p++;
6468 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006469 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
6471 p = skipwhite(p);
6472 if (*p == NUL)
6473 break;
6474 *q++ = '"';
6475 *q++ = ',';
6476 *q++ = '"';
6477 }
6478 else
6479 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006480 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 }
6482 }
6483 *q++ = '"';
6484 *q = 0;
6485
6486 *lenp = len;
6487 return buf;
6488}
6489
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006490 static size_t
6491add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6492{
6493 size_t result;
6494
6495 result = STRLEN(mod_str);
6496 if (*multi_mods)
6497 result += 1;
6498 if (buf != NULL)
6499 {
6500 if (*multi_mods)
6501 STRCAT(buf, " ");
6502 STRCAT(buf, mod_str);
6503 }
6504
6505 *multi_mods = 1;
6506
6507 return result;
6508}
6509
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510/*
6511 * Check for a <> code in a user command.
6512 * "code" points to the '<'. "len" the length of the <> (inclusive).
6513 * "buf" is where the result is to be added.
6514 * "split_buf" points to a buffer used for splitting, caller should free it.
6515 * "split_len" is the length of what "split_buf" contains.
6516 * Returns the length of the replacement, which has been added to "buf".
6517 * Returns -1 if there was no match, and only the "<" has been copied.
6518 */
6519 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006520uc_check_code(
6521 char_u *code,
6522 size_t len,
6523 char_u *buf,
6524 ucmd_T *cmd, /* the user command we're expanding */
6525 exarg_T *eap, /* ex arguments */
6526 char_u **split_buf,
6527 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 size_t result = 0;
6530 char_u *p = code + 1;
6531 size_t l = len - 2;
6532 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006533 enum {
6534 ct_ARGS,
6535 ct_BANG,
6536 ct_COUNT,
6537 ct_LINE1,
6538 ct_LINE2,
6539 ct_RANGE,
6540 ct_MODS,
6541 ct_REGISTER,
6542 ct_LT,
6543 ct_NONE
6544 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545
6546 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6547 {
6548 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6549 p += 2;
6550 l -= 2;
6551 }
6552
Bram Moolenaar371d5402006-03-20 21:47:49 +00006553 ++l;
6554 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006556 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006558 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006560 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006562 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006564 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006566 else if (STRNICMP(p, "range>", l) == 0)
6567 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006568 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006570 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006572 else if (STRNICMP(p, "mods>", l) == 0)
6573 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
6575 switch (type)
6576 {
6577 case ct_ARGS:
6578 /* Simple case first */
6579 if (*eap->arg == NUL)
6580 {
6581 if (quote == 1)
6582 {
6583 result = 2;
6584 if (buf != NULL)
6585 STRCPY(buf, "''");
6586 }
6587 else
6588 result = 0;
6589 break;
6590 }
6591
6592 /* When specified there is a single argument don't split it.
6593 * Works for ":Cmd %" when % is "a b c". */
6594 if ((eap->argt & NOSPC) && quote == 2)
6595 quote = 1;
6596
6597 switch (quote)
6598 {
6599 case 0: /* No quoting, no splitting */
6600 result = STRLEN(eap->arg);
6601 if (buf != NULL)
6602 STRCPY(buf, eap->arg);
6603 break;
6604 case 1: /* Quote, but don't split */
6605 result = STRLEN(eap->arg) + 2;
6606 for (p = eap->arg; *p; ++p)
6607 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006608#ifdef FEAT_MBYTE
6609 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6610 /* DBCS can contain \ in a trail byte, skip the
6611 * double-byte character. */
6612 ++p;
6613 else
6614#endif
6615 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 ++result;
6617 }
6618
6619 if (buf != NULL)
6620 {
6621 *buf++ = '"';
6622 for (p = eap->arg; *p; ++p)
6623 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006624#ifdef FEAT_MBYTE
6625 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6626 /* DBCS can contain \ in a trail byte, copy the
6627 * double-byte character to avoid escaping. */
6628 *buf++ = *p++;
6629 else
6630#endif
6631 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 *buf++ = '\\';
6633 *buf++ = *p;
6634 }
6635 *buf = '"';
6636 }
6637
6638 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006639 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 /* This is hard, so only do it once, and cache the result */
6641 if (*split_buf == NULL)
6642 *split_buf = uc_split_args(eap->arg, split_len);
6643
6644 result = *split_len;
6645 if (buf != NULL && result != 0)
6646 STRCPY(buf, *split_buf);
6647
6648 break;
6649 }
6650 break;
6651
6652 case ct_BANG:
6653 result = eap->forceit ? 1 : 0;
6654 if (quote)
6655 result += 2;
6656 if (buf != NULL)
6657 {
6658 if (quote)
6659 *buf++ = '"';
6660 if (eap->forceit)
6661 *buf++ = '!';
6662 if (quote)
6663 *buf = '"';
6664 }
6665 break;
6666
6667 case ct_LINE1:
6668 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006669 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 case ct_COUNT:
6671 {
6672 char num_buf[20];
6673 long num = (type == ct_LINE1) ? eap->line1 :
6674 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006675 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6677 size_t num_len;
6678
6679 sprintf(num_buf, "%ld", num);
6680 num_len = STRLEN(num_buf);
6681 result = num_len;
6682
6683 if (quote)
6684 result += 2;
6685
6686 if (buf != NULL)
6687 {
6688 if (quote)
6689 *buf++ = '"';
6690 STRCPY(buf, num_buf);
6691 buf += num_len;
6692 if (quote)
6693 *buf = '"';
6694 }
6695
6696 break;
6697 }
6698
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006699 case ct_MODS:
6700 {
6701 int multi_mods = 0;
6702 typedef struct {
6703 int *varp;
6704 char *name;
6705 } mod_entry_T;
6706 static mod_entry_T mod_entries[] = {
6707#ifdef FEAT_BROWSE_CMD
6708 {&cmdmod.browse, "browse"},
6709#endif
6710#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6711 {&cmdmod.confirm, "confirm"},
6712#endif
6713 {&cmdmod.hide, "hide"},
6714 {&cmdmod.keepalt, "keepalt"},
6715 {&cmdmod.keepjumps, "keepjumps"},
6716 {&cmdmod.keepmarks, "keepmarks"},
6717 {&cmdmod.keeppatterns, "keeppatterns"},
6718 {&cmdmod.lockmarks, "lockmarks"},
6719 {&cmdmod.noswapfile, "noswapfile"},
6720 {NULL, NULL}
6721 };
6722 int i;
6723
6724 result = quote ? 2 : 0;
6725 if (buf != NULL)
6726 {
6727 if (quote)
6728 *buf++ = '"';
6729 *buf = '\0';
6730 }
6731
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006732 /* :aboveleft and :leftabove */
6733 if (cmdmod.split & WSP_ABOVE)
6734 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6735 /* :belowright and :rightbelow */
6736 if (cmdmod.split & WSP_BELOW)
6737 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6738 /* :botright */
6739 if (cmdmod.split & WSP_BOT)
6740 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006741
6742 /* the modifiers that are simple flags */
6743 for (i = 0; mod_entries[i].varp != NULL; ++i)
6744 if (*mod_entries[i].varp)
6745 result += add_cmd_modifier(buf, mod_entries[i].name,
6746 &multi_mods);
6747
6748 /* TODO: How to support :noautocmd? */
6749#ifdef HAVE_SANDBOX
6750 /* TODO: How to support :sandbox?*/
6751#endif
6752 /* :silent */
6753 if (msg_silent > 0)
6754 result += add_cmd_modifier(buf,
6755 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006756 /* :tab */
6757 if (cmdmod.tab > 0)
6758 result += add_cmd_modifier(buf, "tab", &multi_mods);
6759 /* :topleft */
6760 if (cmdmod.split & WSP_TOP)
6761 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006762 /* TODO: How to support :unsilent?*/
6763 /* :verbose */
6764 if (p_verbose > 0)
6765 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006766 /* :vertical */
6767 if (cmdmod.split & WSP_VERT)
6768 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006769 if (quote && buf != NULL)
6770 {
6771 buf += result - 2;
6772 *buf = '"';
6773 }
6774 break;
6775 }
6776
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 case ct_REGISTER:
6778 result = eap->regname ? 1 : 0;
6779 if (quote)
6780 result += 2;
6781 if (buf != NULL)
6782 {
6783 if (quote)
6784 *buf++ = '\'';
6785 if (eap->regname)
6786 *buf++ = eap->regname;
6787 if (quote)
6788 *buf = '\'';
6789 }
6790 break;
6791
6792 case ct_LT:
6793 result = 1;
6794 if (buf != NULL)
6795 *buf = '<';
6796 break;
6797
6798 default:
6799 /* Not recognized: just copy the '<' and return -1. */
6800 result = (size_t)-1;
6801 if (buf != NULL)
6802 *buf = '<';
6803 break;
6804 }
6805
6806 return result;
6807}
6808
6809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006810do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
6812 char_u *buf;
6813 char_u *p;
6814 char_u *q;
6815
6816 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006817 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006818 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 size_t len, totlen;
6820
6821 size_t split_len = 0;
6822 char_u *split_buf = NULL;
6823 ucmd_T *cmd;
6824#ifdef FEAT_EVAL
6825 scid_T save_current_SID = current_SID;
6826#endif
6827
6828 if (eap->cmdidx == CMD_USER)
6829 cmd = USER_CMD(eap->useridx);
6830 else
6831 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6832
6833 /*
6834 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006835 * First round: "buf" is NULL, compute length, allocate "buf".
6836 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 */
6838 buf = NULL;
6839 for (;;)
6840 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006841 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006842 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006844
6845 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006847 start = vim_strchr(p, '<');
6848 if (start != NULL)
6849 end = vim_strchr(start + 1, '>');
6850 if (buf != NULL)
6851 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006852 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6853 ;
6854 if (*ksp == K_SPECIAL
6855 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006856 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6857# ifdef FEAT_GUI
6858 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6859# endif
6860 ))
6861 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006862 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006863 * KS_SPECIAL KE_FILLER, like for mappings, but
6864 * do_cmdline() doesn't handle that, so convert it back.
6865 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6866 len = ksp - p;
6867 if (len > 0)
6868 {
6869 mch_memmove(q, p, len);
6870 q += len;
6871 }
6872 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6873 p = ksp + 3;
6874 continue;
6875 }
6876 }
6877
6878 /* break if there no <item> is found */
6879 if (start == NULL || end == NULL)
6880 break;
6881
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 /* Include the '>' */
6883 ++end;
6884
6885 /* Take everything up to the '<' */
6886 len = start - p;
6887 if (buf == NULL)
6888 totlen += len;
6889 else
6890 {
6891 mch_memmove(q, p, len);
6892 q += len;
6893 }
6894
6895 len = uc_check_code(start, end - start, q, cmd, eap,
6896 &split_buf, &split_len);
6897 if (len == (size_t)-1)
6898 {
6899 /* no match, continue after '<' */
6900 p = start + 1;
6901 len = 1;
6902 }
6903 else
6904 p = end;
6905 if (buf == NULL)
6906 totlen += len;
6907 else
6908 q += len;
6909 }
6910 if (buf != NULL) /* second time here, finished */
6911 {
6912 STRCPY(q, p);
6913 break;
6914 }
6915
6916 totlen += STRLEN(p); /* Add on the trailing characters */
6917 buf = alloc((unsigned)(totlen + 1));
6918 if (buf == NULL)
6919 {
6920 vim_free(split_buf);
6921 return;
6922 }
6923 }
6924
6925#ifdef FEAT_EVAL
6926 current_SID = cmd->uc_scriptID;
6927#endif
6928 (void)do_cmdline(buf, eap->getline, eap->cookie,
6929 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6930#ifdef FEAT_EVAL
6931 current_SID = save_current_SID;
6932#endif
6933 vim_free(buf);
6934 vim_free(split_buf);
6935}
6936
6937# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6938 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006939get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
6941 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6942}
6943
6944/*
6945 * Function given to ExpandGeneric() to obtain the list of user command names.
6946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006948get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 if (idx < curbuf->b_ucmds.ga_len)
6951 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6952 idx -= curbuf->b_ucmds.ga_len;
6953 if (idx < ucmds.ga_len)
6954 return USER_CMD(idx)->uc_name;
6955 return NULL;
6956}
6957
6958/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006959 * Function given to ExpandGeneric() to obtain the list of user address type names.
6960 */
6961 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006962get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006963{
6964 return (char_u *)addr_type_complete[idx].name;
6965}
6966
6967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 * Function given to ExpandGeneric() to obtain the list of user command
6969 * attributes.
6970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006972get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006975 {"addr", "bang", "bar", "buffer", "complete",
6976 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006978 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 return NULL;
6980 return (char_u *)user_cmd_flags[idx];
6981}
6982
6983/*
6984 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006987get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6990
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006991 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 return NULL;
6993 return (char_u *)user_cmd_nargs[idx];
6994}
6995
6996/*
6997 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007000get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
7002 return (char_u *)command_complete[idx].name;
7003}
7004# endif /* FEAT_CMDL_COMPL */
7005
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007006/*
7007 * Parse address type argument
7008 */
7009 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007010parse_addr_type_arg(
7011 char_u *value,
7012 int vallen,
7013 long *argt,
7014 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007015{
7016 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007017
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007018 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7019 {
7020 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7021 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7022 if (a && b)
7023 {
7024 *addr_type_arg = addr_type_complete[i].expand;
7025 break;
7026 }
7027 }
7028
7029 if (addr_type_complete[i].expand == -1)
7030 {
7031 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007032
Bram Moolenaar1c465442017-03-12 20:10:05 +01007033 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007034 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007035 err[i] = NUL;
7036 EMSG2(_("E180: Invalid address type value: %s"), err);
7037 return FAIL;
7038 }
7039
7040 if (*addr_type_arg != ADDR_LINES)
7041 *argt |= NOTADR;
7042
7043 return OK;
7044}
7045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046#endif /* FEAT_USR_CMDS */
7047
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007048#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7049/*
7050 * Parse a completion argument "value[vallen]".
7051 * The detected completion goes in "*complp", argument type in "*argt".
7052 * When there is an argument, for function and user defined completion, it's
7053 * copied to allocated memory and stored in "*compl_arg".
7054 * Returns FAIL if something is wrong.
7055 */
7056 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007057parse_compl_arg(
7058 char_u *value,
7059 int vallen,
7060 int *complp,
7061 long *argt,
7062 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007063{
7064 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007065# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007066 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007067# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007068 int i;
7069 int valend = vallen;
7070
7071 /* Look for any argument part - which is the part after any ',' */
7072 for (i = 0; i < vallen; ++i)
7073 {
7074 if (value[i] == ',')
7075 {
7076 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007077# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007078 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007079# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007080 valend = i;
7081 break;
7082 }
7083 }
7084
7085 for (i = 0; command_complete[i].expand != 0; ++i)
7086 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007087 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007088 && STRNCMP(value, command_complete[i].name, valend) == 0)
7089 {
7090 *complp = command_complete[i].expand;
7091 if (command_complete[i].expand == EXPAND_BUFFERS)
7092 *argt |= BUFNAME;
7093 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7094 || command_complete[i].expand == EXPAND_FILES)
7095 *argt |= XFILE;
7096 break;
7097 }
7098 }
7099
7100 if (command_complete[i].expand == 0)
7101 {
7102 EMSG2(_("E180: Invalid complete value: %s"), value);
7103 return FAIL;
7104 }
7105
7106# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7107 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7108 && arg != NULL)
7109# else
7110 if (arg != NULL)
7111# endif
7112 {
7113 EMSG(_("E468: Completion argument only allowed for custom completion"));
7114 return FAIL;
7115 }
7116
7117# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7118 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7119 && arg == NULL)
7120 {
7121 EMSG(_("E467: Custom completion requires a function argument"));
7122 return FAIL;
7123 }
7124
7125 if (arg != NULL)
7126 *compl_arg = vim_strnsave(arg, (int)arglen);
7127# endif
7128 return OK;
7129}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007130
7131 int
7132cmdcomplete_str_to_type(char_u *complete_str)
7133{
7134 int i;
7135
7136 for (i = 0; command_complete[i].expand != 0; ++i)
7137 if (STRCMP(complete_str, command_complete[i].name) == 0)
7138 return command_complete[i].expand;
7139
7140 return EXPAND_NOTHING;
7141}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007142#endif
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007145ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
Bram Moolenaare6850792010-05-14 15:28:44 +02007147 if (*eap->arg == NUL)
7148 {
7149#ifdef FEAT_EVAL
7150 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7151 char_u *p = NULL;
7152
7153 if (expr != NULL)
7154 {
7155 ++emsg_off;
7156 p = eval_to_string(expr, NULL, FALSE);
7157 --emsg_off;
7158 vim_free(expr);
7159 }
7160 if (p != NULL)
7161 {
7162 MSG(p);
7163 vim_free(p);
7164 }
7165 else
7166 MSG("default");
7167#else
7168 MSG(_("unknown"));
7169#endif
7170 }
7171 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007172 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173}
7174
7175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007176ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
7178 if (*eap->arg == NUL && eap->cmd[2] == '!')
7179 MSG(_("Greetings, Vim user!"));
7180 do_highlight(eap->arg, eap->forceit, FALSE);
7181}
7182
7183
7184/*
7185 * Call this function if we thought we were going to exit, but we won't
7186 * (because of an error). May need to restore the terminal mode.
7187 */
7188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007189not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 exiting = FALSE;
7192 settmode(TMODE_RAW);
7193}
7194
7195/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007196 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 */
7198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007199ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007201 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007202
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203#ifdef FEAT_CMDWIN
7204 if (cmdwin_type != 0)
7205 {
7206 cmdwin_result = Ctrl_C;
7207 return;
7208 }
7209#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007210 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007211 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007212 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007213 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007214 return;
7215 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007216 if (eap->addr_count > 0)
7217 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007218 int wnr = eap->line2;
7219
7220 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7221 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007222 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007223 }
7224 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007225 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007226
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007227#ifdef FEAT_AUTOCMD
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007228 /* Refuse to quit when locked. */
7229 if (curbuf_locked())
7230 return;
7231 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7232 /* Bail out when autocommands closed the window.
7233 * Refuse to quit when the buffer in the last window is being closed (can
7234 * only happen in autocommands). */
7235 if (!win_valid(wp)
Bram Moolenaar0ea50702017-07-08 14:44:50 +02007236 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007237 return;
7238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240#ifdef FEAT_NETBEANS_INTG
7241 netbeansForcedQuit = eap->forceit;
7242#endif
7243
7244 /*
7245 * If there are more files or windows we won't exit.
7246 */
7247 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7248 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007249 if ((!buf_hide(wp->w_buffer)
7250 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007251 | (eap->forceit ? CCGD_FORCEIT : 0)
7252 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007254 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
7256 not_exiting();
7257 }
7258 else
7259 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007260 /* quit last window
7261 * Note: only_one_window() returns true, even so a help window is
7262 * still open. In that case only quit, if no address has been
7263 * specified. Example:
7264 * :h|wincmd w|1q - don't quit
7265 * :h|wincmd w|q - quit
7266 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007267 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007269 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007270#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007274 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 }
7276}
7277
7278/*
7279 * ":cquit".
7280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007282ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
7284 getout(1); /* this does not always pass on the exit code to the Manx
7285 compiler. why? */
7286}
7287
7288/*
7289 * ":qall": try to quit all windows
7290 */
7291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007292ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294# ifdef FEAT_CMDWIN
7295 if (cmdwin_type != 0)
7296 {
7297 if (eap->forceit)
7298 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7299 else
7300 cmdwin_result = K_XF2;
7301 return;
7302 }
7303# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007304
7305 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007306 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007307 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007308 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007309 return;
7310 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007311#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007312 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7313 /* Refuse to quit when locked or when the buffer in the last window is
7314 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007315 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007316 return;
7317#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007318
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007320 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 getout(0);
7322 not_exiting();
7323}
7324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325/*
7326 * ":close": close current window, unless it is the last one
7327 */
7328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007329ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007331 win_T *win;
7332 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007333#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007335 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007337#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007338 if (!text_locked()
7339#ifdef FEAT_AUTOCMD
7340 && !curbuf_locked()
7341#endif
7342 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007343 {
7344 if (eap->addr_count == 0)
7345 ex_win_close(eap->forceit, curwin, NULL);
7346 else {
Bram Moolenaar29323592016-07-24 22:04:11 +02007347 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007348 {
7349 winnr++;
7350 if (winnr == eap->line2)
7351 break;
7352 }
7353 if (win == NULL)
7354 win = lastwin;
7355 ex_win_close(eap->forceit, win, NULL);
7356 }
7357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358}
7359
Bram Moolenaar4033c552017-09-16 20:54:51 +02007360#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007361/*
7362 * ":pclose": Close any preview window.
7363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007365ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007366{
7367 win_T *win;
7368
Bram Moolenaar29323592016-07-24 22:04:11 +02007369 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007370 if (win->w_p_pvw)
7371 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007372 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007373 break;
7374 }
7375}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007376#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007377
Bram Moolenaarf740b292006-02-16 22:11:02 +00007378/*
7379 * Close window "win" and take care of handling closing the last window for a
7380 * modified buffer.
7381 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007383ex_win_close(
7384 int forceit,
7385 win_T *win,
7386 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
7388 int need_hide;
7389 buf_T *buf = win->w_buffer;
7390
7391 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007392 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007394#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 if ((p_confirm || cmdmod.confirm) && p_write)
7396 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007397 bufref_T bufref;
7398
7399 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007401 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 return;
7403 need_hide = FALSE;
7404 }
7405 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007408 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 return;
7410 }
7411 }
7412
Bram Moolenaar4033c552017-09-16 20:54:51 +02007413#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007415#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007418 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007419 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007420 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007421 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422}
7423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007425 * Handle the argument for a tabpage related ex command.
7426 * Returns a tabpage number.
7427 * When an error is encountered then eap->errmsg is set.
7428 */
7429 static int
7430get_tabpage_arg(exarg_T *eap)
7431{
7432 int tab_number;
7433 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7434
7435 if (eap->arg && *eap->arg != NUL)
7436 {
7437 char_u *p = eap->arg;
7438 char_u *p_save;
7439 int relative = 0; /* argument +N/-N means: go to N places to the
7440 * right/left relative to the current position. */
7441
7442 if (*p == '-')
7443 {
7444 relative = -1;
7445 p++;
7446 }
7447 else if (*p == '+')
7448 {
7449 relative = 1;
7450 p++;
7451 }
7452
7453 p_save = p;
7454 tab_number = getdigits(&p);
7455
7456 if (relative == 0)
7457 {
7458 if (STRCMP(p, "$") == 0)
7459 tab_number = LAST_TAB_NR;
7460 else if (p == p_save || *p_save == '-' || *p != NUL
7461 || tab_number > LAST_TAB_NR)
7462 {
7463 /* No numbers as argument. */
7464 eap->errmsg = e_invarg;
7465 goto theend;
7466 }
7467 }
7468 else
7469 {
7470 if (*p_save == NUL)
7471 tab_number = 1;
7472 else if (p == p_save || *p_save == '-' || *p != NUL
7473 || tab_number == 0)
7474 {
7475 /* No numbers as argument. */
7476 eap->errmsg = e_invarg;
7477 goto theend;
7478 }
7479 tab_number = tab_number * relative + tabpage_index(curtab);
7480 if (!unaccept_arg0 && relative == -1)
7481 --tab_number;
7482 }
7483 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7484 eap->errmsg = e_invarg;
7485 }
7486 else if (eap->addr_count > 0)
7487 {
7488 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007489 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007490 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007491 tab_number = 0;
7492 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007493 else
7494 {
7495 tab_number = eap->line2;
7496 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7497 {
7498 --tab_number;
7499 if (tab_number < unaccept_arg0)
7500 eap->errmsg = e_invarg;
7501 }
7502 }
7503 }
7504 else
7505 {
7506 switch (eap->cmdidx)
7507 {
7508 case CMD_tabnext:
7509 tab_number = tabpage_index(curtab) + 1;
7510 if (tab_number > LAST_TAB_NR)
7511 tab_number = 1;
7512 break;
7513 case CMD_tabmove:
7514 tab_number = LAST_TAB_NR;
7515 break;
7516 default:
7517 tab_number = tabpage_index(curtab);
7518 }
7519 }
7520
7521theend:
7522 return tab_number;
7523}
7524
7525/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007526 * ":tabclose": close current tab page, unless it is the last one.
7527 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 */
7529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007530ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007532 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007533 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007534
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007535# ifdef FEAT_CMDWIN
7536 if (cmdwin_type != 0)
7537 cmdwin_result = K_IGNORE;
7538 else
7539# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007540 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007541 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007542 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007544 tab_number = get_tabpage_arg(eap);
7545 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007546 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007547 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007548 if (tp == NULL)
7549 {
7550 beep_flush();
7551 return;
7552 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007553 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007554 {
7555 tabpage_close_other(tp, eap->forceit);
7556 return;
7557 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007558 else if (!text_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007559#ifdef FEAT_AUTOCMD
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007560 && !curbuf_locked()
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007561#endif
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007562 )
7563 tabpage_close(eap->forceit);
7564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007566}
7567
7568/*
7569 * ":tabonly": close all tab pages except the current one
7570 */
7571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007572ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007573{
7574 tabpage_T *tp;
7575 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007576 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007577
7578# ifdef FEAT_CMDWIN
7579 if (cmdwin_type != 0)
7580 cmdwin_result = K_IGNORE;
7581 else
7582# endif
7583 if (first_tabpage->tp_next == NULL)
7584 MSG(_("Already only one tab page"));
7585 else
7586 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007587 tab_number = get_tabpage_arg(eap);
7588 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007589 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007590 goto_tabpage(tab_number);
7591 /* Repeat this up to a 1000 times, because autocommands may
7592 * mess up the lists. */
7593 for (done = 0; done < 1000; ++done)
7594 {
7595 FOR_ALL_TABPAGES(tp)
7596 if (tp->tp_topframe != topframe)
7597 {
7598 tabpage_close_other(tp, eap->forceit);
7599 /* if we failed to close it quit */
7600 if (valid_tabpage(tp))
7601 done = 1000;
7602 /* start over, "tp" is now invalid */
7603 break;
7604 }
7605 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007606 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007607 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007608 }
7609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
7612/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007613 * Close the current tab page.
7614 */
7615 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007616tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007617{
7618 /* First close all the windows but the current one. If that worked then
7619 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007620 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007621 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007622 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007623 ex_win_close(forceit, curwin, NULL);
7624# ifdef FEAT_GUI
7625 need_mouse_correct = TRUE;
7626# endif
7627}
7628
7629/*
7630 * Close tab page "tp", which is not the current tab page.
7631 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007632 * Also takes care of the tab pages line disappearing when closing the
7633 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007634 */
7635 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007636tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007637{
7638 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007639 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007640 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007641
7642 /* Limit to 1000 windows, autocommands may add a window while we close
7643 * one. OK, so I'm paranoid... */
7644 while (++done < 1000)
7645 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007646 wp = tp->tp_firstwin;
7647 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007648
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007649 /* Autocommands may delete the tab page under our fingers and we may
7650 * fail to close a window with a modified buffer. */
7651 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007652 break;
7653 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007654
Bram Moolenaar29323592016-07-24 22:04:11 +02007655#ifdef FEAT_AUTOCMD
7656 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
7657#endif
7658
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007659 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007660 if (h != tabline_height())
7661 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007662}
7663
7664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 * ":only".
7666 */
7667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007668ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007670 win_T *wp;
7671 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672# ifdef FEAT_GUI
7673 need_mouse_correct = TRUE;
7674# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007675 if (eap->addr_count > 0)
7676 {
7677 wnr = eap->line2;
7678 for (wp = firstwin; --wnr > 0; )
7679 {
7680 if (wp->w_next == NULL)
7681 break;
7682 else
7683 wp = wp->w_next;
7684 }
7685 win_goto(wp);
7686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 close_others(TRUE, eap->forceit);
7688}
7689
7690/*
7691 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007692 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007694 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007695ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
7697 if (eap->addr_count == 0)
7698 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007699 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701
7702 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007703ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007705 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007706 if (!eap->skip)
7707 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007708#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007709 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007710#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007711 if (eap->addr_count == 0)
7712 win_close(curwin, FALSE); /* don't free buffer */
7713 else
7714 {
7715 int winnr = 0;
7716 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007717
Bram Moolenaar2256c992016-11-15 21:17:07 +01007718 FOR_ALL_WINDOWS(win)
7719 {
7720 winnr++;
7721 if (winnr == eap->line2)
7722 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007723 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007724 if (win == NULL)
7725 win = lastwin;
7726 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 }
7729}
7730
7731/*
7732 * ":stop" and ":suspend": Suspend Vim.
7733 */
7734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007735ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 /*
7738 * Disallow suspending for "rvim".
7739 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007740 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {
7742 if (!eap->forceit)
7743 autowrite_all();
7744 windgoto((int)Rows - 1, 0);
7745 out_char('\n');
7746 out_flush();
7747 stoptermcap();
7748 out_flush(); /* needed for SUN to restore xterm buffer */
7749#ifdef FEAT_TITLE
7750 mch_restore_title(3); /* restore window titles */
7751#endif
7752 ui_suspend(); /* call machine specific function */
7753#ifdef FEAT_TITLE
7754 maketitle();
7755 resettitle(); /* force updating the title */
7756#endif
7757 starttermcap();
7758 scroll_start(); /* scroll screen before redrawing */
7759 redraw_later_clear();
7760 shell_resized(); /* may have resized window */
7761 }
7762}
7763
7764/*
7765 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7766 */
7767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007768ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769{
7770#ifdef FEAT_CMDWIN
7771 if (cmdwin_type != 0)
7772 {
7773 cmdwin_result = Ctrl_C;
7774 return;
7775 }
7776#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007777 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007778 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007779 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007780 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007781 return;
7782 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007783#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007784 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7785 /* Refuse to quit when locked or when the buffer in the last window is
7786 * being closed (can only happen in autocommands). */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02007787 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007788 return;
7789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 /*
7792 * if more files or windows we won't exit
7793 */
7794 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7795 exiting = TRUE;
7796 if ( ((eap->cmdidx == CMD_wq
7797 || curbufIsChanged())
7798 && do_write(eap) == FAIL)
7799 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007800 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 {
7802 not_exiting();
7803 }
7804 else
7805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007808 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809# ifdef FEAT_GUI
7810 need_mouse_correct = TRUE;
7811# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007812 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007813 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 }
7815}
7816
7817/*
7818 * ":print", ":list", ":number".
7819 */
7820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007821ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007823 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7824 EMSG(_(e_emptybuf));
7825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007827 for ( ;!got_int; ui_breakcheck())
7828 {
7829 print_line(eap->line1,
7830 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7831 || (eap->flags & EXFLAG_NR)),
7832 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7833 if (++eap->line1 > eap->line2)
7834 break;
7835 out_flush(); /* show one line at a time */
7836 }
7837 setpcmark();
7838 /* put cursor at last line */
7839 curwin->w_cursor.lnum = eap->line2;
7840 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844}
7845
7846#ifdef FEAT_BYTEOFF
7847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007848ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850 goto_byte(eap->line2);
7851}
7852#endif
7853
7854/*
7855 * ":shell".
7856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 do_shell(NULL, 0);
7861}
7862
Bram Moolenaar4033c552017-09-16 20:54:51 +02007863#if defined(HAVE_DROP_FILE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007865 || defined(FEAT_GUI_MSWIN) \
7866 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 || defined(PROTO)
7868
7869/*
7870 * Handle a file drop. The code is here because a drop is *nearly* like an
7871 * :args command, but not quite (we have a list of exact filenames, so we
7872 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7873 * code is very similar to :args and hence needs access to a lot of the static
7874 * functions in this file.
7875 *
7876 * The list should be allocated using alloc(), as should each item in the
7877 * list. This function takes over responsibility for freeing the list.
7878 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007879 * XXX The list is made into the argument list. This is freed using
Bram Moolenaara06ecab2016-07-16 14:47:36 +02007880 * FreeWild(), which does a series of vim_free() calls.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 */
7882 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007883handle_drop(
7884 int filec, /* the number of files dropped */
7885 char_u **filev, /* the list of files dropped */
7886 int split) /* force splitting the window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 exarg_T ea;
7889 int save_msg_scroll = msg_scroll;
7890
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007891 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007892 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007894#ifdef FEAT_AUTOCMD
7895 if (curbuf_locked())
7896 return;
7897#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007898 /* When the screen is being updated we should not change buffers and
7899 * windows structures, it may cause freed memory to be used. */
7900 if (updating_screen)
7901 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902
7903 /* Check whether the current buffer is changed. If so, we will need
7904 * to split the current window or data could be lost.
7905 * We don't need to check if the 'hidden' option is set, as in this
7906 * case the buffer won't be lost.
7907 */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007908 if (!buf_hide(curbuf) && !split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {
7910 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007911 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 --emsg_off;
7913 }
7914 if (split)
7915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 if (win_split(0, 0) == FAIL)
7917 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007918 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
7920 /* When splitting the window, create a new alist. Otherwise the
7921 * existing one is overwritten. */
7922 alist_unlink(curwin->w_alist);
7923 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
7925
7926 /*
7927 * Set up the new argument list.
7928 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007929 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
7931 /*
7932 * Move to the first file.
7933 */
7934 /* Fake up a minimal "next" command for do_argfile() */
7935 vim_memset(&ea, 0, sizeof(ea));
7936 ea.cmd = (char_u *)"next";
7937 do_argfile(&ea, 0);
7938
7939 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7940 * mode that is not needed here. */
7941 need_start_insertmode = FALSE;
7942
7943 /* Restore msg_scroll, otherwise a following command may cause scrolling
7944 * unexpectedly. The screen will be redrawn by the caller, thus
7945 * msg_scroll being set by displaying a message is irrelevant. */
7946 msg_scroll = save_msg_scroll;
7947}
7948#endif
7949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950/*
7951 * Clear an argument list: free all file names and reset it to zero entries.
7952 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007953 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007954alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
7956 while (--al->al_ga.ga_len >= 0)
7957 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7958 ga_clear(&al->al_ga);
7959}
7960
7961/*
7962 * Init an argument list.
7963 */
7964 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007965alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
7967 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7968}
7969
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970/*
7971 * Remove a reference from an argument list.
7972 * Ignored when the argument list is the global one.
7973 * If the argument list is no longer used by any window, free it.
7974 */
7975 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
7978 if (al != &global_alist && --al->al_refcount <= 0)
7979 {
7980 alist_clear(al);
7981 vim_free(al);
7982 }
7983}
7984
Bram Moolenaar4033c552017-09-16 20:54:51 +02007985#if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986/*
7987 * Create a new argument list and use it for the current window.
7988 */
7989 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007990alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7993 if (curwin->w_alist == NULL)
7994 {
7995 curwin->w_alist = &global_alist;
7996 ++global_alist.al_refcount;
7997 }
7998 else
7999 {
8000 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008001 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 alist_init(curwin->w_alist);
8003 }
8004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005#endif
8006
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008007#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008/*
8009 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008010 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8011 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 */
8013 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008014alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
8016 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008017 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 char_u **new_arg_files;
8019 int new_arg_file_count;
8020 char_u *save_p_su = p_su;
8021 int i;
8022
8023 /* Don't use 'suffixes' here. This should work like the shell did the
8024 * expansion. Also, the vimrc file isn't read yet, thus the user
8025 * can't set the options. */
8026 p_su = empty_option;
8027 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8028 if (old_arg_files != NULL)
8029 {
8030 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008031 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8032 old_arg_count = GARGCOUNT;
8033 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008035 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 && new_arg_file_count > 0)
8037 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008038 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8039 TRUE, fnum_list, fnum_len);
8040 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 }
8043 p_su = save_p_su;
8044}
8045#endif
8046
8047/*
8048 * Set the argument list for the current window.
8049 * Takes over the allocated files[] and the allocated fnames in it.
8050 */
8051 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052alist_set(
8053 alist_T *al,
8054 int count,
8055 char_u **files,
8056 int use_curbuf,
8057 int *fnum_list,
8058 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008061 static int recursive = 0;
8062
8063 if (recursive)
8064 {
8065#ifdef FEAT_AUTOCMD
8066 EMSG(_(e_au_recursive));
8067#endif
8068 return;
8069 }
8070 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071
8072 alist_clear(al);
8073 if (ga_grow(&al->al_ga, count) == OK)
8074 {
8075 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008076 {
8077 if (got_int)
8078 {
8079 /* When adding many buffers this can take a long time. Allow
8080 * interrupting here. */
8081 while (i < count)
8082 vim_free(files[i++]);
8083 break;
8084 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008085
8086 /* May set buffer name of a buffer previously used for the
8087 * argument list, so that it's re-used by alist_add. */
8088 if (fnum_list != NULL && i < fnum_len)
8089 buf_set_name(fnum_list[i], files[i]);
8090
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008092 ui_breakcheck();
8093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 vim_free(files);
8095 }
8096 else
8097 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008100
8101 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102}
8103
8104/*
8105 * Add file "fname" to argument list "al".
8106 * "fname" must have been allocated and "al" must have been checked for room.
8107 */
8108 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008109alist_add(
8110 alist_T *al,
8111 char_u *fname,
8112 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113{
8114 if (fname == NULL) /* don't add NULL file names */
8115 return;
8116#ifdef BACKSLASH_IN_FILENAME
8117 slash_adjust(fname);
8118#endif
8119 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8120 if (set_fnum > 0)
8121 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8122 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8123 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124}
8125
8126#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8127/*
8128 * Adjust slashes in file names. Called after 'shellslash' was set.
8129 */
8130 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008131alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
8133 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008135 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
8137 for (i = 0; i < GARGCOUNT; ++i)
8138 if (GARGLIST[i].ae_fname != NULL)
8139 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008140 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 if (wp->w_alist != &global_alist)
8142 for (i = 0; i < WARGCOUNT(wp); ++i)
8143 if (WARGLIST(wp)[i].ae_fname != NULL)
8144 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145}
8146#endif
8147
8148/*
8149 * ":preserve".
8150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008152ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008154 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 ml_preserve(curbuf, TRUE);
8156}
8157
8158/*
8159 * ":recover".
8160 */
8161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008162ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163{
8164 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8165 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008166 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8167 | CCGD_MULTWIN
8168 | (eap->forceit ? CCGD_FORCEIT : 0)
8169 | CCGD_EXCMD)
8170
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 && (*eap->arg == NUL
8172 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8173 ml_recover();
8174 recoverymode = FALSE;
8175}
8176
8177/*
8178 * Command modifier used in a wrong way.
8179 */
8180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182{
8183 eap->errmsg = e_invcmd;
8184}
8185
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186/*
8187 * :sview [+command] file split window with new file, read-only
8188 * :split [[+command] file] split window with current or new file
8189 * :vsplit [[+command] file] split window vertically with current or new file
8190 * :new [[+command] file] split window with no or new file
8191 * :vnew [[+command] file] split vertically window with no or new file
8192 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008193 *
8194 * :tabedit open new Tab page with empty window
8195 * :tabedit [+command] file open new Tab page and edit "file"
8196 * :tabnew [[+command] file] just like :tabedit
8197 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 */
8199 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008200ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008202 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008203#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008205#endif
8206#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209
Bram Moolenaar4033c552017-09-16 20:54:51 +02008210#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213
Bram Moolenaar4033c552017-09-16 20:54:51 +02008214#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008216 * quickfix windows. But it's OK when doing ":tab split". */
8217 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 if (eap->cmdidx == CMD_split)
8220 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (eap->cmdidx == CMD_vsplit)
8222 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225
Bram Moolenaar4033c552017-09-16 20:54:51 +02008226#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008227 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
8229 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8230 FNAME_MESS, TRUE, curbuf->b_ffname);
8231 if (fname == NULL)
8232 goto theend;
8233 eap->arg = fname;
8234 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008235# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008236 else
8237# endif
8238#endif
8239#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 && eap->cmdidx != CMD_new)
8243 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008244# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008245 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008246# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008247 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008248# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008249 au_has_group((char_u *)"FileExplorer"))
8250 {
8251 /* No browsing supported but we do have the file explorer:
8252 * Edit the directory. */
8253 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8254 eap->arg = (char_u *)".";
8255 }
8256 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00008257# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008258 {
8259 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008261 if (fname == NULL)
8262 goto theend;
8263 eap->arg = fname;
8264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 }
8266 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008269 /*
8270 * Either open new tab page or split the window.
8271 */
8272 if (eap->cmdidx == CMD_tabedit
8273 || eap->cmdidx == CMD_tabfind
8274 || eap->cmdidx == CMD_tabnew)
8275 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008276 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8277 : eap->addr_count == 0 ? 0
8278 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008279 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008280 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008281
8282 /* set the alternate buffer for the window we came from */
8283 if (curwin != old_curwin
8284 && win_valid(old_curwin)
8285 && old_curwin->w_buffer != curbuf
8286 && !cmdmod.keepalt)
8287 old_curwin->w_alt_fnum = curbuf->b_fnum;
8288 }
8289 }
8290 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8292 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008293# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 /* Reset 'scrollbind' when editing another file, but keep it when
8295 * doing ":split" without arguments. */
8296 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008297# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008301 {
8302 RESET_BINDING(curwin);
8303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 else
8305 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 do_exedit(eap, old_curwin);
8308 }
8309
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008310# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008314# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315theend:
8316 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008319
8320/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008321 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008322 */
8323 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008324tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008325{
8326 exarg_T ea;
8327
8328 vim_memset(&ea, 0, sizeof(ea));
8329 ea.cmdidx = CMD_tabnew;
8330 ea.cmd = (char_u *)"tabn";
8331 ea.arg = (char_u *)"";
8332 ex_splitview(&ea);
8333}
8334
8335/*
8336 * :tabnext command
8337 */
8338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008339ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008340{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008341 int tab_number;
8342
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008343 switch (eap->cmdidx)
8344 {
8345 case CMD_tabfirst:
8346 case CMD_tabrewind:
8347 goto_tabpage(1);
8348 break;
8349 case CMD_tablast:
8350 goto_tabpage(9999);
8351 break;
8352 case CMD_tabprevious:
8353 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008354 if (eap->arg && *eap->arg != NUL)
8355 {
8356 char_u *p = eap->arg;
8357 char_u *p_save = p;
8358
8359 tab_number = getdigits(&p);
8360 if (p == p_save || *p_save == '-' || *p != NUL
8361 || tab_number == 0)
8362 {
8363 /* No numbers as argument. */
8364 eap->errmsg = e_invarg;
8365 return;
8366 }
8367 }
8368 else
8369 {
8370 if (eap->addr_count == 0)
8371 tab_number = 1;
8372 else
8373 {
8374 tab_number = eap->line2;
8375 if (tab_number < 1)
8376 {
8377 eap->errmsg = e_invrange;
8378 return;
8379 }
8380 }
8381 }
8382 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008383 break;
8384 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008385 tab_number = get_tabpage_arg(eap);
8386 if (eap->errmsg == NULL)
8387 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008388 break;
8389 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008390}
8391
8392/*
8393 * :tabmove command
8394 */
8395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008396ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008397{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008398 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008399
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008400 tab_number = get_tabpage_arg(eap);
8401 if (eap->errmsg == NULL)
8402 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008403}
8404
8405/*
8406 * :tabs command: List tabs and their contents.
8407 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008409ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008410{
8411 tabpage_T *tp;
8412 win_T *wp;
8413 int tabcount = 1;
8414
8415 msg_start();
8416 msg_scroll = TRUE;
8417 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8418 {
8419 msg_putchar('\n');
8420 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008421 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008422 out_flush(); /* output one line at a time */
8423 ui_breakcheck();
8424
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008425 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008426 wp = firstwin;
8427 else
8428 wp = tp->tp_firstwin;
8429 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8430 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008431 msg_putchar('\n');
8432 msg_putchar(wp == curwin ? '>' : ' ');
8433 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008434 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8435 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008436 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008437 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008438 else
8439 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8440 IObuff, IOSIZE, TRUE);
8441 msg_outtrans(IObuff);
8442 out_flush(); /* output one line at a time */
8443 ui_breakcheck();
8444 }
8445 }
8446}
8447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448/*
8449 * ":mode": Set screen mode.
8450 * If no argument given, just get the screen size and redraw.
8451 */
8452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 if (*eap->arg == NUL)
8456 shell_resized();
8457 else
8458 mch_screenmode(eap->arg);
8459}
8460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461/*
8462 * ":resize".
8463 * set, increment or decrement current window height
8464 */
8465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008466ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
8468 int n;
8469 win_T *wp = curwin;
8470
8471 if (eap->addr_count > 0)
8472 {
8473 n = eap->line2;
8474 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8475 ;
8476 }
8477
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008478# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (cmdmod.split & WSP_VERT)
8483 {
8484 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008485 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8487 n = 9999;
8488 win_setwidth_win((int)n, wp);
8489 }
8490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {
8492 if (*eap->arg == '-' || *eap->arg == '+')
8493 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008494 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 n = 9999;
8496 win_setheight_win((int)n, wp);
8497 }
8498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
8500/*
8501 * ":find [+command] <file>" command.
8502 */
8503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008504ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506#ifdef FEAT_SEARCHPATH
8507 char_u *fname;
8508 int count;
8509
8510 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8511 TRUE, curbuf->b_ffname);
8512 if (eap->addr_count > 0)
8513 {
8514 /* Repeat finding the file "count" times. This matters when it
8515 * appears several times in the path. */
8516 count = eap->line2;
8517 while (fname != NULL && --count > 0)
8518 {
8519 vim_free(fname);
8520 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8521 FALSE, curbuf->b_ffname);
8522 }
8523 }
8524
8525 if (fname != NULL)
8526 {
8527 eap->arg = fname;
8528#endif
8529 do_exedit(eap, NULL);
8530#ifdef FEAT_SEARCHPATH
8531 vim_free(fname);
8532 }
8533#endif
8534}
8535
8536/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008537 * ":open" simulation: for now just work like ":visual".
8538 */
8539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008540ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008541{
8542 regmatch_T regmatch;
8543 char_u *p;
8544
8545 curwin->w_cursor.lnum = eap->line2;
8546 beginline(BL_SOL | BL_FIX);
8547 if (*eap->arg == '/')
8548 {
8549 /* ":open /pattern/": put cursor in column found with pattern */
8550 ++eap->arg;
8551 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8552 *p = NUL;
8553 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8554 if (regmatch.regprog != NULL)
8555 {
8556 regmatch.rm_ic = p_ic;
8557 p = ml_get_curline();
8558 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008559 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008560 else
8561 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008562 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008563 }
8564 /* Move to the NUL, ignore any other arguments. */
8565 eap->arg += STRLEN(eap->arg);
8566 }
8567 check_cursor();
8568
8569 eap->cmdidx = CMD_visual;
8570 do_exedit(eap, NULL);
8571}
8572
8573/*
8574 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 */
8576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008577ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 do_exedit(eap, NULL);
8580}
8581
8582/*
8583 * ":edit <file>" command and alikes.
8584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008586do_exedit(
8587 exarg_T *eap,
8588 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008592 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
8594 /*
8595 * ":vi" command ends Ex mode.
8596 */
8597 if (exmode_active && (eap->cmdidx == CMD_visual
8598 || eap->cmdidx == CMD_view))
8599 {
8600 exmode_active = FALSE;
8601 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008602 {
8603 /* Special case: ":global/pat/visual\NLvi-commands" */
8604 if (global_busy)
8605 {
8606 int rd = RedrawingDisabled;
8607 int nwr = no_wait_return;
8608 int ms = msg_scroll;
8609#ifdef FEAT_GUI
8610 int he = hold_gui_events;
8611#endif
8612
8613 if (eap->nextcmd != NULL)
8614 {
8615 stuffReadbuff(eap->nextcmd);
8616 eap->nextcmd = NULL;
8617 }
8618
8619 if (exmode_was != EXMODE_VIM)
8620 settmode(TMODE_RAW);
8621 RedrawingDisabled = 0;
8622 no_wait_return = 0;
8623 need_wait_return = FALSE;
8624 msg_scroll = 0;
8625#ifdef FEAT_GUI
8626 hold_gui_events = 0;
8627#endif
8628 must_redraw = CLEAR;
8629
8630 main_loop(FALSE, TRUE);
8631
8632 RedrawingDisabled = rd;
8633 no_wait_return = nwr;
8634 msg_scroll = ms;
8635#ifdef FEAT_GUI
8636 hold_gui_events = he;
8637#endif
8638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
8642
8643 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008644 || eap->cmdidx == CMD_tabnew
8645 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008646 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008648 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 setpcmark();
8650 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008651 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8652 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008654 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 || *eap->arg != NUL
8656#ifdef FEAT_BROWSE
8657 || cmdmod.browse
8658#endif
8659 )
8660 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008661#ifdef FEAT_AUTOCMD
8662 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8663 * can bring us here, others are stopped earlier. */
8664 if (*eap->arg != NUL && curbuf_locked())
8665 return;
8666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 n = readonlymode;
8668 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8669 readonlymode = TRUE;
8670 else if (eap->cmdidx == CMD_enew)
8671 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8672 empty buffer */
8673 setpcmark();
8674 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8675 NULL, eap,
8676 /* ":edit" goes to first line if Vi compatible */
8677 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8678 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8679 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008680 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008682 /* after a split we can use an existing buffer */
8683 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684#ifdef FEAT_LISTCMDS
8685 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8686#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008687 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 {
8689 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 if (old_curwin != NULL)
8691 {
8692 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008693 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02008695#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008696 cleanup_T cs;
8697
8698 /* Reset the error/interrupt/exception state here so that
8699 * aborting() returns FALSE when closing a window. */
8700 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008701#endif
8702#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008704#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008705 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008706
Bram Moolenaar4033c552017-09-16 20:54:51 +02008707#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008708 /* Restore the error/interrupt/exception state if not
8709 * discarded by a new aborting error, interrupt, or
8710 * uncaught exception. */
8711 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 }
8714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 }
8716 else if (readonlymode && curbuf->b_nwindows == 1)
8717 {
8718 /* When editing an already visited buffer, 'readonly' won't be set
8719 * but the previous value is kept. With ":view" and ":sview" we
8720 * want the file to be readonly, except when another window is
8721 * editing the same buffer. */
8722 curbuf->b_p_ro = TRUE;
8723 }
8724 readonlymode = n;
8725 }
8726 else
8727 {
8728 if (eap->do_ecmd_cmd != NULL)
8729 do_cmdline_cmd(eap->do_ecmd_cmd);
8730#ifdef FEAT_TITLE
8731 n = curwin->w_arg_idx_invalid;
8732#endif
8733 check_arg_idx(curwin);
8734#ifdef FEAT_TITLE
8735 if (n != curwin->w_arg_idx_invalid)
8736 maketitle();
8737#endif
8738 }
8739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 /*
8741 * if ":split file" worked, set alternate file name in old window to new
8742 * file
8743 */
8744 if (old_curwin != NULL
8745 && *eap->arg != NUL
8746 && curwin != old_curwin
8747 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008748 && old_curwin->w_buffer != curbuf
8749 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
8752 ex_no_reprint = TRUE;
8753}
8754
8755#ifndef FEAT_GUI
8756/*
8757 * ":gui" and ":gvim" when there is no GUI.
8758 */
8759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008760ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 eap->errmsg = e_nogvim;
8763}
8764#endif
8765
8766#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008768ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
8770 gui_make_tearoff(eap->arg);
8771}
8772#endif
8773
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008774#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008776ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008778 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780#endif
8781
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008783ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
8785 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8786 MSG(_("No swap file"));
8787 else
8788 msg(curbuf->b_ml.ml_mfp->mf_fname);
8789}
8790
8791/*
8792 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8793 * offset.
8794 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008797ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
8799#ifdef FEAT_SCROLLBIND
8800 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008801 win_T *save_curwin = curwin;
8802 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 long topline;
8804 long y;
8805 linenr_T old_linenr = curwin->w_cursor.lnum;
8806
8807 setpcmark();
8808
8809 /*
8810 * determine max topline
8811 */
8812 if (curwin->w_p_scb)
8813 {
8814 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008815 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 {
8817 if (wp->w_p_scb && wp->w_buffer)
8818 {
8819 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8820 if (topline > y)
8821 topline = y;
8822 }
8823 }
8824 if (topline < 1)
8825 topline = 1;
8826 }
8827 else
8828 {
8829 topline = 1;
8830 }
8831
8832
8833 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008834 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008836 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
8838 if (curwin->w_p_scb)
8839 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008840 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 y = topline - curwin->w_topline;
8842 if (y > 0)
8843 scrollup(y, TRUE);
8844 else
8845 scrolldown(-y, TRUE);
8846 curwin->w_scbind_pos = topline;
8847 redraw_later(VALID);
8848 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008852 curwin = save_curwin;
8853 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 if (curwin->w_p_scb)
8855 {
8856 did_syncbind = TRUE;
8857 checkpcmark();
8858 if (old_linenr != curwin->w_cursor.lnum)
8859 {
8860 char_u ctrl_o[2];
8861
8862 ctrl_o[0] = Ctrl_O;
8863 ctrl_o[1] = 0;
8864 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8865 }
8866 }
8867#endif
8868}
8869
8870
8871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008872ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008874 int i;
8875 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8876 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
8878 if (eap->usefilter) /* :r!cmd */
8879 do_bang(1, eap, FALSE, FALSE, TRUE);
8880 else
8881 {
8882 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8883 return;
8884
8885#ifdef FEAT_BROWSE
8886 if (cmdmod.browse)
8887 {
8888 char_u *browseFile;
8889
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008890 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 NULL, NULL, NULL, curbuf);
8892 if (browseFile != NULL)
8893 {
8894 i = readfile(browseFile, NULL,
8895 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8896 vim_free(browseFile);
8897 }
8898 else
8899 i = OK;
8900 }
8901 else
8902#endif
8903 if (*eap->arg == NUL)
8904 {
8905 if (check_fname() == FAIL) /* check for no file name */
8906 return;
8907 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8908 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8909 }
8910 else
8911 {
8912 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8913 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8914 i = readfile(eap->arg, NULL,
8915 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8916
8917 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01008918 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 {
8920#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8921 if (!aborting())
8922#endif
8923 EMSG2(_(e_notopen), eap->arg);
8924 }
8925 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008926 {
8927 if (empty && exmode_active)
8928 {
8929 /* Delete the empty line that remains. Historically ex does
8930 * this but vi doesn't. */
8931 if (eap->line2 == 0)
8932 lnum = curbuf->b_ml.ml_line_count;
8933 else
8934 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008935 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008936 {
8937 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008938 if (curwin->w_cursor.lnum > 1
8939 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008940 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008941 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008942 }
8943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 }
8947}
8948
Bram Moolenaarea408852005-06-25 22:49:46 +00008949static char_u *prev_dir = NULL;
8950
8951#if defined(EXITFREE) || defined(PROTO)
8952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008953free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00008954{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008955 VIM_CLEAR(prev_dir);
8956 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00008957}
8958#endif
8959
Bram Moolenaarf4258302013-06-02 18:20:17 +02008960/*
8961 * Deal with the side effects of changing the current directory.
8962 * When "local" is TRUE then this was after an ":lcd" command.
8963 */
8964 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008965post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008966{
Bram Moolenaard23a8232018-02-10 18:45:26 +01008967 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008968 if (local)
8969 {
8970 /* If still in global directory, need to remember current
8971 * directory as global directory. */
8972 if (globaldir == NULL && prev_dir != NULL)
8973 globaldir = vim_strsave(prev_dir);
8974 /* Remember this local directory for the window. */
8975 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8976 curwin->w_localdir = vim_strsave(NameBuff);
8977 }
8978 else
8979 {
8980 /* We are now in the global directory, no need to remember its
8981 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01008982 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008983 }
8984
8985 shorten_fnames(TRUE);
8986}
8987
Bram Moolenaarea408852005-06-25 22:49:46 +00008988
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989/*
8990 * ":cd", ":lcd", ":chdir" and ":lchdir".
8991 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008992 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008993ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 char_u *new_dir;
8996 char_u *tofree;
8997
8998 new_dir = eap->arg;
8999#if !defined(UNIX) && !defined(VMS)
9000 /* for non-UNIX ":cd" means: print current directory */
9001 if (*new_dir == NUL)
9002 ex_pwd(NULL);
9003 else
9004#endif
9005 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009006#ifdef FEAT_AUTOCMD
9007 if (allbuf_locked())
9008 return;
9009#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009010 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9011 && !eap->forceit)
9012 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009013 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009014 return;
9015 }
9016
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 /* ":cd -": Change to previous directory */
9018 if (STRCMP(new_dir, "-") == 0)
9019 {
9020 if (prev_dir == NULL)
9021 {
9022 EMSG(_("E186: No previous directory"));
9023 return;
9024 }
9025 new_dir = prev_dir;
9026 }
9027
9028 /* Save current directory for next ":cd -" */
9029 tofree = prev_dir;
9030 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9031 prev_dir = vim_strsave(NameBuff);
9032 else
9033 prev_dir = NULL;
9034
9035#if defined(UNIX) || defined(VMS)
9036 /* for UNIX ":cd" means: go to home directory */
9037 if (*new_dir == NUL)
9038 {
9039 /* use NameBuff for home directory name */
9040# ifdef VMS
9041 char_u *p;
9042
9043 p = mch_getenv((char_u *)"SYS$LOGIN");
9044 if (p == NULL || *p == NUL) /* empty is the same as not set */
9045 NameBuff[0] = NUL;
9046 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009047 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048# else
9049 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9050# endif
9051 new_dir = NameBuff;
9052 }
9053#endif
9054 if (new_dir == NULL || vim_chdir(new_dir))
9055 EMSG(_(e_failed));
9056 else
9057 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009058 int is_local_chdir = eap->cmdidx == CMD_lcd
9059 || eap->cmdidx == CMD_lchdir;
9060
9061 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062
9063 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009064 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009066#ifdef FEAT_AUTOCMD
9067 apply_autocmds(EVENT_DIRCHANGED,
9068 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9069 new_dir, FALSE, curbuf);
9070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 }
9072 vim_free(tofree);
9073 }
9074}
9075
9076/*
9077 * ":pwd".
9078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009080ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9083 {
9084#ifdef BACKSLASH_IN_FILENAME
9085 slash_adjust(NameBuff);
9086#endif
9087 msg(NameBuff);
9088 }
9089 else
9090 EMSG(_("E187: Unknown"));
9091}
9092
9093/*
9094 * ":=".
9095 */
9096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009097ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
9099 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009100 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101}
9102
9103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009104ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009106 int n;
9107 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108
9109 if (cursor_valid())
9110 {
9111 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9112 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009113 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009115
9116 len = eap->line2;
9117 switch (*eap->arg)
9118 {
9119 case 'm': break;
9120 case NUL: len *= 1000L; break;
9121 default: EMSG2(_(e_invarg2), eap->arg); return;
9122 }
9123 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124}
9125
9126/*
9127 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9128 */
9129 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009130do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
9132 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009133 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134
9135 cursor_on();
9136 out_flush();
Bram Moolenaar975b5272016-03-15 23:10:59 +01009137 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009139 wait_now = msec - done > 1000L ? 1000L : msec - done;
9140#ifdef FEAT_TIMERS
9141 {
9142 long due_time = check_due_timer();
9143
9144 if (due_time > 0 && due_time < wait_now)
9145 wait_now = due_time;
9146 }
9147#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009148#ifdef FEAT_JOB_CHANNEL
9149 if (has_any_channel() && wait_now > 100L)
9150 wait_now = 100L;
9151#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009152 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009153#ifdef FEAT_JOB_CHANNEL
9154 if (has_any_channel())
9155 ui_breakcheck_force(TRUE);
9156 else
9157#endif
9158 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009159#ifdef MESSAGE_QUEUE
9160 /* Process the netbeans and clientserver messages that may have been
9161 * received in the call to ui_breakcheck() when the GUI is in use. This
9162 * may occur when running a test case. */
9163 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 }
9166}
9167
9168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009169do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170{
9171 int mode;
9172 char_u *cmdp;
9173
9174 cmdp = eap->cmd;
9175 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9176
9177 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9178 eap->arg, mode, isabbrev))
9179 {
9180 case 1: EMSG(_(e_invarg));
9181 break;
9182 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9183 break;
9184 }
9185}
9186
9187/*
9188 * ":winsize" command (obsolete).
9189 */
9190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009191ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192{
9193 int w, h;
9194 char_u *arg = eap->arg;
9195 char_u *p;
9196
9197 w = getdigits(&arg);
9198 arg = skipwhite(arg);
9199 p = arg;
9200 h = getdigits(&arg);
9201 if (*p != NUL && *arg == NUL)
9202 set_shellsize(w, h, TRUE);
9203 else
9204 EMSG(_("E465: :winsize requires two number arguments"));
9205}
9206
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009208ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210 int xchar = NUL;
9211 char_u *p;
9212
9213 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9214 {
9215 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9216 if (eap->arg[1] == NUL)
9217 {
9218 EMSG(_(e_invarg));
9219 return;
9220 }
9221 xchar = eap->arg[1];
9222 p = eap->arg + 2;
9223 }
9224 else
9225 p = eap->arg + 1;
9226
9227 eap->nextcmd = check_nextcmd(p);
9228 p = skipwhite(p);
9229 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9230 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009231 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 {
9233 /* Pass flags on for ":vertical wincmd ]". */
9234 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009235 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9237 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009238 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 }
9240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
Bram Moolenaar843ee412004-06-30 16:16:41 +00009242#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243/*
9244 * ":winpos".
9245 */
9246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009247ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248{
9249 int x, y;
9250 char_u *arg = eap->arg;
9251 char_u *p;
9252
9253 if (*arg == NUL)
9254 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009255# if defined(FEAT_GUI) || defined(MSWIN)
9256# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009258# else
9259 if (mch_get_winpos(&x, &y) != FAIL)
9260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 {
9262 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9263 msg(IObuff);
9264 }
9265 else
9266# endif
9267 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9268 }
9269 else
9270 {
9271 x = getdigits(&arg);
9272 arg = skipwhite(arg);
9273 p = arg;
9274 y = getdigits(&arg);
9275 if (*p == NUL || *arg != NUL)
9276 {
9277 EMSG(_("E466: :winpos requires two number arguments"));
9278 return;
9279 }
9280# ifdef FEAT_GUI
9281 if (gui.in_use)
9282 gui_mch_set_winpos(x, y);
9283 else if (gui.starting)
9284 {
9285 /* Remember the coordinates for when the window is opened. */
9286 gui_win_x = x;
9287 gui_win_y = y;
9288 }
9289# ifdef HAVE_TGETENT
9290 else
9291# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009292# else
9293# ifdef MSWIN
9294 mch_set_winpos(x, y);
9295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296# endif
9297# ifdef HAVE_TGETENT
9298 if (*T_CWP)
9299 term_set_winpos(x, y);
9300# endif
9301 }
9302}
9303#endif
9304
9305/*
9306 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9307 */
9308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009309ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311 oparg_T oa;
9312
9313 clear_oparg(&oa);
9314 oa.regname = eap->regname;
9315 oa.start.lnum = eap->line1;
9316 oa.end.lnum = eap->line2;
9317 oa.line_count = eap->line2 - eap->line1 + 1;
9318 oa.motion_type = MLINE;
9319#ifdef FEAT_VIRTUALEDIT
9320 virtual_op = FALSE;
9321#endif
9322 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9323 {
9324 setpcmark();
9325 curwin->w_cursor.lnum = eap->line1;
9326 beginline(BL_SOL | BL_FIX);
9327 }
9328
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009329 if (VIsual_active)
9330 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009331
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 switch (eap->cmdidx)
9333 {
9334 case CMD_delete:
9335 oa.op_type = OP_DELETE;
9336 op_delete(&oa);
9337 break;
9338
9339 case CMD_yank:
9340 oa.op_type = OP_YANK;
9341 (void)op_yank(&oa, FALSE, TRUE);
9342 break;
9343
9344 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009345 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009347 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9348#else
9349 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009351 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 oa.op_type = OP_RSHIFT;
9353 else
9354 oa.op_type = OP_LSHIFT;
9355 op_shift(&oa, FALSE, eap->amount);
9356 break;
9357 }
9358#ifdef FEAT_VIRTUALEDIT
9359 virtual_op = MAYBE;
9360#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009361 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362}
9363
9364/*
9365 * ":put".
9366 */
9367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009368ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 /* ":0put" works like ":1put!". */
9371 if (eap->line2 == 0)
9372 {
9373 eap->line2 = 1;
9374 eap->forceit = TRUE;
9375 }
9376 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009377 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9378 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379}
9380
9381/*
9382 * Handle ":copy" and ":move".
9383 */
9384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009385ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 long n;
9388
Bram Moolenaarded27822017-01-02 14:27:34 +01009389 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 if (eap->arg == NULL) /* error detected */
9391 {
9392 eap->nextcmd = NULL;
9393 return;
9394 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009395 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396
9397 /*
9398 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9399 */
9400 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9401 {
9402 EMSG(_(e_invaddr));
9403 return;
9404 }
9405
9406 if (eap->cmdidx == CMD_move)
9407 {
9408 if (do_move(eap->line1, eap->line2, n) == FAIL)
9409 return;
9410 }
9411 else
9412 ex_copy(eap->line1, eap->line2, n);
9413 u_clearline();
9414 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009415 ex_may_print(eap);
9416}
9417
9418/*
9419 * Print the current line if flags were given to the Ex command.
9420 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009421 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009422ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009423{
9424 if (eap->flags != 0)
9425 {
9426 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9427 (eap->flags & EXFLAG_LIST));
9428 ex_no_reprint = TRUE;
9429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430}
9431
9432/*
9433 * ":smagic" and ":snomagic".
9434 */
9435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009436ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
9438 int magic_save = p_magic;
9439
9440 p_magic = (eap->cmdidx == CMD_smagic);
9441 do_sub(eap);
9442 p_magic = magic_save;
9443}
9444
9445/*
9446 * ":join".
9447 */
9448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009449ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451 curwin->w_cursor.lnum = eap->line1;
9452 if (eap->line1 == eap->line2)
9453 {
9454 if (eap->addr_count >= 2) /* :2,2join does nothing */
9455 return;
9456 if (eap->line2 == curbuf->b_ml.ml_line_count)
9457 {
9458 beep_flush();
9459 return;
9460 }
9461 ++eap->line2;
9462 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009463 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009465 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466}
9467
9468/*
9469 * ":[addr]@r" or ":[addr]*r": execute register
9470 */
9471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009472ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
9474 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009475 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476
9477 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009478 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
9480#ifdef USE_ON_FLY_SCROLL
9481 dont_scroll = TRUE; /* disallow scrolling here */
9482#endif
9483
9484 /* get the register name. No name means to use the previous one */
9485 c = *eap->arg;
9486 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9487 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009488 /* Put the register in the typeahead buffer with the "silent" flag. */
9489 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9490 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 else
9495 {
9496 int save_efr = exec_from_reg;
9497
9498 exec_from_reg = TRUE;
9499
9500 /*
9501 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009502 * Continue until the stuff buffer is empty and all added characters
9503 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009505 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9507
9508 exec_from_reg = save_efr;
9509 }
9510}
9511
9512/*
9513 * ":!".
9514 */
9515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009516ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9519}
9520
9521/*
9522 * ":undo".
9523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009525ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009527 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009528 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009529 else
9530 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531}
9532
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009533#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009535ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009536{
9537 char_u hash[UNDO_HASH_SIZE];
9538
9539 u_compute_hash(hash);
9540 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9541}
9542
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009544ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009545{
9546 char_u hash[UNDO_HASH_SIZE];
9547
9548 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009549 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009550}
9551#endif
9552
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553/*
9554 * ":redo".
9555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009557ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
9559 u_redo(1);
9560}
9561
9562/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009563 * ":earlier" and ":later".
9564 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009566ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009567{
9568 long count = 0;
9569 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009570 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009571 char_u *p = eap->arg;
9572
9573 if (*p == NUL)
9574 count = 1;
9575 else if (isdigit(*p))
9576 {
9577 count = getdigits(&p);
9578 switch (*p)
9579 {
9580 case 's': ++p; sec = TRUE; break;
9581 case 'm': ++p; sec = TRUE; count *= 60; break;
9582 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009583 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9584 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009585 }
9586 }
9587
9588 if (*p != NUL)
9589 EMSG2(_(e_invarg2), eap->arg);
9590 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009591 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9592 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009593}
9594
9595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 * ":redir": start/stop redirection.
9597 */
9598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009599ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
9601 char *mode;
9602 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009603 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604
Bram Moolenaarba768492016-07-08 15:32:54 +02009605#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009606 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009607 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009608 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009609 return;
9610 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009611#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009612
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 if (STRICMP(eap->arg, "END") == 0)
9614 close_redir();
9615 else
9616 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009617 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009619 ++arg;
9620 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009622 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 mode = "a";
9624 }
9625 else
9626 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009627 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628
9629 close_redir();
9630
9631 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009632 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 if (fname == NULL)
9634 return;
9635#ifdef FEAT_BROWSE
9636 if (cmdmod.browse)
9637 {
9638 char_u *browseFile;
9639
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009640 browseFile = do_browse(BROWSE_SAVE,
9641 (char_u *)_("Save Redirection"),
9642 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 if (browseFile == NULL)
9644 return; /* operation cancelled */
9645 vim_free(fname);
9646 fname = browseFile;
9647 eap->forceit = TRUE; /* since dialog already asked */
9648 }
9649#endif
9650
9651 redir_fd = open_exfile(fname, eap->forceit, mode);
9652 vim_free(fname);
9653 }
9654#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009655 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 {
9657 /* redirect to a register a-z (resp. A-Z for appending) */
9658 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009659 ++arg;
9660 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009662 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009663 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009665 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009667 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009668 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009669 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009672 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009673 if (*arg == '>')
9674 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009675 /* Make register empty when not using @A-@Z and the
9676 * command is valid. */
9677 if (*arg == NUL && !isupper(redir_reg))
9678 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009680 }
9681 if (*arg != NUL)
9682 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009683 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009684 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009685 }
9686 }
9687 else if (*arg == '=' && arg[1] == '>')
9688 {
9689 int append;
9690
9691 /* redirect to a variable */
9692 close_redir();
9693 arg += 2;
9694
9695 if (*arg == '>')
9696 {
9697 ++arg;
9698 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 }
9700 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009701 append = FALSE;
9702
9703 if (var_redir_start(skipwhite(arg), append) == OK)
9704 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 }
9706#endif
9707
9708 /* TODO: redirect to a buffer */
9709
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009711 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009713
9714 /* Make sure redirection is not off. Can happen for cmdline completion
9715 * that indirectly invokes a command to catch its output. */
9716 if (redir_fd != NULL
9717#ifdef FEAT_EVAL
9718 || redir_reg || redir_vname
9719#endif
9720 )
9721 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722}
9723
9724/*
9725 * ":redraw": force redraw
9726 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009727 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009728ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729{
9730 int r = RedrawingDisabled;
9731 int p = p_lz;
9732
9733 RedrawingDisabled = 0;
9734 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009735 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009737 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738#ifdef FEAT_TITLE
9739 if (need_maketitle)
9740 maketitle();
9741#endif
9742 RedrawingDisabled = r;
9743 p_lz = p;
9744
9745 /* Reset msg_didout, so that a message that's there is overwritten. */
9746 msg_didout = FALSE;
9747 msg_col = 0;
9748
Bram Moolenaar56667a52013-07-17 11:54:28 +02009749 /* No need to wait after an intentional redraw. */
9750 need_wait_return = FALSE;
9751
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 out_flush();
9753}
9754
9755/*
9756 * ":redrawstatus": force redraw of status line(s)
9757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009759ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 int r = RedrawingDisabled;
9762 int p = p_lz;
9763
9764 RedrawingDisabled = 0;
9765 p_lz = FALSE;
9766 if (eap->forceit)
9767 status_redraw_all();
9768 else
9769 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009770 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 RedrawingDisabled = r;
9772 p_lz = p;
9773 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774}
9775
9776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009777close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779 if (redir_fd != NULL)
9780 {
9781 fclose(redir_fd);
9782 redir_fd = NULL;
9783 }
9784#ifdef FEAT_EVAL
9785 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009786 if (redir_vname)
9787 {
9788 var_redir_stop();
9789 redir_vname = 0;
9790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791#endif
9792}
9793
9794#if defined(FEAT_SESSION) && defined(USE_CRNL)
9795# define MKSESSION_NL
9796static int mksession_nl = FALSE; /* use NL only in put_eol() */
9797#endif
9798
9799/*
9800 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9801 */
9802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009803ex_mkrc(
9804 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806 FILE *fd;
9807 int failed = FALSE;
9808 char_u *fname;
9809#ifdef FEAT_BROWSE
9810 char_u *browseFile = NULL;
9811#endif
9812#ifdef FEAT_SESSION
9813 int view_session = FALSE;
9814 int using_vdir = FALSE; /* using 'viewdir'? */
9815 char_u *viewFile = NULL;
9816 unsigned *flagp;
9817#endif
9818
9819 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9820 {
9821#ifdef FEAT_SESSION
9822 view_session = TRUE;
9823#else
9824 ex_ni(eap);
9825 return;
9826#endif
9827 }
9828
9829#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009830 /* Use the short file name until ":lcd" is used. We also don't use the
9831 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009832 did_lcd = FALSE;
9833
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9835 if (eap->cmdidx == CMD_mkview
9836 && (*eap->arg == NUL
9837 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9838 {
9839 eap->forceit = TRUE;
9840 fname = get_view_file(*eap->arg);
9841 if (fname == NULL)
9842 return;
9843 viewFile = fname;
9844 using_vdir = TRUE;
9845 }
9846 else
9847#endif
9848 if (*eap->arg != NUL)
9849 fname = eap->arg;
9850 else if (eap->cmdidx == CMD_mkvimrc)
9851 fname = (char_u *)VIMRC_FILE;
9852#ifdef FEAT_SESSION
9853 else if (eap->cmdidx == CMD_mksession)
9854 fname = (char_u *)SESSION_FILE;
9855#endif
9856 else
9857 fname = (char_u *)EXRC_FILE;
9858
9859#ifdef FEAT_BROWSE
9860 if (cmdmod.browse)
9861 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009862 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863# ifdef FEAT_SESSION
9864 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9865 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9866# endif
9867 (char_u *)_("Save Setup"),
9868 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9869 if (browseFile == NULL)
9870 goto theend;
9871 fname = browseFile;
9872 eap->forceit = TRUE; /* since dialog already asked */
9873 }
9874#endif
9875
9876#if defined(FEAT_SESSION) && defined(vim_mkdir)
9877 /* When using 'viewdir' may have to create the directory. */
9878 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009879 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880#endif
9881
9882 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9883 if (fd != NULL)
9884 {
9885#ifdef FEAT_SESSION
9886 if (eap->cmdidx == CMD_mkview)
9887 flagp = &vop_flags;
9888 else
9889 flagp = &ssop_flags;
9890#endif
9891
9892#ifdef MKSESSION_NL
9893 /* "unix" in 'sessionoptions': use NL line separator */
9894 if (view_session && (*flagp & SSOP_UNIX))
9895 mksession_nl = TRUE;
9896#endif
9897
9898 /* Write the version command for :mkvimrc */
9899 if (eap->cmdidx == CMD_mkvimrc)
9900 (void)put_line(fd, "version 6.0");
9901
9902#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009903 if (eap->cmdidx == CMD_mksession)
9904 {
9905 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9906 failed = TRUE;
9907 }
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 if (eap->cmdidx != CMD_mkview)
9910#endif
9911 {
9912 /* Write setting 'compatible' first, because it has side effects.
9913 * For that same reason only do it when needed. */
9914 if (p_cp)
9915 (void)put_line(fd, "if !&cp | set cp | endif");
9916 else
9917 (void)put_line(fd, "if &cp | set nocp | endif");
9918 }
9919
9920#ifdef FEAT_SESSION
9921 if (!view_session
9922 || (eap->cmdidx == CMD_mksession
9923 && (*flagp & SSOP_OPTIONS)))
9924#endif
9925 failed |= (makemap(fd, NULL) == FAIL
9926 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9927
9928#ifdef FEAT_SESSION
9929 if (!failed && view_session)
9930 {
9931 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9932 failed = TRUE;
9933 if (eap->cmdidx == CMD_mksession)
9934 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009935 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936
Bram Moolenaard9462e32011-04-11 21:35:11 +02009937 dirnow = alloc(MAXPATHL);
9938 if (dirnow == NULL)
9939 failed = TRUE;
9940 else
9941 {
9942 /*
9943 * Change to session file's dir.
9944 */
9945 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009947 *dirnow = NUL;
9948 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9949 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009950 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009951 shorten_fnames(TRUE);
9952 }
9953 else if (*dirnow != NUL
9954 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9955 {
9956 if (mch_chdir((char *)globaldir) == 0)
9957 shorten_fnames(TRUE);
9958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959
Bram Moolenaard9462e32011-04-11 21:35:11 +02009960 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961
Bram Moolenaard9462e32011-04-11 21:35:11 +02009962 /* restore original dir */
9963 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009965 {
9966 if (mch_chdir((char *)dirnow) != 0)
9967 EMSG(_(e_prev_dir));
9968 shorten_fnames(TRUE);
9969 }
9970 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 }
9972 }
9973 else
9974 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009975 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9976 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 }
9978 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9979 == FAIL)
9980 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009981 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9982 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009983 if (eap->cmdidx == CMD_mksession)
9984 {
9985 if (put_line(fd, "unlet SessionLoad") == FAIL)
9986 failed = TRUE;
9987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 }
9989#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009990 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9991 failed = TRUE;
9992
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 failed |= fclose(fd);
9994
9995 if (failed)
9996 EMSG(_(e_write));
9997#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9998 else if (eap->cmdidx == CMD_mksession)
9999 {
10000 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010001 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002
Bram Moolenaard9462e32011-04-11 21:35:11 +020010003 tbuf = alloc(MAXPATHL);
10004 if (tbuf != NULL)
10005 {
10006 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10007 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10008 vim_free(tbuf);
10009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 }
10011#endif
10012#ifdef MKSESSION_NL
10013 mksession_nl = FALSE;
10014#endif
10015 }
10016
10017#ifdef FEAT_BROWSE
10018theend:
10019 vim_free(browseFile);
10020#endif
10021#ifdef FEAT_SESSION
10022 vim_free(viewFile);
10023#endif
10024}
10025
Bram Moolenaardf177f62005-02-22 08:39:57 +000010026#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10027 || defined(PROTO)
10028 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010029vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010030{
10031 if (vim_mkdir(name, prot) != 0)
10032 {
10033 EMSG2(_("E739: Cannot create directory: %s"), name);
10034 return FAIL;
10035 }
10036 return OK;
10037}
10038#endif
10039
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040/*
10041 * Open a file for writing for an Ex command, with some checks.
10042 * Return file descriptor, or NULL on failure.
10043 */
10044 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010045open_exfile(
10046 char_u *fname,
10047 int forceit,
10048 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
10050 FILE *fd;
10051
10052#ifdef UNIX
10053 /* with Unix it is possible to open a directory */
10054 if (mch_isdir(fname))
10055 {
10056 EMSG2(_(e_isadir2), fname);
10057 return NULL;
10058 }
10059#endif
10060 if (!forceit && *mode != 'a' && vim_fexists(fname))
10061 {
10062 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10063 return NULL;
10064 }
10065
10066 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10067 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10068
10069 return fd;
10070}
10071
10072/*
10073 * ":mark" and ":k".
10074 */
10075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010076ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077{
10078 pos_T pos;
10079
10080 if (*eap->arg == NUL) /* No argument? */
10081 EMSG(_(e_argreq));
10082 else if (eap->arg[1] != NUL) /* more than one character? */
10083 EMSG(_(e_trailing));
10084 else
10085 {
10086 pos = curwin->w_cursor; /* save curwin->w_cursor */
10087 curwin->w_cursor.lnum = eap->line2;
10088 beginline(BL_WHITE | BL_FIX);
10089 if (setmark(*eap->arg) == FAIL) /* set mark */
10090 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10091 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10092 }
10093}
10094
10095/*
10096 * Update w_topline, w_leftcol and the cursor position.
10097 */
10098 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010099update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 check_cursor(); /* put cursor on valid line */
10102 update_topline();
10103 if (!curwin->w_p_wrap)
10104 validate_cursor();
10105 update_curswant();
10106}
10107
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010109 * Save the current State and go to Normal mode.
10110 * Return TRUE if the typeahead could be saved.
10111 */
10112 int
10113save_current_state(save_state_T *sst)
10114{
10115 sst->save_msg_scroll = msg_scroll;
10116 sst->save_restart_edit = restart_edit;
10117 sst->save_msg_didout = msg_didout;
10118 sst->save_State = State;
10119 sst->save_insertmode = p_im;
10120 sst->save_finish_op = finish_op;
10121 sst->save_opcount = opcount;
10122
10123 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10124 restart_edit = 0; /* don't go to Insert mode */
10125 p_im = FALSE; /* don't use 'insertmode' */
10126
10127 /*
10128 * Save the current typeahead. This is required to allow using ":normal"
10129 * from an event handler and makes sure we don't hang when the argument
10130 * ends with half a command.
10131 */
10132 save_typeahead(&sst->tabuf);
10133 return sst->tabuf.typebuf_valid;
10134}
10135
10136 void
10137restore_current_state(save_state_T *sst)
10138{
10139 /* Restore the previous typeahead. */
10140 restore_typeahead(&sst->tabuf);
10141
10142 msg_scroll = sst->save_msg_scroll;
10143 restart_edit = sst->save_restart_edit;
10144 p_im = sst->save_insertmode;
10145 finish_op = sst->save_finish_op;
10146 opcount = sst->save_opcount;
10147 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10148
10149 /* Restore the state (needed when called from a function executed for
10150 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10151 State = sst->save_State;
10152#ifdef CURSOR_SHAPE
10153 ui_cursor_shape(); /* may show different cursor shape */
10154#endif
10155}
10156
10157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 * ":normal[!] {commands}": Execute normal mode commands.
10159 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010160 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010161ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010163 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164#ifdef FEAT_MBYTE
10165 char_u *arg = NULL;
10166 int l;
10167 char_u *p;
10168#endif
10169
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010170 if (ex_normal_lock > 0)
10171 {
10172 EMSG(_(e_secure));
10173 return;
10174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 if (ex_normal_busy >= p_mmd)
10176 {
10177 EMSG(_("E192: Recursive use of :normal too deep"));
10178 return;
10179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180
10181#ifdef FEAT_MBYTE
10182 /*
10183 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10184 * this for the K_SPECIAL leading byte, otherwise special keys will not
10185 * work.
10186 */
10187 if (has_mbyte)
10188 {
10189 int len = 0;
10190
10191 /* Count the number of characters to be escaped. */
10192 for (p = eap->arg; *p != NUL; ++p)
10193 {
10194# ifdef FEAT_GUI
10195 if (*p == CSI) /* leadbyte CSI */
10196 len += 2;
10197# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010198 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10200# ifdef FEAT_GUI
10201 || *p == CSI
10202# endif
10203 )
10204 len += 2;
10205 }
10206 if (len > 0)
10207 {
10208 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10209 if (arg != NULL)
10210 {
10211 len = 0;
10212 for (p = eap->arg; *p != NUL; ++p)
10213 {
10214 arg[len++] = *p;
10215# ifdef FEAT_GUI
10216 if (*p == CSI)
10217 {
10218 arg[len++] = KS_EXTRA;
10219 arg[len++] = (int)KE_CSI;
10220 }
10221# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010222 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 {
10224 arg[len++] = *++p;
10225 if (*p == K_SPECIAL)
10226 {
10227 arg[len++] = KS_SPECIAL;
10228 arg[len++] = KE_FILLER;
10229 }
10230# ifdef FEAT_GUI
10231 else if (*p == CSI)
10232 {
10233 arg[len++] = KS_EXTRA;
10234 arg[len++] = (int)KE_CSI;
10235 }
10236# endif
10237 }
10238 arg[len] = NUL;
10239 }
10240 }
10241 }
10242 }
10243#endif
10244
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010245 ++ex_normal_busy;
10246 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 {
10248 /*
10249 * Repeat the :normal command for each line in the range. When no
10250 * range given, execute it just once, without positioning the cursor
10251 * first.
10252 */
10253 do
10254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 if (eap->addr_count != 0)
10256 {
10257 curwin->w_cursor.lnum = eap->line1++;
10258 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010259 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 }
10261
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010262 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263#ifdef FEAT_MBYTE
10264 arg != NULL ? arg :
10265#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010266 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 }
10268 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10269 }
10270
10271 /* Might not return to the main loop when in an event handler. */
10272 update_topline_cursor();
10273
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010274 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010276#ifdef FEAT_MOUSE
10277 setmouse();
10278#endif
10279#ifdef CURSOR_SHAPE
10280 ui_cursor_shape(); /* may show different cursor shape */
10281#endif
10282
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283#ifdef FEAT_MBYTE
10284 vim_free(arg);
10285#endif
10286}
10287
10288/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010289 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 */
10291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010292ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010294 if (eap->forceit)
10295 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010296 /* cursor line can be zero on startup */
10297 if (!curwin->w_cursor.lnum)
10298 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010299 coladvance((colnr_T)MAXCOL);
10300 curwin->w_curswant = MAXCOL;
10301 curwin->w_set_curswant = FALSE;
10302 }
10303
Bram Moolenaara40c5002005-01-09 21:16:21 +000010304 /* Ignore the command when already in Insert mode. Inserting an
10305 * expression register that invokes a function can do this. */
10306 if (State & INSERT)
10307 return;
10308
Bram Moolenaar64969662005-12-14 21:59:55 +000010309 if (eap->cmdidx == CMD_startinsert)
10310 restart_edit = 'a';
10311 else if (eap->cmdidx == CMD_startreplace)
10312 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010314 restart_edit = 'V';
10315
10316 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010318 if (eap->cmdidx == CMD_startinsert)
10319 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 curwin->w_curswant = 0; /* avoid MAXCOL */
10321 }
10322}
10323
10324/*
10325 * ":stopinsert"
10326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010328ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
10330 restart_edit = 0;
10331 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010332 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010335/*
10336 * Execute normal mode command "cmd".
10337 * "remap" can be REMAP_NONE or REMAP_YES.
10338 */
10339 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010340exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010341{
Bram Moolenaar25281632016-01-21 23:32:32 +010010342 /* Stuff the argument into the typeahead buffer. */
10343 ins_typebuf(cmd, remap, 0, TRUE, silent);
10344 exec_normal(FALSE);
10345}
Bram Moolenaar25281632016-01-21 23:32:32 +010010346
Bram Moolenaar25281632016-01-21 23:32:32 +010010347/*
10348 * Execute normal_cmd() until there is no typeahead left.
10349 */
10350 void
10351exec_normal(int was_typed)
10352{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010353 oparg_T oa;
10354
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010355 clear_oparg(&oa);
10356 finish_op = FALSE;
Bram Moolenaar25281632016-01-21 23:32:32 +010010357 while ((!stuff_empty() || ((was_typed || !typebuf_typed())
10358 && typebuf.tb_len > 0)) && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010359 {
10360 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +010010361 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010362 }
10363}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010364
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365#ifdef FEAT_FIND_ID
10366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010367ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368{
10369 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10370 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10371 (linenr_T)1, (linenr_T)MAXLNUM);
10372}
10373
Bram Moolenaar4033c552017-09-16 20:54:51 +020010374#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375/*
10376 * ":psearch"
10377 */
10378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010379ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380{
10381 g_do_tagpreview = p_pvh;
10382 ex_findpat(eap);
10383 g_do_tagpreview = 0;
10384}
10385#endif
10386
10387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010388ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389{
10390 int whole = TRUE;
10391 long n;
10392 char_u *p;
10393 int action;
10394
10395 switch (cmdnames[eap->cmdidx].cmd_name[2])
10396 {
10397 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10398 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10399 action = ACTION_GOTO;
10400 else
10401 action = ACTION_SHOW;
10402 break;
10403 case 'i': /* ":ilist" and ":dlist" */
10404 action = ACTION_SHOW_ALL;
10405 break;
10406 case 'u': /* ":ijump" and ":djump" */
10407 action = ACTION_GOTO;
10408 break;
10409 default: /* ":isplit" and ":dsplit" */
10410 action = ACTION_SPLIT;
10411 break;
10412 }
10413
10414 n = 1;
10415 if (vim_isdigit(*eap->arg)) /* get count */
10416 {
10417 n = getdigits(&eap->arg);
10418 eap->arg = skipwhite(eap->arg);
10419 }
10420 if (*eap->arg == '/') /* Match regexp, not just whole words */
10421 {
10422 whole = FALSE;
10423 ++eap->arg;
10424 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10425 if (*p)
10426 {
10427 *p++ = NUL;
10428 p = skipwhite(p);
10429
10430 /* Check for trailing illegal characters */
10431 if (!ends_excmd(*p))
10432 eap->errmsg = e_trailing;
10433 else
10434 eap->nextcmd = check_nextcmd(p);
10435 }
10436 }
10437 if (!eap->skip)
10438 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10439 whole, !eap->forceit,
10440 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10441 n, action, eap->line1, eap->line2);
10442}
10443#endif
10444
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445
Bram Moolenaar4033c552017-09-16 20:54:51 +020010446#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447/*
10448 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10449 */
10450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010451ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010453 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10455}
10456
10457/*
10458 * ":pedit"
10459 */
10460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010461ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463 win_T *curwin_save = curwin;
10464
10465 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010466 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010467 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 do_exedit(eap, NULL);
10469 keep_help_flag = FALSE;
10470 if (curwin != curwin_save && win_valid(curwin_save))
10471 {
10472 /* Return cursor to where we were */
10473 validate_cursor();
10474 redraw_later(VALID);
10475 win_enter(curwin_save, TRUE);
10476 }
10477 g_do_tagpreview = 0;
10478}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
10481/*
10482 * ":stag", ":stselect" and ":stjump".
10483 */
10484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010485ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486{
10487 postponed_split = -1;
10488 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010489 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10491 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010492 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494
10495/*
10496 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10497 */
10498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010499ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10502}
10503
10504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010505ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
10507 int cmd;
10508
10509 switch (name[1])
10510 {
10511 case 'j': cmd = DT_JUMP; /* ":tjump" */
10512 break;
10513 case 's': cmd = DT_SELECT; /* ":tselect" */
10514 break;
10515 case 'p': cmd = DT_PREV; /* ":tprevious" */
10516 break;
10517 case 'N': cmd = DT_PREV; /* ":tNext" */
10518 break;
10519 case 'n': cmd = DT_NEXT; /* ":tnext" */
10520 break;
10521 case 'o': cmd = DT_POP; /* ":pop" */
10522 break;
10523 case 'f': /* ":tfirst" */
10524 case 'r': cmd = DT_FIRST; /* ":trewind" */
10525 break;
10526 case 'l': cmd = DT_LAST; /* ":tlast" */
10527 break;
10528 default: /* ":tag" */
10529#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010530 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010532 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 return;
10534 }
10535#endif
10536 cmd = DT_TAG;
10537 break;
10538 }
10539
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010540 if (name[0] == 'l')
10541 {
10542#ifndef FEAT_QUICKFIX
10543 ex_ni(eap);
10544 return;
10545#else
10546 cmd = DT_LTAG;
10547#endif
10548 }
10549
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10551 eap->forceit, TRUE);
10552}
10553
10554/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010555 * Check "str" for starting with a special cmdline variable.
10556 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10557 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10558 */
10559 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010560find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010561{
10562 int len;
10563 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010564 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010565 "%",
10566#define SPEC_PERC 0
10567 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010568#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010569 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010570#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010571 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010572#define SPEC_CCWORD (SPEC_CWORD + 1)
10573 "<cexpr>", /* expr under cursor */
10574#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010575 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010576#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010577 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010578#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010579 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010580#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010581#ifdef FEAT_AUTOCMD
10582 "<afile>", /* autocommand file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010583# define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010584 "<abuf>", /* autocommand buffer number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010585# define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010586 "<amatch>", /* autocommand match name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010587# define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010588#endif
10589#ifdef FEAT_CLIENTSERVER
10590 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010591# ifdef FEAT_AUTOCMD
Bram Moolenaar65f08472017-09-10 18:16:20 +020010592# define SPEC_CLIENT (SPEC_AMATCH + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010593# else
Bram Moolenaar65f08472017-09-10 18:16:20 +020010594# define SPEC_CLIENT (SPEC_SLNUM + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010595# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010596#endif
10597 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010598
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010599 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010600 {
10601 len = (int)STRLEN(spec_str[i]);
10602 if (STRNCMP(src, spec_str[i], len) == 0)
10603 {
10604 *usedlen = len;
10605 return i;
10606 }
10607 }
10608 return -1;
10609}
10610
10611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 * Evaluate cmdline variables.
10613 *
10614 * change '%' to curbuf->b_ffname
10615 * '#' to curwin->w_altfile
10616 * '<cword>' to word under the cursor
10617 * '<cWORD>' to WORD under the cursor
10618 * '<cfile>' to path name under the cursor
10619 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010620 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 * '<afile>' to file name for autocommand
10622 * '<abuf>' to buffer number for autocommand
10623 * '<amatch>' to matching name for autocommand
10624 *
10625 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10626 * "" for error without a message) and NULL is returned.
10627 * Returns an allocated string if a valid match was found.
10628 * Returns NULL if no match was found. "usedlen" then still contains the
10629 * number of characters to skip.
10630 */
10631 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010632eval_vars(
10633 char_u *src, /* pointer into commandline */
10634 char_u *srcstart, /* beginning of valid memory for src */
10635 int *usedlen, /* characters after src that are used */
10636 linenr_T *lnump, /* line number for :e command, or NULL */
10637 char_u **errormsg, /* pointer to error message */
10638 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010639 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640{
10641 int i;
10642 char_u *s;
10643 char_u *result;
10644 char_u *resultbuf = NULL;
10645 int resultlen;
10646 buf_T *buf;
10647 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10648 int spec_idx;
10649#ifdef FEAT_MODIFY_FNAME
10650 int skip_mod = FALSE;
10651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653
10654 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010655 if (escaped != NULL)
10656 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657
10658 /*
10659 * Check if there is something to do.
10660 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010661 spec_idx = find_cmdline_var(src, usedlen);
10662 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 {
10664 *usedlen = 1;
10665 return NULL;
10666 }
10667
10668 /*
10669 * Skip when preceded with a backslash "\%" and "\#".
10670 * Note: In "\\%" the % is also not recognized!
10671 */
10672 if (src > srcstart && src[-1] == '\\')
10673 {
10674 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010675 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 return NULL;
10677 }
10678
10679 /*
10680 * word or WORD under cursor
10681 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010682 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10683 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010685 resultlen = find_ident_under_cursor(&result,
10686 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10687 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10688 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 if (resultlen == 0)
10690 {
10691 *errormsg = (char_u *)"";
10692 return NULL;
10693 }
10694 }
10695
10696 /*
10697 * '#': Alternate file name
10698 * '%': Current file name
10699 * File name under the cursor
10700 * File name for autocommand
10701 * and following modifiers
10702 */
10703 else
10704 {
10705 switch (spec_idx)
10706 {
10707 case SPEC_PERC: /* '%': current file */
10708 if (curbuf->b_fname == NULL)
10709 {
10710 result = (char_u *)"";
10711 valid = 0; /* Must have ":p:h" to be valid */
10712 }
10713 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 break;
10716
10717 case SPEC_HASH: /* '#' or "#99": alternate file */
10718 if (src[1] == '#') /* "##": the argument list */
10719 {
10720 result = arg_all();
10721 resultbuf = result;
10722 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010723 if (escaped != NULL)
10724 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725#ifdef FEAT_MODIFY_FNAME
10726 skip_mod = TRUE;
10727#endif
10728 break;
10729 }
10730 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010731 if (*s == '<') /* "#<99" uses v:oldfiles */
10732 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010734 if (s == src + 2 && src[1] == '-')
10735 /* just a minus sign, don't skip over it */
10736 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 *usedlen = (int)(s - src); /* length of what we expand */
10738
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010739 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010741 if (*usedlen < 2)
10742 {
10743 /* Should we give an error message for #<text? */
10744 *usedlen = 1;
10745 return NULL;
10746 }
10747#ifdef FEAT_EVAL
10748 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10749 (long)i);
10750 if (result == NULL)
10751 {
10752 *errormsg = (char_u *)"";
10753 return NULL;
10754 }
10755#else
10756 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 }
10760 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010761 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010762 if (i == 0 && src[1] == '<' && *usedlen > 1)
10763 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010764 buf = buflist_findnr(i);
10765 if (buf == NULL)
10766 {
10767 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10768 return NULL;
10769 }
10770 if (lnump != NULL)
10771 *lnump = ECMD_LAST;
10772 if (buf->b_fname == NULL)
10773 {
10774 result = (char_u *)"";
10775 valid = 0; /* Must have ":p:h" to be valid */
10776 }
10777 else
10778 result = buf->b_fname;
10779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 break;
10781
10782#ifdef FEAT_SEARCHPATH
10783 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010784 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 if (result == NULL)
10786 {
10787 *errormsg = (char_u *)"";
10788 return NULL;
10789 }
10790 resultbuf = result; /* remember allocated string */
10791 break;
10792#endif
10793
10794#ifdef FEAT_AUTOCMD
10795 case SPEC_AFILE: /* file name for autocommand */
10796 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010797 if (result != NULL && !autocmd_fname_full)
10798 {
10799 /* Still need to turn the fname into a full path. It is
10800 * postponed to avoid a delay when <afile> is not used. */
10801 autocmd_fname_full = TRUE;
10802 result = FullName_save(autocmd_fname, FALSE);
10803 vim_free(autocmd_fname);
10804 autocmd_fname = result;
10805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (result == NULL)
10807 {
10808 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10809 return NULL;
10810 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010811 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 break;
10813
10814 case SPEC_ABUF: /* buffer number for autocommand */
10815 if (autocmd_bufnr <= 0)
10816 {
10817 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10818 return NULL;
10819 }
10820 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10821 result = strbuf;
10822 break;
10823
10824 case SPEC_AMATCH: /* match name for autocommand */
10825 result = autocmd_match;
10826 if (result == NULL)
10827 {
10828 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10829 return NULL;
10830 }
10831 break;
10832
10833#endif
10834 case SPEC_SFILE: /* file name for ":so" command */
10835 result = sourcing_name;
10836 if (result == NULL)
10837 {
10838 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10839 return NULL;
10840 }
10841 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010842 case SPEC_SLNUM: /* line in file for ":so" command */
10843 if (sourcing_name == NULL || sourcing_lnum == 0)
10844 {
10845 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10846 return NULL;
10847 }
10848 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10849 result = strbuf;
10850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851#if defined(FEAT_CLIENTSERVER)
10852 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010853 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10854 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 result = strbuf;
10856 break;
10857#endif
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010858 default:
10859 result = (char_u *)""; /* avoid gcc warning */
10860 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 }
10862
10863 resultlen = (int)STRLEN(result); /* length of new string */
10864 if (src[*usedlen] == '<') /* remove the file name extension */
10865 {
10866 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 resultlen = (int)(s - result);
10869 }
10870#ifdef FEAT_MODIFY_FNAME
10871 else if (!skip_mod)
10872 {
10873 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10874 &resultlen);
10875 if (result == NULL)
10876 {
10877 *errormsg = (char_u *)"";
10878 return NULL;
10879 }
10880 }
10881#endif
10882 }
10883
10884 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10885 {
10886 if (valid != VALID_HEAD + VALID_PATH)
10887 /* xgettext:no-c-format */
10888 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10889 else
10890 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10891 result = NULL;
10892 }
10893 else
10894 result = vim_strnsave(result, resultlen);
10895 vim_free(resultbuf);
10896 return result;
10897}
10898
10899/*
10900 * Concatenate all files in the argument list, separated by spaces, and return
10901 * it in one allocated string.
10902 * Spaces and backslashes in the file names are escaped with a backslash.
10903 * Returns NULL when out of memory.
10904 */
10905 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010906arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907{
10908 int len;
10909 int idx;
10910 char_u *retval = NULL;
10911 char_u *p;
10912
10913 /*
10914 * Do this loop two times:
10915 * first time: compute the total length
10916 * second time: concatenate the names
10917 */
10918 for (;;)
10919 {
10920 len = 0;
10921 for (idx = 0; idx < ARGCOUNT; ++idx)
10922 {
10923 p = alist_name(&ARGLIST[idx]);
10924 if (p != NULL)
10925 {
10926 if (len > 0)
10927 {
10928 /* insert a space in between names */
10929 if (retval != NULL)
10930 retval[len] = ' ';
10931 ++len;
10932 }
10933 for ( ; *p != NUL; ++p)
10934 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020010935 if (*p == ' '
10936#ifndef BACKSLASH_IN_FILENAME
10937 || *p == '\\'
10938#endif
10939 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 {
10941 /* insert a backslash */
10942 if (retval != NULL)
10943 retval[len] = '\\';
10944 ++len;
10945 }
10946 if (retval != NULL)
10947 retval[len] = *p;
10948 ++len;
10949 }
10950 }
10951 }
10952
10953 /* second time: break here */
10954 if (retval != NULL)
10955 {
10956 retval[len] = NUL;
10957 break;
10958 }
10959
10960 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010961 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 if (retval == NULL)
10963 break;
10964 }
10965
10966 return retval;
10967}
10968
10969#if defined(FEAT_AUTOCMD) || defined(PROTO)
10970/*
10971 * Expand the <sfile> string in "arg".
10972 *
10973 * Returns an allocated string, or NULL for any error.
10974 */
10975 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010976expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
10978 char_u *errormsg;
10979 int len;
10980 char_u *result;
10981 char_u *newres;
10982 char_u *repl;
10983 int srclen;
10984 char_u *p;
10985
10986 result = vim_strsave(arg);
10987 if (result == NULL)
10988 return NULL;
10989
10990 for (p = result; *p; )
10991 {
10992 if (STRNCMP(p, "<sfile>", 7) != 0)
10993 ++p;
10994 else
10995 {
10996 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010997 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 if (errormsg != NULL)
10999 {
11000 if (*errormsg)
11001 emsg(errormsg);
11002 vim_free(result);
11003 return NULL;
11004 }
11005 if (repl == NULL) /* no match (cannot happen) */
11006 {
11007 p += srclen;
11008 continue;
11009 }
11010 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11011 newres = alloc(len);
11012 if (newres == NULL)
11013 {
11014 vim_free(repl);
11015 vim_free(result);
11016 return NULL;
11017 }
11018 mch_memmove(newres, result, (size_t)(p - result));
11019 STRCPY(newres + (p - result), repl);
11020 len = (int)STRLEN(newres);
11021 STRCAT(newres, p + srclen);
11022 vim_free(repl);
11023 vim_free(result);
11024 result = newres;
11025 p = newres + len; /* continue after the match */
11026 }
11027 }
11028
11029 return result;
11030}
11031#endif
11032
11033#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011034static int ses_winsizes(FILE *fd, int restore_size,
11035 win_T *tab_firstwin);
11036static int ses_win_rec(FILE *fd, frame_T *fr);
11037static frame_T *ses_skipframe(frame_T *fr);
11038static int ses_do_frame(frame_T *fr);
11039static int ses_do_win(win_T *wp);
11040static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11041static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011042static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043
11044/*
11045 * Write openfile commands for the current buffers to an .exrc file.
11046 * Return FAIL on error, OK otherwise.
11047 */
11048 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011049makeopens(
11050 FILE *fd,
11051 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052{
11053 buf_T *buf;
11054 int only_save_windows = TRUE;
11055 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 int restore_size = TRUE;
11057 win_T *wp;
11058 char_u *sname;
11059 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011060 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011061 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011062 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011063 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011064 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011065 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066
11067 if (ssop_flags & SSOP_BUFFERS)
11068 only_save_windows = FALSE; /* Save ALL buffers */
11069
11070 /*
11071 * Begin by setting the this_session variable, and then other
11072 * sessionable variables.
11073 */
11074#ifdef FEAT_EVAL
11075 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11076 return FAIL;
11077 if (ssop_flags & SSOP_GLOBALS)
11078 if (store_session_globals(fd) == FAIL)
11079 return FAIL;
11080#endif
11081
11082 /*
11083 * Close all windows but one.
11084 */
11085 if (put_line(fd, "silent only") == FAIL)
11086 return FAIL;
11087
11088 /*
11089 * Now a :cd command to the session directory or the current directory
11090 */
11091 if (ssop_flags & SSOP_SESDIR)
11092 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011093 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11094 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 return FAIL;
11096 }
11097 else if (ssop_flags & SSOP_CURDIR)
11098 {
11099 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11100 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011101 || fputs("cd ", fd) < 0
11102 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11103 || put_eol(fd) == FAIL)
11104 {
11105 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 vim_free(sname);
11109 }
11110
11111 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011112 * If there is an empty, unnamed buffer we will wipe it out later.
11113 * Remember the buffer number.
11114 */
11115 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11116 return FAIL;
11117 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11118 return FAIL;
11119 if (put_line(fd, "endif") == FAIL)
11120 return FAIL;
11121
11122 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 * Now save the current files, current buffer first.
11124 */
11125 if (put_line(fd, "set shortmess=aoO") == FAIL)
11126 return FAIL;
11127
11128 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011129 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 {
11131 if (!(only_save_windows && buf->b_nwindows == 0)
11132 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
11133 && buf->b_fname != NULL
11134 && buf->b_p_bl)
11135 {
11136 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11137 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011138 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 return FAIL;
11140 }
11141 }
11142
11143 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011144 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11146 return FAIL;
11147
11148 if (ssop_flags & SSOP_RESIZE)
11149 {
11150 /* Note: after the restore we still check it worked!*/
11151 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11152 || put_eol(fd) == FAIL)
11153 return FAIL;
11154 }
11155
11156#ifdef FEAT_GUI
11157 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11158 {
11159 int x, y;
11160
11161 if (gui_mch_get_winpos(&x, &y) == OK)
11162 {
11163 /* Note: after the restore we still check it worked!*/
11164 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11165 return FAIL;
11166 }
11167 }
11168#endif
11169
11170 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011171 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11172 * will be displayed when creating the next tab. That resizes the windows
11173 * in the first tab, which may cause problems. Set 'showtabline' to 2
11174 * temporarily to avoid that.
11175 */
11176 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11177 {
11178 if (put_line(fd, "set stal=2") == FAIL)
11179 return FAIL;
11180 restore_stal = TRUE;
11181 }
11182
11183 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011184 * May repeat putting Windows for each tab, when "tabpages" is in
11185 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011186 * Don't use goto_tabpage(), it may change directory and trigger
11187 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011189 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011190 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011191 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 {
Bram Moolenaar695baee2015-04-13 12:39:22 +020011193 int need_tabnew = FALSE;
11194 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011195
Bram Moolenaar18144c82006-04-12 21:52:12 +000011196 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011198 tabpage_T *tp = find_tabpage(tabnr);
11199
11200 if (tp == NULL)
11201 break; /* done all tab pages */
11202 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011203 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011204 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011205 tab_topframe = topframe;
11206 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011207 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011208 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011209 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011210 tab_topframe = tp->tp_topframe;
11211 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011212 if (tabnr > 1)
11213 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215
Bram Moolenaar18144c82006-04-12 21:52:12 +000011216 /*
11217 * Before creating the window layout, try loading one file. If this
11218 * is aborted we don't end up with a number of useless windows.
11219 * This may have side effects! (e.g., compressed or network file).
11220 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011221 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011222 {
11223 if (ses_do_win(wp)
11224 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011225 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011226#ifdef FEAT_QUICKFIX
11227 && !bt_nofile(wp->w_buffer)
11228#endif
11229 )
11230 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011231 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011232 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11233 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011234 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011235 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011236 if (!wp->w_arg_idx_invalid)
11237 edited_win = wp;
11238 break;
11239 }
11240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011242 /* If no file got edited create an empty tab page. */
11243 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
11244 return FAIL;
11245
Bram Moolenaar18144c82006-04-12 21:52:12 +000011246 /*
11247 * Save current window layout.
11248 */
11249 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011251 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011253 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11254 return FAIL;
11255 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11256 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257
Bram Moolenaar18144c82006-04-12 21:52:12 +000011258 /*
11259 * Check if window sizes can be restored (no windows omitted).
11260 * Remember the window number of the current window after restoring.
11261 */
11262 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011263 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011264 {
11265 if (ses_do_win(wp))
11266 ++nr;
11267 else
11268 restore_size = FALSE;
11269 if (curwin == wp)
11270 cnr = nr;
11271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
Bram Moolenaar18144c82006-04-12 21:52:12 +000011273 /* Go to the first window. */
11274 if (put_line(fd, "wincmd t") == FAIL)
11275 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011276
Bram Moolenaar18144c82006-04-12 21:52:12 +000011277 /*
11278 * If more than one window, see if sizes can be restored.
11279 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11280 * resized when moving between windows.
11281 * Do this before restoring the view, so that the topline and the
11282 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011283 * winminheight and winminwidth need to be set to avoid an error if the
11284 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011285 */
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011286 if (put_line(fd, "set winminheight=1 winheight=1 winminwidth=1 winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011287 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011288 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290
Bram Moolenaar18144c82006-04-12 21:52:12 +000011291 /*
11292 * Restore the view of the window (options, file, cursor, etc.).
11293 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011294 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011295 {
11296 if (!ses_do_win(wp))
11297 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011298 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11299 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011300 return FAIL;
11301 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11302 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011303 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011304 }
11305
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011306 /* The argument index in the first tab page is zero, need to set it in
11307 * each window. For further tab pages it's the window where we do
11308 * "tabedit". */
11309 cur_arg_idx = next_arg_idx;
11310
Bram Moolenaar18144c82006-04-12 21:52:12 +000011311 /*
11312 * Restore cursor to the current window if it's not the first one.
11313 */
11314 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11315 || put_eol(fd) == FAIL))
11316 return FAIL;
11317
11318 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011319 * Restore window sizes again after jumping around in windows, because
11320 * the current window has a minimum size while others may not.
11321 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011322 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011323 return FAIL;
11324
Bram Moolenaar18144c82006-04-12 21:52:12 +000011325 /* Don't continue in another tab page when doing only the current one
11326 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011327 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011328 break;
11329 }
11330
11331 if (ssop_flags & SSOP_TABPAGES)
11332 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011333 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11334 || put_eol(fd) == FAIL)
11335 return FAIL;
11336 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011337 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11338 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011339
Bram Moolenaar9c102382006-05-03 21:26:49 +000011340 /*
11341 * Wipe out an empty unnamed buffer we started in.
11342 */
11343 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
11344 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011345 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011346 return FAIL;
11347 if (put_line(fd, "endif") == FAIL)
11348 return FAIL;
11349 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11350 return FAIL;
11351
11352 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11353 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11354 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11355 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011356 /* Re-apply 'winminheight' and 'winminwidth'. */
11357 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11358 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
11361 /*
11362 * Lastly, execute the x.vim file if it exists.
11363 */
11364 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11365 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011366 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 || put_line(fd, "endif") == FAIL)
11368 return FAIL;
11369
11370 return OK;
11371}
11372
11373 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011374ses_winsizes(
11375 FILE *fd,
11376 int restore_size,
11377 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378{
11379 int n = 0;
11380 win_T *wp;
11381
11382 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11383 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011384 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 {
11386 if (!ses_do_win(wp))
11387 continue;
11388 ++n;
11389
11390 /* restore height when not full height */
11391 if (wp->w_height + wp->w_status_height < topframe->fr_height
11392 && (fprintf(fd,
11393 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11394 n, (long)wp->w_height, Rows / 2, Rows) < 0
11395 || put_eol(fd) == FAIL))
11396 return FAIL;
11397
11398 /* restore width when not full width */
11399 if (wp->w_width < Columns && (fprintf(fd,
11400 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11401 n, (long)wp->w_width, Columns / 2, Columns) < 0
11402 || put_eol(fd) == FAIL))
11403 return FAIL;
11404 }
11405 }
11406 else
11407 {
11408 /* Just equalise window sizes */
11409 if (put_line(fd, "wincmd =") == FAIL)
11410 return FAIL;
11411 }
11412 return OK;
11413}
11414
11415/*
11416 * Write commands to "fd" to recursively create windows for frame "fr",
11417 * horizontally and vertically split.
11418 * After the commands the last window in the frame is the current window.
11419 * Returns FAIL when writing the commands to "fd" fails.
11420 */
11421 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011422ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423{
11424 frame_T *frc;
11425 int count = 0;
11426
11427 if (fr->fr_layout != FR_LEAF)
11428 {
11429 /* Find first frame that's not skipped and then create a window for
11430 * each following one (first frame is already there). */
11431 frc = ses_skipframe(fr->fr_child);
11432 if (frc != NULL)
11433 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11434 {
11435 /* Make window as big as possible so that we have lots of room
11436 * to split. */
11437 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11438 || put_line(fd, fr->fr_layout == FR_COL
11439 ? "split" : "vsplit") == FAIL)
11440 return FAIL;
11441 ++count;
11442 }
11443
11444 /* Go back to the first window. */
11445 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11446 ? "%dwincmd k" : "%dwincmd h", count) < 0
11447 || put_eol(fd) == FAIL))
11448 return FAIL;
11449
11450 /* Recursively create frames/windows in each window of this column or
11451 * row. */
11452 frc = ses_skipframe(fr->fr_child);
11453 while (frc != NULL)
11454 {
11455 ses_win_rec(fd, frc);
11456 frc = ses_skipframe(frc->fr_next);
11457 /* Go to next window. */
11458 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11459 return FAIL;
11460 }
11461 }
11462 return OK;
11463}
11464
11465/*
11466 * Skip frames that don't contain windows we want to save in the Session.
11467 * Returns NULL when there none.
11468 */
11469 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011470ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471{
11472 frame_T *frc;
11473
11474 for (frc = fr; frc != NULL; frc = frc->fr_next)
11475 if (ses_do_frame(frc))
11476 break;
11477 return frc;
11478}
11479
11480/*
11481 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11482 * the Session.
11483 */
11484 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011485ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
11487 frame_T *frc;
11488
11489 if (fr->fr_layout == FR_LEAF)
11490 return ses_do_win(fr->fr_win);
11491 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11492 if (ses_do_frame(frc))
11493 return TRUE;
11494 return FALSE;
11495}
11496
11497/*
11498 * Return non-zero if window "wp" is to be stored in the Session.
11499 */
11500 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011501ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502{
11503 if (wp->w_buffer->b_fname == NULL
11504#ifdef FEAT_QUICKFIX
11505 /* When 'buftype' is "nofile" can't restore the window contents. */
11506 || bt_nofile(wp->w_buffer)
11507#endif
11508 )
11509 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011510 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 return (ssop_flags & SSOP_HELP);
11512 return TRUE;
11513}
11514
11515/*
11516 * Write commands to "fd" to restore the view of a window.
11517 * Caller must make sure 'scrolloff' is zero.
11518 */
11519 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011520put_view(
11521 FILE *fd,
11522 win_T *wp,
11523 int add_edit, /* add ":edit" command to view */
11524 unsigned *flagp, /* vop_flags or ssop_flags */
11525 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011526 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527{
11528 win_T *save_curwin;
11529 int f;
11530 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011531 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532
11533 /* Always restore cursor position for ":mksession". For ":mkview" only
11534 * when 'viewoptions' contains "cursor". */
11535 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11536
11537 /*
11538 * Local argument list.
11539 */
11540 if (wp->w_alist == &global_alist)
11541 {
11542 if (put_line(fd, "argglobal") == FAIL)
11543 return FAIL;
11544 }
11545 else
11546 {
11547 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11548 flagp == &vop_flags
11549 || !(*flagp & SSOP_CURDIR)
11550 || wp->w_localdir != NULL, flagp) == FAIL)
11551 return FAIL;
11552 }
11553
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011554 /* Only when part of a session: restore the argument index. Some
11555 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011556 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011557 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011559 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560 || put_eol(fd) == FAIL)
11561 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011562 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 }
11564
11565 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011566 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 {
11568 /*
11569 * Load the file.
11570 */
11571 if (wp->w_buffer->b_ffname != NULL
11572#ifdef FEAT_QUICKFIX
11573 && !bt_nofile(wp->w_buffer)
11574#endif
11575 )
11576 {
11577 /*
11578 * Editing a file in this buffer: use ":edit file".
11579 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011580 *
11581 * Note, if a buffer for that file already exists, use :badd to
11582 * edit that buffer, to not lose folding information (:edit resets
11583 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011585 if (fputs("if bufexists('", fd) < 0
11586 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11587 || fputs("') | buffer ", fd) < 0
11588 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11589 || fputs(" | else | edit ", fd) < 0
11590 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11591 || fputs(" | endif", fd) < 0
11592 ||
11593 put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 return FAIL;
11595 }
11596 else
11597 {
11598 /* No file in this buffer, just make it empty. */
11599 if (put_line(fd, "enew") == FAIL)
11600 return FAIL;
11601#ifdef FEAT_QUICKFIX
11602 if (wp->w_buffer->b_ffname != NULL)
11603 {
11604 /* The buffer does have a name, but it's not a file name. */
11605 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011606 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 return FAIL;
11608 }
11609#endif
11610 do_cursor = FALSE;
11611 }
11612 }
11613
11614 /*
11615 * Local mappings and abbreviations.
11616 */
11617 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11618 && makemap(fd, wp->w_buffer) == FAIL)
11619 return FAIL;
11620
11621 /*
11622 * Local options. Need to go to the window temporarily.
11623 * Store only local values when using ":mkview" and when ":mksession" is
11624 * used and 'sessionoptions' doesn't include "options".
11625 * Some folding options are always stored when "folds" is included,
11626 * otherwise the folds would not be restored correctly.
11627 */
11628 save_curwin = curwin;
11629 curwin = wp;
11630 curbuf = curwin->w_buffer;
11631 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11632 f = makeset(fd, OPT_LOCAL,
11633 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11634#ifdef FEAT_FOLDING
11635 else if (*flagp & SSOP_FOLDS)
11636 f = makefoldset(fd);
11637#endif
11638 else
11639 f = OK;
11640 curwin = save_curwin;
11641 curbuf = curwin->w_buffer;
11642 if (f == FAIL)
11643 return FAIL;
11644
11645#ifdef FEAT_FOLDING
11646 /*
11647 * Save Folds when 'buftype' is empty and for help files.
11648 */
11649 if ((*flagp & SSOP_FOLDS)
11650 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011651 && (*wp->w_buffer->b_p_bt == NUL || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 {
11653 if (put_folds(fd, wp) == FAIL)
11654 return FAIL;
11655 }
11656#endif
11657
11658 /*
11659 * Set the cursor after creating folds, since that moves the cursor.
11660 */
11661 if (do_cursor)
11662 {
11663
11664 /* Restore the cursor line in the file and relatively in the
11665 * window. Don't use "G", it changes the jumplist. */
11666 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11667 (long)wp->w_cursor.lnum,
11668 (long)(wp->w_cursor.lnum - wp->w_topline),
11669 (long)wp->w_height / 2, (long)wp->w_height) < 0
11670 || put_eol(fd) == FAIL
11671 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11672 || put_line(fd, "exe s:l") == FAIL
11673 || put_line(fd, "normal! zt") == FAIL
11674 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11675 || put_eol(fd) == FAIL)
11676 return FAIL;
11677 /* Restore the cursor column and left offset when not wrapping. */
11678 if (wp->w_cursor.col == 0)
11679 {
11680 if (put_line(fd, "normal! 0") == FAIL)
11681 return FAIL;
11682 }
11683 else
11684 {
11685 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11686 {
11687 if (fprintf(fd,
11688 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011689 (long)wp->w_virtcol + 1,
11690 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 (long)wp->w_width / 2, (long)wp->w_width) < 0
11692 || put_eol(fd) == FAIL
11693 || put_line(fd, "if s:c > 0") == FAIL
11694 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011695 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11696 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 || put_eol(fd) == FAIL
11698 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011699 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 || put_eol(fd) == FAIL
11701 || put_line(fd, "endif") == FAIL)
11702 return FAIL;
11703 }
11704 else
11705 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011706 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 || put_eol(fd) == FAIL)
11708 return FAIL;
11709 }
11710 }
11711 }
11712
11713 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011714 * Local directory, if the current flag is not view options or the "curdir"
11715 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011717 if (wp->w_localdir != NULL
11718 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 {
11720 if (fputs("lcd ", fd) < 0
11721 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11722 || put_eol(fd) == FAIL)
11723 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011724 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725 }
11726
11727 return OK;
11728}
11729
11730/*
11731 * Write an argument list to the session file.
11732 * Returns FAIL if writing fails.
11733 */
11734 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011735ses_arglist(
11736 FILE *fd,
11737 char *cmd,
11738 garray_T *gap,
11739 int fullname, /* TRUE: use full path name */
11740 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741{
11742 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011743 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 char_u *s;
11745
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011746 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11747 return FAIL;
11748 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 return FAIL;
11750 for (i = 0; i < gap->ga_len; ++i)
11751 {
11752 /* NULL file names are skipped (only happens when out of memory). */
11753 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11754 if (s != NULL)
11755 {
11756 if (fullname)
11757 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011758 buf = alloc(MAXPATHL);
11759 if (buf != NULL)
11760 {
11761 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11762 s = buf;
11763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011765 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011766 || ses_put_fname(fd, s, flagp) == FAIL
11767 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011768 {
11769 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011771 }
11772 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 }
11774 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011775 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776}
11777
11778/*
11779 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011780 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 * Returns FAIL if writing fails.
11782 */
11783 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011784ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785{
11786 char_u *name;
11787
11788 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011789 * the session file will be sourced.
11790 * Don't do this for ":mkview", we don't know the current directory.
11791 * Don't do this after ":lcd", we don't keep track of what the current
11792 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 if (buf->b_sfname != NULL
11794 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011795 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011796#ifdef FEAT_AUTOCHDIR
11797 && !p_acd
11798#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011799 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 name = buf->b_sfname;
11801 else
11802 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011803 if (ses_put_fname(fd, name, flagp) == FAIL
11804 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 return FAIL;
11806 return OK;
11807}
11808
11809/*
11810 * Write a file name to the session file.
11811 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11812 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011813 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 */
11815 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011816ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817{
11818 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011819 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821
11822 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011823 if (sname == NULL)
11824 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011826 if (*flagp & SSOP_SLASH)
11827 {
11828 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011829 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011830 if (*p == '\\')
11831 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011833
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011834 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011835 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011837 if (p == NULL)
11838 return FAIL;
11839
11840 /* write the result */
11841 if (fputs((char *)p, fd) < 0)
11842 retval = FAIL;
11843
11844 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 return retval;
11846}
11847
11848/*
11849 * ":loadview [nr]"
11850 */
11851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011852ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
11854 char_u *fname;
11855
11856 fname = get_view_file(*eap->arg);
11857 if (fname != NULL)
11858 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011859 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 vim_free(fname);
11861 }
11862}
11863
11864/*
11865 * Get the name of the view file for the current buffer.
11866 */
11867 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011868get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869{
11870 int len = 0;
11871 char_u *p, *s;
11872 char_u *retval;
11873 char_u *sname;
11874
11875 if (curbuf->b_ffname == NULL)
11876 {
11877 EMSG(_(e_noname));
11878 return NULL;
11879 }
11880 sname = home_replace_save(NULL, curbuf->b_ffname);
11881 if (sname == NULL)
11882 return NULL;
11883
11884 /*
11885 * We want a file name without separators, because we're not going to make
11886 * a directory.
11887 * "normal" path separator -> "=+"
11888 * "=" -> "=="
11889 * ":" path separator -> "=-"
11890 */
11891 for (p = sname; *p; ++p)
11892 if (*p == '=' || vim_ispathsep(*p))
11893 ++len;
11894 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11895 if (retval != NULL)
11896 {
11897 STRCPY(retval, p_vdir);
11898 add_pathsep(retval);
11899 s = retval + STRLEN(retval);
11900 for (p = sname; *p; ++p)
11901 {
11902 if (*p == '=')
11903 {
11904 *s++ = '=';
11905 *s++ = '=';
11906 }
11907 else if (vim_ispathsep(*p))
11908 {
11909 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011910#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 if (*p == ':')
11912 *s++ = '-';
11913 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011915 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 }
11917 else
11918 *s++ = *p;
11919 }
11920 *s++ = '=';
11921 *s++ = c;
11922 STRCPY(s, ".vim");
11923 }
11924
11925 vim_free(sname);
11926 return retval;
11927}
11928
11929#endif /* FEAT_SESSION */
11930
11931/*
11932 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11933 * Return FAIL for a write error.
11934 */
11935 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011936put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937{
11938 if (
11939#ifdef USE_CRNL
11940 (
11941# ifdef MKSESSION_NL
11942 !mksession_nl &&
11943# endif
11944 (putc('\r', fd) < 0)) ||
11945#endif
11946 (putc('\n', fd) < 0))
11947 return FAIL;
11948 return OK;
11949}
11950
11951/*
11952 * Write a line to "fd".
11953 * Return FAIL for a write error.
11954 */
11955 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011956put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957{
11958 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11959 return FAIL;
11960 return OK;
11961}
11962
11963#ifdef FEAT_VIMINFO
11964/*
11965 * ":rviminfo" and ":wviminfo".
11966 */
11967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011968ex_viminfo(
11969 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970{
11971 char_u *save_viminfo;
11972
11973 save_viminfo = p_viminfo;
11974 if (*p_viminfo == NUL)
11975 p_viminfo = (char_u *)"'100";
11976 if (eap->cmdidx == CMD_rviminfo)
11977 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011978 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11979 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 EMSG(_("E195: Cannot open viminfo file for reading"));
11981 }
11982 else
11983 write_viminfo(eap->arg, eap->forceit);
11984 p_viminfo = save_viminfo;
11985}
11986#endif
11987
11988#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011989/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011990 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011991 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011994dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 if (fname == NULL)
11997 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011998 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999}
12000#endif
12001
12002/*
12003 * ":behave {mswin,xterm}"
12004 */
12005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012006ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007{
12008 if (STRCMP(eap->arg, "mswin") == 0)
12009 {
12010 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12011 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12012 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12013 set_option_value((char_u *)"keymodel", 0L,
12014 (char_u *)"startsel,stopsel", 0);
12015 }
12016 else if (STRCMP(eap->arg, "xterm") == 0)
12017 {
12018 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12019 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12020 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12021 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12022 }
12023 else
12024 EMSG2(_(e_invarg2), eap->arg);
12025}
12026
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012027#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12028/*
12029 * Function given to ExpandGeneric() to obtain the possible arguments of the
12030 * ":behave {mswin,xterm}" command.
12031 */
12032 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012033get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012034{
12035 if (idx == 0)
12036 return (char_u *)"mswin";
12037 if (idx == 1)
12038 return (char_u *)"xterm";
12039 return NULL;
12040}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012041
12042/*
12043 * Function given to ExpandGeneric() to obtain the possible arguments of the
12044 * ":messages {clear}" command.
12045 */
12046 char_u *
12047get_messages_arg(expand_T *xp UNUSED, int idx)
12048{
12049 if (idx == 0)
12050 return (char_u *)"clear";
12051 return NULL;
12052}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012053#endif
12054
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012055 char_u *
12056get_mapclear_arg(expand_T *xp UNUSED, int idx)
12057{
12058 if (idx == 0)
12059 return (char_u *)"<buffer>";
12060 return NULL;
12061}
12062
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063#ifdef FEAT_AUTOCMD
12064static int filetype_detect = FALSE;
12065static int filetype_plugin = FALSE;
12066static int filetype_indent = FALSE;
12067
12068/*
12069 * ":filetype [plugin] [indent] {on,off,detect}"
12070 * on: Load the filetype.vim file to install autocommands for file types.
12071 * off: Load the ftoff.vim file to remove all autocommands for file types.
12072 * plugin on: load filetype.vim and ftplugin.vim
12073 * plugin off: load ftplugof.vim
12074 * indent on: load filetype.vim and indent.vim
12075 * indent off: load indoff.vim
12076 */
12077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012078ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079{
12080 char_u *arg = eap->arg;
12081 int plugin = FALSE;
12082 int indent = FALSE;
12083
12084 if (*eap->arg == NUL)
12085 {
12086 /* Print current status. */
12087 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12088 filetype_detect ? "ON" : "OFF",
12089 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12090 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12091 return;
12092 }
12093
12094 /* Accept "plugin" and "indent" in any order. */
12095 for (;;)
12096 {
12097 if (STRNCMP(arg, "plugin", 6) == 0)
12098 {
12099 plugin = TRUE;
12100 arg = skipwhite(arg + 6);
12101 continue;
12102 }
12103 if (STRNCMP(arg, "indent", 6) == 0)
12104 {
12105 indent = TRUE;
12106 arg = skipwhite(arg + 6);
12107 continue;
12108 }
12109 break;
12110 }
12111 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12112 {
12113 if (*arg == 'o' || !filetype_detect)
12114 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012115 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 filetype_detect = TRUE;
12117 if (plugin)
12118 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012119 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120 filetype_plugin = TRUE;
12121 }
12122 if (indent)
12123 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012124 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 filetype_indent = TRUE;
12126 }
12127 }
12128 if (*arg == 'd')
12129 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012130 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012131 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 }
12133 }
12134 else if (STRCMP(arg, "off") == 0)
12135 {
12136 if (plugin || indent)
12137 {
12138 if (plugin)
12139 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012140 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 filetype_plugin = FALSE;
12142 }
12143 if (indent)
12144 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012145 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 filetype_indent = FALSE;
12147 }
12148 }
12149 else
12150 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012151 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 filetype_detect = FALSE;
12153 }
12154 }
12155 else
12156 EMSG2(_(e_invarg2), arg);
12157}
12158
12159/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012160 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 */
12162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012163ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164{
12165 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012166 {
12167 char_u *arg = eap->arg;
12168
12169 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12170 arg += 9;
12171
12172 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12173 if (arg != eap->arg)
12174 did_filetype = FALSE;
12175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176}
12177#endif
12178
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012180ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181{
12182#ifdef FEAT_DIGRAPHS
12183 if (*eap->arg != NUL)
12184 putdigraph(eap->arg);
12185 else
12186 listdigraphs();
12187#else
12188 EMSG(_("E196: No digraphs in this version"));
12189#endif
12190}
12191
12192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012193ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
12195 int flags = 0;
12196
12197 if (eap->cmdidx == CMD_setlocal)
12198 flags = OPT_LOCAL;
12199 else if (eap->cmdidx == CMD_setglobal)
12200 flags = OPT_GLOBAL;
12201#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
12202 if (cmdmod.browse && flags == 0)
12203 ex_options(eap);
12204 else
12205#endif
12206 (void)do_set(eap->arg, flags);
12207}
12208
12209#ifdef FEAT_SEARCH_EXTRA
12210/*
12211 * ":nohlsearch"
12212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012214ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010012216 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012217 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218}
12219
12220/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012221 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 * Sets nextcmd to the start of the next command, if any. Also called when
12223 * skipping commands to find the next command.
12224 */
12225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012226ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227{
12228 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012229 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 char_u *end;
12231 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012232 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012233
12234 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012235 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012236 else
12237 {
12238 EMSG(e_invcmd);
12239 return;
12240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241
12242 /* First clear any old pattern. */
12243 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012244 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245
12246 if (ends_excmd(*eap->arg))
12247 end = eap->arg;
12248 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012249 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 end = eap->arg + 4;
12251 else
12252 {
12253 p = skiptowhite(eap->arg);
12254 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012255 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 p = skipwhite(p);
12257 if (*p == NUL)
12258 {
12259 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012260 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 EMSG2(_(e_invarg2), eap->arg);
12262 return;
12263 }
12264 end = skip_regexp(p + 1, *p, TRUE, NULL);
12265 if (!eap->skip)
12266 {
12267 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12268 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012269 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 eap->errmsg = e_trailing;
12271 return;
12272 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012273 if (*end != *p)
12274 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012275 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012276 EMSG2(_(e_invarg2), p);
12277 return;
12278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279
12280 c = *end;
12281 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012282 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012283 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012284 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 }
12286 }
12287 eap->nextcmd = find_nextcmd(end);
12288}
12289#endif
12290
12291#ifdef FEAT_CRYPT
12292/*
12293 * ":X": Get crypt key
12294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012296ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012298 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012299 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300}
12301#endif
12302
12303#ifdef FEAT_FOLDING
12304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012305ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306{
12307 if (foldManualAllowed(TRUE))
12308 foldCreate(eap->line1, eap->line2);
12309}
12310
12311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012312ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313{
12314 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12315 eap->forceit, FALSE);
12316}
12317
12318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012319ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320{
12321 linenr_T lnum;
12322
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012323#ifdef FEAT_CLIPBOARD
12324 start_global_changes();
12325#endif
12326
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 /* First set the marks for all lines closed/open. */
12328 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12329 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12330 ml_setmarked(lnum);
12331
12332 /* Execute the command on the marked lines. */
12333 global_exe(eap->arg);
12334 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012335#ifdef FEAT_CLIPBOARD
12336 end_global_changes();
12337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338}
12339#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012340
12341# if defined(FEAT_TIMERS) || defined(PROTO)
12342 int
12343get_pressedreturn(void)
12344{
12345 return ex_pressedreturn;
12346}
12347
12348 void
12349set_pressedreturn(int val)
12350{
12351 ex_pressedreturn = val;
12352}
12353#endif