blob: 9040c0d0730a6bfb8fcb06b654b91c9ce24c14fe [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
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020032 sctx_T uc_script_ctx; /* SCTX where the command was defined */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010033# 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
64#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010065static 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 +000066#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010067static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static int if_level = 0; /* depth in :if */
69#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020070static void free_cmdmod(void);
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
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void ex_autocmd(exarg_T *eap);
85static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void ex_bunload(exarg_T *eap);
87static void ex_buffer(exarg_T *eap);
88static void ex_bmodified(exarg_T *eap);
89static void ex_bnext(exarg_T *eap);
90static void ex_bprevious(exarg_T *eap);
91static void ex_brewind(exarg_T *eap);
92static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static char_u *getargcmd(char_u **);
94static char_u *skip_cmd_arg(char_u *p, int rembs);
95static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#ifndef FEAT_QUICKFIX
97# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000098# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000099# define ex_cc ex_ni
100# define ex_cnext ex_ni
101# define ex_cfile ex_ni
102# define qf_list ex_ni
103# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200104# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000106# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200108#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109# define ex_cclose ex_ni
110# define ex_copen ex_ni
111# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +0200112# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000114#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
115# define ex_cexpr ex_ni
116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200118static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200120#if !defined(FEAT_PERL) \
121 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
122 || !defined(FEAT_TCL) \
123 || !defined(FEAT_RUBY) \
124 || !defined(FEAT_LUA) \
125 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000126# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static char_u *invalid_range(exarg_T *eap);
130static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000131#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000133#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100134static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
135static void ex_highlight(exarg_T *eap);
136static void ex_colorscheme(exarg_T *eap);
137static void ex_quit(exarg_T *eap);
138static void ex_cquit(exarg_T *eap);
139static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100140static void ex_close(exarg_T *eap);
141static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
142static void ex_only(exarg_T *eap);
143static void ex_resize(exarg_T *eap);
144static void ex_stag(exarg_T *eap);
145static void ex_tabclose(exarg_T *eap);
146static void ex_tabonly(exarg_T *eap);
147static void ex_tabnext(exarg_T *eap);
148static void ex_tabmove(exarg_T *eap);
149static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200150#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100151static void ex_pclose(exarg_T *eap);
152static void ex_ptag(exarg_T *eap);
153static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#else
155# define ex_pclose ex_ni
156# define ex_ptag ex_ni
157# define ex_pedit ex_ni
158#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100159static void ex_hide(exarg_T *eap);
160static void ex_stop(exarg_T *eap);
161static void ex_exit(exarg_T *eap);
162static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100164static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#else
166# define ex_goto ex_ni
167#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100168static void ex_shell(exarg_T *eap);
169static void ex_preserve(exarg_T *eap);
170static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100171static void ex_mode(exarg_T *eap);
172static void ex_wrongmodifier(exarg_T *eap);
173static void ex_find(exarg_T *eap);
174static void ex_open(exarg_T *eap);
175static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#ifndef FEAT_GUI
177# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100178static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
180#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100181static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#else
183# define ex_tearoff ex_ni
184#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100185#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
186 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100187static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#else
189# define ex_popup ex_ni
190#endif
191#ifndef FEAT_GUI_MSWIN
192# define ex_simalt ex_ni
193#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000194#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195# define gui_mch_find_dialog ex_ni
196# define gui_mch_replace_dialog ex_ni
197#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000198#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199# define ex_helpfind ex_ni
200#endif
201#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100202# define ex_cscope ex_ni
203# define ex_scscope ex_ni
204# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#endif
206#ifndef FEAT_SYN_HL
207# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200208# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000209#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100210#ifndef FEAT_EVAL
211# define ex_packadd ex_ni
212# define ex_packloadall ex_ni
213#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200214#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200215# define ex_syntime ex_ni
216#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000217#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000218# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000219# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000220# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000221# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000222# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000223#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200224#ifndef FEAT_PERSISTENT_UNDO
225# define ex_rundo ex_ni
226# define ex_wundo ex_ni
227#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200228#ifndef FEAT_LUA
229# define ex_lua ex_script_ni
230# define ex_luado ex_ni
231# define ex_luafile ex_ni
232#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000233#ifndef FEAT_MZSCHEME
234# define ex_mzscheme ex_script_ni
235# define ex_mzfile ex_ni
236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifndef FEAT_PERL
238# define ex_perl ex_script_ni
239# define ex_perldo ex_ni
240#endif
241#ifndef FEAT_PYTHON
242# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200243# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244# define ex_pyfile ex_ni
245#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200246#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200247# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200248# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200249# define ex_py3file ex_ni
250#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100251#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
252# define ex_pyx ex_script_ni
253# define ex_pyxdo ex_ni
254# define ex_pyxfile ex_ni
255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifndef FEAT_TCL
257# define ex_tcl ex_script_ni
258# define ex_tcldo ex_ni
259# define ex_tclfile ex_ni
260#endif
261#ifndef FEAT_RUBY
262# define ex_ruby ex_script_ni
263# define ex_rubydo ex_ni
264# define ex_rubyfile ex_ni
265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifndef FEAT_KEYMAP
267# define ex_loadkeymap ex_ni
268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static void ex_swapname(exarg_T *eap);
270static void ex_syncbind(exarg_T *eap);
271static void ex_read(exarg_T *eap);
272static void ex_pwd(exarg_T *eap);
273static void ex_equal(exarg_T *eap);
274static void ex_sleep(exarg_T *eap);
275static void do_exmap(exarg_T *eap, int isabbrev);
276static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100277static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000278#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100279static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280#else
281# define ex_winpos ex_ni
282#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100283static void ex_operators(exarg_T *eap);
284static void ex_put(exarg_T *eap);
285static void ex_copymove(exarg_T *eap);
286static void ex_submagic(exarg_T *eap);
287static void ex_join(exarg_T *eap);
288static void ex_at(exarg_T *eap);
289static void ex_bang(exarg_T *eap);
290static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200291#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100292static void ex_wundo(exarg_T *eap);
293static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200294#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100295static void ex_redo(exarg_T *eap);
296static void ex_later(exarg_T *eap);
297static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100298static void ex_redrawstatus(exarg_T *eap);
299static void close_redir(void);
300static void ex_mkrc(exarg_T *eap);
301static void ex_mark(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302#ifdef FEAT_USR_CMDS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100303static char_u *uc_fun_cmd(void);
304static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100306static void ex_startinsert(exarg_T *eap);
307static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static void ex_checkpath(exarg_T *eap);
310static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#else
312# define ex_findpat ex_ni
313# define ex_checkpath ex_ni
314#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200315#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#else
318# define ex_psearch ex_ni
319#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100320static void ex_tag(exarg_T *eap);
321static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#ifndef FEAT_EVAL
323# define ex_scriptnames ex_ni
324# define ex_finish ex_ni
325# define ex_echo ex_ni
326# define ex_echohl ex_ni
327# define ex_execute ex_ni
328# define ex_call ex_ni
329# define ex_if ex_ni
330# define ex_endif ex_ni
331# define ex_else ex_ni
332# define ex_while ex_ni
333# define ex_continue ex_ni
334# define ex_break ex_ni
335# define ex_endwhile ex_ni
336# define ex_throw ex_ni
337# define ex_try ex_ni
338# define ex_catch ex_ni
339# define ex_finally ex_ni
340# define ex_endtry ex_ni
341# define ex_endfunction ex_ni
342# define ex_let ex_ni
343# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000344# define ex_lockvar ex_ni
345# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346# define ex_function ex_ni
347# define ex_delfunction ex_ni
348# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000349# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100351static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static int makeopens(FILE *fd, char_u *dirnow);
354static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
355static void ex_loadview(exarg_T *eap);
356static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000357static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#else
359# define ex_loadview ex_ni
360#endif
361#ifndef FEAT_EVAL
362# define ex_compiler ex_ni
363#endif
364#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100365static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366#else
367# define ex_viminfo ex_ni
368#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100369static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100370static void ex_filetype(exarg_T *eap);
371static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000373# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define ex_diffpatch ex_ni
375# define ex_diffgetput ex_ni
376# define ex_diffsplit ex_ni
377# define ex_diffthis ex_ni
378# define ex_diffupdate ex_ni
379#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100380static void ex_digraphs(exarg_T *eap);
381static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100382#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383# define ex_options ex_ni
384#endif
385#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100386static void ex_nohlsearch(exarg_T *eap);
387static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#else
389# define ex_nohlsearch ex_ni
390# define ex_match ex_ni
391#endif
392#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100393static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#else
395# define ex_X ex_ni
396#endif
397#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100398static void ex_fold(exarg_T *eap);
399static void ex_foldopen(exarg_T *eap);
400static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#else
402# define ex_fold ex_ni
403# define ex_foldopen ex_ni
404# define ex_folddo ex_ni
405#endif
406#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
407 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
408# define ex_language ex_ni
409#endif
410#ifndef FEAT_SIGNS
411# define ex_sign ex_ni
412#endif
413#ifndef FEAT_SUN_WORKSHOP
414# define ex_wsverb ex_ni
415#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000416#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200417# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000418# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200419# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
422#ifndef FEAT_EVAL
423# define ex_debug ex_ni
424# define ex_breakadd ex_ni
425# define ex_debuggreedy ex_ni
426# define ex_breakdel ex_ni
427# define ex_breaklist ex_ni
428#endif
429
430#ifndef FEAT_CMDHIST
431# define ex_history ex_ni
432#endif
433#ifndef FEAT_JUMPLIST
434# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200435# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436# define ex_changes ex_ni
437#endif
438
Bram Moolenaar05159a02005-02-26 23:04:13 +0000439#ifndef FEAT_PROFILE
440# define ex_profile ex_ni
441#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200442#ifndef FEAT_TERMINAL
443# define ex_terminal ex_ni
444#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000445
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446/*
447 * Declare cmdnames[].
448 */
449#define DO_DECLARE_EXCMD
450#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200451#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100452
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453static char_u dollar_command[2] = {'$', 0};
454
455
456#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000457/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458typedef struct
459{
460 char_u *line; /* command line */
461 linenr_T lnum; /* sourcing_lnum of the line */
462} wcmd_T;
463
464/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000465 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000467 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000469struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 garray_T *lines_gap; /* growarray with line info */
472 int current_line; /* last read line from growarray */
473 int repeating; /* TRUE when looping a second time */
474 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100475 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 void *cookie;
477};
478
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100479static char_u *get_loop_line(int c, void *cookie, int indent);
480static int store_loop_line(garray_T *gap, char_u *line);
481static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000482
483/* Struct to save a few things while debugging. Used in do_cmdline() only. */
484struct dbg_stuff
485{
486 int trylevel;
487 int force_abort;
488 except_T *caught_stack;
489 char_u *vv_exception;
490 char_u *vv_throwpoint;
491 int did_emsg;
492 int got_int;
493 int did_throw;
494 int need_rethrow;
495 int check_cstack;
496 except_T *current_exception;
497};
498
Bram Moolenaared203462004-06-16 11:19:22 +0000499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100500save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000501{
502 dsp->trylevel = trylevel; trylevel = 0;
503 dsp->force_abort = force_abort; force_abort = FALSE;
504 dsp->caught_stack = caught_stack; caught_stack = NULL;
505 dsp->vv_exception = v_exception(NULL);
506 dsp->vv_throwpoint = v_throwpoint(NULL);
507
508 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
509 dsp->did_emsg = did_emsg; did_emsg = FALSE;
510 dsp->got_int = got_int; got_int = FALSE;
511 dsp->did_throw = did_throw; did_throw = FALSE;
512 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
513 dsp->check_cstack = check_cstack; check_cstack = FALSE;
514 dsp->current_exception = current_exception; current_exception = NULL;
515}
516
517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100518restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000519{
520 suppress_errthrow = FALSE;
521 trylevel = dsp->trylevel;
522 force_abort = dsp->force_abort;
523 caught_stack = dsp->caught_stack;
524 (void)v_exception(dsp->vv_exception);
525 (void)v_throwpoint(dsp->vv_throwpoint);
526 did_emsg = dsp->did_emsg;
527 got_int = dsp->got_int;
528 did_throw = dsp->did_throw;
529 need_rethrow = dsp->need_rethrow;
530 check_cstack = dsp->check_cstack;
531 current_exception = dsp->current_exception;
532}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533#endif
534
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535/*
536 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
537 * command is given.
538 */
539 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100540do_exmode(
541 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
543 int save_msg_scroll;
544 int prev_msg_row;
545 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100546 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547
548 if (improved)
549 exmode_active = EXMODE_VIM;
550 else
551 exmode_active = EXMODE_NORMAL;
552 State = NORMAL;
553
554 /* When using ":global /pat/ visual" and then "Q" we return to continue
555 * the :global command. */
556 if (global_busy)
557 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
559 save_msg_scroll = msg_scroll;
560 ++RedrawingDisabled; /* don't redisplay the window */
561 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#ifdef FEAT_GUI
563 /* Ignore scrollbar and mouse events in Ex mode */
564 ++hold_gui_events;
565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
567 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
568 while (exmode_active)
569 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000570 /* Check for a ":normal" command and no more characters left. */
571 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
572 {
573 exmode_active = FALSE;
574 break;
575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 msg_scroll = TRUE;
577 need_wait_return = FALSE;
578 ex_pressedreturn = FALSE;
579 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100580 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 prev_msg_row = msg_row;
582 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (improved)
584 {
585 cmdline_row = msg_row;
586 do_cmdline(NULL, getexline, NULL, 0);
587 }
588 else
589 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
590 lines_left = Rows - 1;
591
Bram Moolenaardf177f62005-02-22 08:39:57 +0000592 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100593 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000595 if (curbuf->b_ml.ml_flags & ML_EMPTY)
596 EMSG(_(e_emptybuf));
597 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000599 if (ex_pressedreturn)
600 {
601 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000602 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000603 msg_row = prev_msg_row;
604 if (prev_msg_row == Rows - 1)
605 msg_row--;
606 }
607 msg_col = 0;
608 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
609 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000612 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
613 {
614 if (curbuf->b_ml.ml_flags & ML_EMPTY)
615 EMSG(_(e_emptybuf));
616 else
617 EMSG(_("E501: At end-of-file"));
618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
620
621#ifdef FEAT_GUI
622 --hold_gui_events;
623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 --RedrawingDisabled;
625 --no_wait_return;
626 update_screen(CLEAR);
627 need_wait_return = FALSE;
628 msg_scroll = save_msg_scroll;
629}
630
631/*
632 * Execute a simple command line. Used for translated commands like "*".
633 */
634 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100635do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 return do_cmdline(cmd, NULL, NULL,
638 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
639}
640
641/*
642 * do_cmdline(): execute one Ex command line
643 *
644 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100645 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 * 2. Split up in parts separated with '|'.
647 *
648 * This function can be called recursively!
649 *
650 * flags:
651 * DOCMD_VERBOSE - The command will be included in the error message.
652 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100653 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 * DOCMD_KEYTYPED - Don't reset KeyTyped.
655 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
656 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
657 *
658 * return FAIL if cmdline could not be executed, OK otherwise
659 */
660 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100661do_cmdline(
662 char_u *cmdline,
663 char_u *(*fgetline)(int, void *, int),
664 void *cookie, /* argument for fgetline() */
665 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
667 char_u *next_cmdline; /* next cmd to execute */
668 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100669 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 static int recursive = 0; /* recursive depth */
671 int msg_didout_before_start = 0;
672 int count = 0; /* line number count */
673 int did_inc = FALSE; /* incremented RedrawingDisabled */
674 int retval = OK;
675#ifdef FEAT_EVAL
676 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000677 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 int current_line = 0; /* active line in lines_ga */
679 char_u *fname = NULL; /* function or script name */
680 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
681 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000682 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 int initial_trylevel;
684 struct msglist **saved_msg_list = NULL;
685 struct msglist *private_msg_list;
686
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100687 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100688 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000692 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695# define cmd_cookie cookie
696#endif
697 static int call_depth = 0; /* recursiveness */
698
699#ifdef FEAT_EVAL
700 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
701 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000702 * This ensures that the do_errthrow() call in do_one_cmd() does not
703 * combine the messages stored by an earlier invocation of do_one_cmd()
704 * with the command name of the later one. This would happen when
705 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 saved_msg_list = msg_list;
707 msg_list = &private_msg_list;
708 private_msg_list = NULL;
709#endif
710
711 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100712 * here. The value of 200 allows nested function calls, ":source", etc.
713 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100714 if (call_depth >= 200
715#ifdef FEAT_EVAL
716 && call_depth >= p_mfd
717#endif
718 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
720 EMSG(_("E169: Command too recursive"));
721#ifdef FEAT_EVAL
722 /* When converting to an exception, we do not include the command name
723 * since this is not an error of the specific command. */
724 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
725 msg_list = saved_msg_list;
726#endif
727 return FAIL;
728 }
729 ++call_depth;
730
731#ifdef FEAT_EVAL
732 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000733 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 cstack.cs_trylevel = 0;
735 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000736 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
738
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100742 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000743 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 ++ex_nesting_level;
745
746 /* Get the function or script name and the address where the next breakpoint
747 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000748 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
750 fname = func_name(real_cookie);
751 breakpoint = func_breakpoint(real_cookie);
752 dbg_tick = func_dbg_tick(real_cookie);
753 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 fname = sourcing_name;
757 breakpoint = source_breakpoint(real_cookie);
758 dbg_tick = source_dbg_tick(real_cookie);
759 }
760
761 /*
762 * Initialize "force_abort" and "suppress_errthrow" at the top level.
763 */
764 if (!recursive)
765 {
766 force_abort = FALSE;
767 suppress_errthrow = FALSE;
768 }
769
770 /*
771 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000772 * exception handling (used when debugging). Otherwise clear it to avoid
773 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000775 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000776 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000777 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200778 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 initial_trylevel = trylevel;
781
782 /*
783 * "did_throw" will be set to TRUE when an exception is being thrown.
784 */
785 did_throw = FALSE;
786#endif
787 /*
788 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * If force_abort is set, we cancel everything.
791 */
792 did_emsg = FALSE;
793
794 /*
795 * KeyTyped is only set when calling vgetc(). Reset it here when not
796 * calling vgetc() (sourced command lines).
797 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100798 if (!(flags & DOCMD_KEYTYPED)
799 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 KeyTyped = FALSE;
801
802 /*
803 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000804 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * - for multiple commands on one line, separated with '|'
806 * - when repeating until there are no more lines (for ":source")
807 */
808 next_cmdline = cmdline;
809 do
810 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813#endif
814
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000815 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (next_cmdline == NULL
817#ifdef FEAT_EVAL
818 && !force_abort
819 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
822 )
823 did_emsg = FALSE;
824
825 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000826 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 * 3. If a line is given: Make a copy, so we can mess with it.
829 */
830
831#ifdef FEAT_EVAL
832 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000833 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 /* Each '|' separated command is stored separately in lines_ga, to
836 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100837 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 /* Check if a function has returned or, unless it has an unclosed
840 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000844 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 func_line_end(real_cookie);
846# endif
847 if (func_has_ended(real_cookie))
848 {
849 retval = FAIL;
850 break;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000854 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100855 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856 script_line_end();
857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
862 retval = FAIL;
863 break;
864 }
865
866 /* If breakpoints have been added/deleted need to check for it. */
867 if (breakpoint != NULL && dbg_tick != NULL
868 && *dbg_tick != debug_tick)
869 {
870 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 fname, sourcing_lnum);
873 *dbg_tick = debug_tick;
874 }
875
876 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
877 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
878
879 /* Did we encounter a breakpoint? */
880 if (breakpoint != NULL && *breakpoint != 0
881 && *breakpoint <= sourcing_lnum)
882 {
883 dbg_breakpoint(fname, sourcing_lnum);
884 /* Find next breakpoint. */
885 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 fname, sourcing_lnum);
888 *dbg_tick = debug_tick;
889 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000891 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892 {
893 if (getline_is_func)
894 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896 script_line_start();
897 }
898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000901 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000903 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100904 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 * below, so that it stores lines in or reads them from
906 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000907 * while/for loop. */
908 cmd_getline = get_loop_line;
909 cmd_cookie = (void *)&cmd_loop_cookie;
910 cmd_loop_cookie.lines_gap = &lines_ga;
911 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100912 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000913 cmd_loop_cookie.cookie = cookie;
914 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916 else
917 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100918 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 cmd_cookie = cookie;
920 }
921#endif
922
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100923 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 if (next_cmdline == NULL)
925 {
926 /*
927 * Need to set msg_didout for the first line after an ":if",
928 * otherwise the ":if" will be overwritten.
929 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100930 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100932 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933#ifdef FEAT_EVAL
934 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
935#else
936 0
937#endif
938 )) == NULL)
939 {
940 /* Don't call wait_return for aborted command line. The NULL
941 * returned for the end of a sourced file or executed function
942 * doesn't do this. */
943 if (KeyTyped && !(flags & DOCMD_REPEAT))
944 need_wait_return = FALSE;
945 retval = FAIL;
946 break;
947 }
948 used_getline = TRUE;
949
950 /*
951 * Keep the first typed line. Clear it when more lines are typed.
952 */
953 if (flags & DOCMD_KEEPLINE)
954 {
955 vim_free(repeat_cmdline);
956 if (count == 0)
957 repeat_cmdline = vim_strsave(next_cmdline);
958 else
959 repeat_cmdline = NULL;
960 }
961 }
962
963 /* 3. Make a copy of the command so we can mess with it. */
964 else if (cmdline_copy == NULL)
965 {
966 next_cmdline = vim_strsave(next_cmdline);
967 if (next_cmdline == NULL)
968 {
969 EMSG(_(e_outofmem));
970 retval = FAIL;
971 break;
972 }
973 }
974 cmdline_copy = next_cmdline;
975
976#ifdef FEAT_EVAL
977 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000978 * Save the current line when inside a ":while" or ":for", and when
979 * the command looks like a ":while" or ":for", because we may need it
980 * later. When there is a '|' and another command, it is stored
981 * separately, because we need to be able to jump back to it from an
982 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000984 if (current_line == lines_ga.ga_len
985 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
989 retval = FAIL;
990 break;
991 }
992 }
993 did_endif = FALSE;
994#endif
995
996 if (count++ == 0)
997 {
998 /*
999 * All output from the commands is put below each other, without
1000 * waiting for a return. Don't do this when executing commands
1001 * from a script or when being called recursive (e.g. for ":e
1002 * +command file").
1003 */
1004 if (!(flags & DOCMD_NOWAIT) && !recursive)
1005 {
1006 msg_didout_before_start = msg_didout;
1007 msg_didany = FALSE; /* no output yet */
1008 msg_start();
1009 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001010 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 ++RedrawingDisabled;
1012 did_inc = TRUE;
1013 }
1014 }
1015
1016 if (p_verbose >= 15 && sourcing_name != NULL)
1017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001019 verbose_enter_scroll();
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 smsg((char_u *)_("line %ld: %s"),
1022 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001023 if (msg_silent == 0)
1024 msg_puts((char_u *)"\n"); /* don't overwrite this */
1025
1026 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 --no_wait_return;
1028 }
1029
1030 /*
1031 * 2. Execute one '|' separated command.
1032 * do_one_cmd() will return NULL if there is no trailing '|'.
1033 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1034 */
1035 ++recursive;
1036 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1037#ifdef FEAT_EVAL
1038 &cstack,
1039#endif
1040 cmd_getline, cmd_cookie);
1041 --recursive;
1042
1043#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 if (cmd_cookie == (void *)&cmd_loop_cookie)
1045 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#endif
1049
1050 if (next_cmdline == NULL)
1051 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001052 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053#ifdef FEAT_CMDHIST
1054 /*
1055 * If the command was typed, remember it for the ':' register.
1056 * Do this AFTER executing the command to make :@: work.
1057 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001058 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 && new_last_cmdline != NULL)
1060 {
1061 vim_free(last_cmdline);
1062 last_cmdline = new_last_cmdline;
1063 new_last_cmdline = NULL;
1064 }
1065#endif
1066 }
1067 else
1068 {
1069 /* need to copy the command after the '|' to cmdline_copy, for the
1070 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001071 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 next_cmdline = cmdline_copy;
1073 }
1074
1075
1076#ifdef FEAT_EVAL
1077 /* reset did_emsg for a function that is not aborted by an error */
1078 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001079 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 && !func_has_abort(real_cookie))
1081 did_emsg = FALSE;
1082
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
1085 ++current_line;
1086
1087 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001088 * An ":endwhile", ":endfor" and ":continue" is handled here.
1089 * If we were executing commands, jump back to the ":while" or
1090 * ":for".
1091 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 /* Jump back to the matching ":while" or ":for". Be careful
1098 * not to use a cs_line[] from an entry that isn't a ":while"
1099 * or ":for": It would make "current_line" invalid and can
1100 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (!did_emsg && !got_int && !did_throw
1102 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 && (cstack.cs_flags[cstack.cs_idx]
1104 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 && cstack.cs_line[cstack.cs_idx] >= 0
1106 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1107 {
1108 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 /* remember we jumped there */
1110 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 line_breakcheck(); /* check if CTRL-C typed */
1112
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 /* Check for the next breakpoint at or after the ":while"
1114 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 if (breakpoint != NULL)
1116 {
1117 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001118 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 fname,
1120 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1121 *dbg_tick = debug_tick;
1122 }
1123 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001126 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001128 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1129 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 }
1132
1133 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1140 }
1141 }
1142
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001143 /* Check for the next breakpoint after a watchexpression */
1144 if (breakpoint != NULL && has_watchexpr())
1145 {
1146 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1147 *dbg_tick = debug_tick;
1148 }
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 /*
1151 * When not inside any ":while" loop, clear remembered lines.
1152 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
1155 if (lines_ga.ga_len > 0)
1156 {
1157 sourcing_lnum =
1158 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1159 free_cmdlines(&lines_ga);
1160 }
1161 current_line = 0;
1162 }
1163
1164 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1166 * being restored at the ":endtry". Reset them here and set the
1167 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1168 * This includes the case where a missing ":endif", ":endwhile" or
1169 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 cstack.cs_lflags &= ~CSL_HAD_FINA;
1174 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1175 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 did_throw ? (void *)current_exception : NULL);
1177 did_emsg = got_int = did_throw = FALSE;
1178 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1179 }
1180
1181 /* Update global "trylevel" for recursive calls to do_cmdline() from
1182 * within this loop. */
1183 trylevel = initial_trylevel + cstack.cs_trylevel;
1184
1185 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001186 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 * files) is aborted because of an error, an interrupt, or an uncaught
1188 * exception, cancel everything. If it is left normally, reset
1189 * force_abort to get the non-EH compatible abortion behavior for
1190 * the rest of the script.
1191 */
1192 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1193 force_abort = FALSE;
1194
1195 /* Convert an interrupt to an exception if appropriate. */
1196 (void)do_intthrow(&cstack);
1197#endif /* FEAT_EVAL */
1198
1199 }
1200 /*
1201 * Continue executing command lines when:
1202 * - no CTRL-C typed, no aborting error, no exception thrown or try
1203 * conditionals need to be checked for executing finally clauses or
1204 * catching an interrupt exception
1205 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 * looping for ":source" command or function call.
1208 */
1209 while (!((got_int
1210#ifdef FEAT_EVAL
1211 || (did_emsg && force_abort) || did_throw
1212#endif
1213 )
1214#ifdef FEAT_EVAL
1215 && cstack.cs_trylevel == 0
1216#endif
1217 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001218 && !(did_emsg
1219#ifdef FEAT_EVAL
1220 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001221 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001222 * the :endtry to be missed. */
1223 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1224#endif
1225 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001226 && (getline_equal(fgetline, cookie, getexmodeline)
1227 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 && (next_cmdline != NULL
1229#ifdef FEAT_EVAL
1230 || cstack.cs_idx >= 0
1231#endif
1232 || (flags & DOCMD_REPEAT)));
1233
1234 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001235 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236#ifdef FEAT_EVAL
1237 free_cmdlines(&lines_ga);
1238 ga_clear(&lines_ga);
1239
1240 if (cstack.cs_idx >= 0)
1241 {
1242 /*
1243 * If a sourced file or executed function ran to its end, report the
1244 * unclosed conditional.
1245 */
1246 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001247 && ((getline_equal(fgetline, cookie, getsourceline)
1248 && !source_finished(fgetline, cookie))
1249 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 && !func_has_ended(real_cookie))))
1251 {
1252 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1253 EMSG(_(e_endtry));
1254 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1255 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001256 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1257 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 else
1259 EMSG(_(e_endif));
1260 }
1261
1262 /*
1263 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1264 * ":endtry" in a sourced file or executed function. If the try
1265 * conditional is in its finally clause, ignore anything pending.
1266 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001267 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 */
1269 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001270 {
1271 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1272
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001273 if (idx >= 0)
1274 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001275 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1276 &cstack.cs_looplevel);
1277 }
1278 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 trylevel = initial_trylevel;
1280 }
1281
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001282 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1283 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001285 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 ? (char_u *)"endfunction" : (char_u *)NULL);
1287
1288 if (trylevel == 0)
1289 {
1290 /*
1291 * When an exception is being thrown out of the outermost try
1292 * conditional, discard the uncaught exception, disable the conversion
1293 * of interrupts or errors to exceptions, and ensure that no more
1294 * commands are executed.
1295 */
1296 if (did_throw)
1297 {
1298 void *p = NULL;
1299 char_u *saved_sourcing_name;
1300 int saved_sourcing_lnum;
1301 struct msglist *messages = NULL, *next;
1302
1303 /*
1304 * If the uncaught exception is a user exception, report it as an
1305 * error. If it is an error exception, display the saved error
1306 * message now. For an interrupt exception, do nothing; the
1307 * interrupt message is given elsewhere.
1308 */
1309 switch (current_exception->type)
1310 {
1311 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001312 vim_snprintf((char *)IObuff, IOSIZE,
1313 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 current_exception->value);
1315 p = vim_strsave(IObuff);
1316 break;
1317 case ET_ERROR:
1318 messages = current_exception->messages;
1319 current_exception->messages = NULL;
1320 break;
1321 case ET_INTERRUPT:
1322 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324
1325 saved_sourcing_name = sourcing_name;
1326 saved_sourcing_lnum = sourcing_lnum;
1327 sourcing_name = current_exception->throw_name;
1328 sourcing_lnum = current_exception->throw_lnum;
1329 current_exception->throw_name = NULL;
1330
1331 discard_current_exception(); /* uses IObuff if 'verbose' */
1332 suppress_errthrow = TRUE;
1333 force_abort = TRUE;
1334
1335 if (messages != NULL)
1336 {
1337 do
1338 {
1339 next = messages->next;
1340 emsg(messages->msg);
1341 vim_free(messages->msg);
1342 vim_free(messages);
1343 messages = next;
1344 }
1345 while (messages != NULL);
1346 }
1347 else if (p != NULL)
1348 {
1349 emsg(p);
1350 vim_free(p);
1351 }
1352 vim_free(sourcing_name);
1353 sourcing_name = saved_sourcing_name;
1354 sourcing_lnum = saved_sourcing_lnum;
1355 }
1356
1357 /*
1358 * On an interrupt or an aborting error not converted to an exception,
1359 * disable the conversion of errors to exceptions. (Interrupts are not
1360 * converted any more, here.) This enables also the interrupt message
1361 * when force_abort is set and did_emsg unset in case of an interrupt
1362 * from a finally clause after an error.
1363 */
1364 else if (got_int || (did_emsg && force_abort))
1365 suppress_errthrow = TRUE;
1366 }
1367
1368 /*
1369 * The current cstack will be freed when do_cmdline() returns. An uncaught
1370 * exception will have to be rethrown in the previous cstack. If a function
1371 * has just returned or a script file was just finished and the previous
1372 * cstack belongs to the same function or, respectively, script file, it
1373 * will have to be checked for finally clauses to be executed due to the
1374 * ":return" or ":finish". This is done in do_one_cmd().
1375 */
1376 if (did_throw)
1377 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001378 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001380 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 && ex_nesting_level > func_level(real_cookie) + 1))
1382 {
1383 if (!did_throw)
1384 check_cstack = TRUE;
1385 }
1386 else
1387 {
1388 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001389 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 --ex_nesting_level;
1391 /*
1392 * Go to debug mode when returning from a function in which we are
1393 * single-stepping.
1394 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001395 if ((getline_equal(fgetline, cookie, getsourceline)
1396 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001398 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 ? (char_u *)_("End of sourced file")
1400 : (char_u *)_("End of function"));
1401 }
1402
1403 /*
1404 * Restore the exception environment (done after returning from the
1405 * debugger).
1406 */
1407 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001408 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410 msg_list = saved_msg_list;
1411#endif /* FEAT_EVAL */
1412
1413 /*
1414 * If there was too much output to fit on the command line, ask the user to
1415 * hit return before redrawing the screen. With the ":global" command we do
1416 * this only once after the command is finished.
1417 */
1418 if (did_inc)
1419 {
1420 --RedrawingDisabled;
1421 --no_wait_return;
1422 msg_scroll = FALSE;
1423
1424 /*
1425 * When just finished an ":if"-":else" which was typed, no need to
1426 * wait for hit-return. Also for an error situation.
1427 */
1428 if (retval == FAIL
1429#ifdef FEAT_EVAL
1430 || (did_endif && KeyTyped && !did_emsg)
1431#endif
1432 )
1433 {
1434 need_wait_return = FALSE;
1435 msg_didany = FALSE; /* don't wait when restarting edit */
1436 }
1437 else if (need_wait_return)
1438 {
1439 /*
1440 * The msg_start() above clears msg_didout. The wait_return we do
1441 * here should not overwrite the command that may be shown before
1442 * doing that.
1443 */
1444 msg_didout |= msg_didout_before_start;
1445 wait_return(FALSE);
1446 }
1447 }
1448
Bram Moolenaar2a942252012-11-28 23:03:07 +01001449#ifdef FEAT_EVAL
1450 did_endif = FALSE; /* in case do_cmdline used recursively */
1451#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 /*
1453 * Reset if_level, in case a sourced script file contains more ":if" than
1454 * ":endif" (could be ":if x | foo | endif").
1455 */
1456 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001457#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 --call_depth;
1460 return retval;
1461}
1462
1463#ifdef FEAT_EVAL
1464/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001465 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 */
1467 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001468get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001470 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 wcmd_T *wp;
1472 char_u *line;
1473
1474 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1475 {
1476 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001477 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001479 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (cp->getline == NULL)
1481 line = getcmdline(c, 0L, indent);
1482 else
1483 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001484 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 ++cp->current_line;
1486
1487 return line;
1488 }
1489
1490 KeyTyped = FALSE;
1491 ++cp->current_line;
1492 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1493 sourcing_lnum = wp->lnum;
1494 return vim_strsave(wp->line);
1495}
1496
1497/*
1498 * Store a line in "gap" so that a ":while" loop can execute it again.
1499 */
1500 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001501store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 if (ga_grow(gap, 1) == FAIL)
1504 return FAIL;
1505 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1506 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1507 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 return OK;
1509}
1510
1511/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001512 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 */
1514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001515free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 while (gap->ga_len > 0)
1518 {
1519 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1520 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
1522}
1523#endif
1524
1525/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001526 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1527 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001530getline_equal(
1531 char_u *(*fgetline)(int, void *, int),
1532 void *cookie UNUSED, /* argument for fgetline() */
1533 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534{
1535#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001536 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001540 * function that's originally used to obtain the lines. This may be
1541 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001542 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001543 cp = (struct loop_cookie *)cookie;
1544 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 {
1546 gp = cp->getline;
1547 cp = cp->cookie;
1548 }
1549 return gp == func;
1550#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001551 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552#endif
1553}
1554
1555#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1556/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001557 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 * getline function. Otherwise return "cookie".
1559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001561getline_cookie(
1562 char_u *(*fgetline)(int, void *, int) UNUSED,
1563 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565# ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001566 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar89d40322006-08-29 15:30:07 +00001569 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001570 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001572 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 cp = (struct loop_cookie *)cookie;
1574 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
1576 gp = cp->getline;
1577 cp = cp->cookie;
1578 }
1579 return cp;
1580# else
1581 return cookie;
1582# endif
1583}
1584#endif
1585
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001586
1587/*
1588 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1589 * ":bwipeout", etc.
1590 * Returns the buffer number.
1591 */
1592 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001593compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001594{
1595 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001596 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001597 int count = offset;
1598
1599 buf = firstbuf;
1600 while (buf->b_next != NULL && buf->b_fnum < lnum)
1601 buf = buf->b_next;
1602 while (count != 0)
1603 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001604 count += (offset < 0) ? 1 : -1;
1605 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1606 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001608 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001609 if (addr_type == ADDR_LOADED_BUFFERS)
1610 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001611 while (buf->b_ml.ml_mfp == NULL)
1612 {
1613 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1614 if (nextbuf == NULL)
1615 break;
1616 buf = nextbuf;
1617 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001619 /* we might have gone too far, last buffer is not loadedd */
1620 if (addr_type == ADDR_LOADED_BUFFERS)
1621 while (buf->b_ml.ml_mfp == NULL)
1622 {
1623 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1624 if (nextbuf == NULL)
1625 break;
1626 buf = nextbuf;
1627 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 return buf->b_fnum;
1629}
1630
Bram Moolenaarf240e182014-11-27 18:33:02 +01001631 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001632current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001633{
1634 win_T *wp;
1635 int nr = 0;
1636
Bram Moolenaar29323592016-07-24 22:04:11 +02001637 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001638 {
1639 ++nr;
1640 if (wp == win)
1641 break;
1642 }
1643 return nr;
1644}
1645
1646 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001647current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001648{
1649 tabpage_T *tp;
1650 int nr = 0;
1651
Bram Moolenaar29323592016-07-24 22:04:11 +02001652 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001653 {
1654 ++nr;
1655 if (tp == tab)
1656 break;
1657 }
1658 return nr;
1659}
1660
1661# define CURRENT_WIN_NR current_win_nr(curwin)
1662# define LAST_WIN_NR current_win_nr(NULL)
1663# define CURRENT_TAB_NR current_tab_nr(curtab)
1664# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666/*
1667 * Execute one Ex command.
1668 *
1669 * If 'sourcing' is TRUE, the command will be included in the error message.
1670 *
1671 * 1. skip comment lines and leading space
1672 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001673 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001674 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001675 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001676 * 6. parse arguments
1677 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001679 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 *
1681 * This function may be called recursively!
1682 */
1683#if (_MSC_VER == 1200)
1684/*
Bram Moolenaared203462004-06-16 11:19:22 +00001685 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001687 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688#endif
1689 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001690do_one_cmd(
1691 char_u **cmdlinep,
1692 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001694 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001696 char_u *(*fgetline)(int, void *, int),
1697 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 char_u *p;
1700 linenr_T lnum;
1701 long n;
1702 char_u *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001703 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001705 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 cmdmod_T save_cmdmod;
1707 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
1710 vim_memset(&ea, 0, sizeof(ea));
1711 ea.line1 = 1;
1712 ea.line2 = 1;
1713#ifdef FEAT_EVAL
1714 ++ex_nesting_level;
1715#endif
1716
Bram Moolenaar21691f82012-12-05 19:13:18 +01001717 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (quitmore
1719#ifdef FEAT_EVAL
1720 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001721 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001722#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001723 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001724 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 --quitmore;
1726
1727 /*
1728 * Reset browse, confirm, etc.. They are restored when returning, for
1729 * recursive calls.
1730 */
1731 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001733 /* "#!anything" is handled like a comment. */
1734 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1735 goto doend;
1736
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001737/*
1738 * 1. Skip comment lines and leading white space and colons.
1739 * 2. Handle command modifiers.
1740 */
1741 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001743 ea.cmdlinep = cmdlinep;
1744 ea.getline = fgetline;
1745 ea.cookie = cookie;
1746#ifdef FEAT_EVAL
1747 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001749 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001750 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001752 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754#ifdef FEAT_EVAL
1755 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1756 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1757#else
1758 ea.skip = (if_level > 0);
1759#endif
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001762 * 3. Skip over the range to find the command. Let "p" point to after it.
1763 *
1764 * We need the command to know what kind of range it uses.
1765 */
1766 cmd = ea.cmd;
1767 ea.cmd = skip_range(ea.cmd, NULL);
1768 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1769 ea.cmd = skipwhite(ea.cmd + 1);
1770 p = find_command(&ea, NULL);
1771
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001772#ifdef FEAT_EVAL
1773# ifdef FEAT_PROFILE
1774 // Count this line for profiling if skip is TRUE.
1775 if (do_profiling == PROF_YES
1776 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1777 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1778 {
1779 int skip = did_emsg || got_int || did_throw;
1780
1781 if (ea.cmdidx == CMD_catch)
1782 skip = !skip && !(cstack->cs_idx >= 0
1783 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1784 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1785 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1786 skip = skip || !(cstack->cs_idx >= 0
1787 && !(cstack->cs_flags[cstack->cs_idx]
1788 & (CSF_ACTIVE | CSF_TRUE)));
1789 else if (ea.cmdidx == CMD_finally)
1790 skip = FALSE;
1791 else if (ea.cmdidx != CMD_endif
1792 && ea.cmdidx != CMD_endfor
1793 && ea.cmdidx != CMD_endtry
1794 && ea.cmdidx != CMD_endwhile)
1795 skip = ea.skip;
1796
1797 if (!skip)
1798 {
1799 if (getline_equal(fgetline, cookie, get_func_line))
1800 func_line_exec(getline_cookie(fgetline, cookie));
1801 else if (getline_equal(fgetline, cookie, getsourceline))
1802 script_line_exec();
1803 }
1804 }
1805# endif
1806
1807 /* May go to debug mode. If this happens and the ">quit" debug command is
1808 * used, throw an interrupt exception and skip the next command. */
1809 dbg_check_breakpoint(&ea);
1810 if (!ea.skip && got_int)
1811 {
1812 ea.skip = TRUE;
1813 (void)do_intthrow(cstack);
1814 }
1815#endif
1816
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001817/*
1818 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 *
1820 * where 'addr' is:
1821 *
1822 * % (entire file)
1823 * $ [+-NUM]
1824 * 'x [+-NUM] (where x denotes a currently defined mark)
1825 * . [+-NUM]
1826 * [+-NUM]..
1827 * NUM
1828 *
1829 * The ea.cmd pointer is updated to point to the first character following the
1830 * range spec. If an initial address is found, but no second, the upper bound
1831 * is equal to the lower.
1832 */
1833
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001834 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001835 if (!IS_USER_CMDIDX(ea.cmdidx))
1836 {
1837 if (ea.cmdidx != CMD_SIZE)
1838 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1839 else
1840 ea.addr_type = ADDR_LINES;
1841
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001842 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001843 if (ea.cmdidx == CMD_wincmd && p != NULL)
1844 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001845 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001846
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001847 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001848 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001849 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001852 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 */
1854
1855 /*
1856 * Skip ':' and any white space
1857 */
1858 ea.cmd = skipwhite(ea.cmd);
1859 while (*ea.cmd == ':')
1860 ea.cmd = skipwhite(ea.cmd + 1);
1861
1862 /*
1863 * If we got a line, but no command, then go to the line.
1864 * If we find a '|' or '\n' we set ea.nextcmd.
1865 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001866 if (*ea.cmd == NUL || *ea.cmd == '"'
1867 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
1869 /*
1870 * strange vi behaviour:
1871 * ":3" jumps to line 3
1872 * ":3|..." prints line 3
1873 * ":|" prints current line
1874 */
1875 if (ea.skip) /* skip this if inside :if */
1876 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001877 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
1879 ea.cmdidx = CMD_print;
1880 ea.argt = RANGE+COUNT+TRLBAR;
1881 if ((errormsg = invalid_range(&ea)) == NULL)
1882 {
1883 correct_range(&ea);
1884 ex_print(&ea);
1885 }
1886 }
1887 else if (ea.addr_count != 0)
1888 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001889 if (ea.line2 > curbuf->b_ml.ml_line_count)
1890 {
1891 /* With '-' in 'cpoptions' a line number past the file is an
1892 * error, otherwise put it at the end of the file. */
1893 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1894 ea.line2 = -1;
1895 else
1896 ea.line2 = curbuf->b_ml.ml_line_count;
1897 }
1898
1899 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001900 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 else
1902 {
1903 if (ea.line2 == 0)
1904 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 else
1906 curwin->w_cursor.lnum = ea.line2;
1907 beginline(BL_SOL | BL_FIX);
1908 }
1909 }
1910 goto doend;
1911 }
1912
Bram Moolenaard5005162014-08-22 23:05:54 +02001913 /* If this looks like an undefined user command and there are CmdUndefined
1914 * autocommands defined, trigger the matching autocommands. */
1915 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1916 && ASCII_ISUPPER(*ea.cmd)
1917 && has_cmdundefined())
1918 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001919 int ret;
1920
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001921 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001922 while (ASCII_ISALNUM(*p))
1923 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001924 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001925 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1926 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001927 /* If the autocommands did something and didn't cause an error, try
1928 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001929 p = (ret
1930#ifdef FEAT_EVAL
1931 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001932#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001933 ) ? find_command(&ea, NULL) : ea.cmd;
1934 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936#ifdef FEAT_USR_CMDS
1937 if (p == NULL)
1938 {
1939 if (!ea.skip)
1940 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
1941 goto doend;
1942 }
1943 /* Check for wrong commands. */
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001944 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1945 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 errormsg = uc_fun_cmd();
1948 goto doend;
1949 }
1950#endif
1951 if (ea.cmdidx == CMD_SIZE)
1952 {
1953 if (!ea.skip)
1954 {
1955 STRCPY(IObuff, _("E492: Not an editor command"));
1956 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001957 {
1958 /* If the modifier was parsed OK the error must be in the
1959 * following command */
1960 if (after_modifier != NULL)
1961 append_command(after_modifier);
1962 else
1963 append_command(*cmdlinep);
1964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001966 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968 goto doend;
1969 }
1970
Bram Moolenaar958636c2014-10-21 20:01:58 +02001971 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1972 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001973#ifdef HAVE_EX_SCRIPT_NI
1974 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1975#endif
1976 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978#ifndef FEAT_EVAL
1979 /*
1980 * When the expression evaluation is disabled, recognize the ":if" and
1981 * ":endif" commands and ignore everything in between it.
1982 */
1983 if (ea.cmdidx == CMD_if)
1984 ++if_level;
1985 if (if_level)
1986 {
1987 if (ea.cmdidx == CMD_endif)
1988 --if_level;
1989 goto doend;
1990 }
1991
1992#endif
1993
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001994 /* forced commands */
1995 if (*p == '!' && ea.cmdidx != CMD_substitute
1996 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 ++p;
1999 ea.forceit = TRUE;
2000 }
2001 else
2002 ea.forceit = FALSE;
2003
2004/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002005 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002007 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002008 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 if (!ea.skip)
2011 {
2012#ifdef HAVE_SANDBOX
2013 if (sandbox != 0 && !(ea.argt & SBOXOK))
2014 {
2015 /* Command not allowed in sandbox. */
2016 errormsg = (char_u *)_(e_sandbox);
2017 goto doend;
2018 }
2019#endif
2020 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2021 {
2022 /* Command not allowed in non-'modifiable' buffer */
2023 errormsg = (char_u *)_(e_modifiable);
2024 goto doend;
2025 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002026
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002027 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002028 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002030 /* Command not allowed when editing the command line. */
Bram Moolenaar75c19462017-02-12 18:34:05 +01002031 errormsg = (char_u *)_(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 goto doend;
2033 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002034
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002035 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002036 * Do allow ":checktime" (it is postponed).
2037 * Do allow ":edit" (check for an argument later).
2038 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002039 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002040 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002041 && ea.cmdidx != CMD_edit
2042 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002043 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002044 && curbuf_locked())
2045 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2048 {
2049 /* no range allowed */
2050 errormsg = (char_u *)_(e_norange);
2051 goto doend;
2052 }
2053 }
2054
2055 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2056 {
2057 errormsg = (char_u *)_(e_nobang);
2058 goto doend;
2059 }
2060
2061 /*
2062 * Don't complain about the range if it is not used
2063 * (could happen if line_count is accidentally set to 0).
2064 */
2065 if (!ea.skip && !ni)
2066 {
2067 /*
2068 * If the range is backwards, ask for confirmation and, if given, swap
2069 * ea.line1 & ea.line2 so it's forwards again.
2070 * When global command is busy, don't ask, will fail below.
2071 */
2072 if (!global_busy && ea.line1 > ea.line2)
2073 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002074 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002076 if (sourcing || exmode_active)
2077 {
2078 errormsg = (char_u *)_("E493: Backwards range given");
2079 goto doend;
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (ask_yesno((char_u *)
2082 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002083 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 lnum = ea.line1;
2086 ea.line1 = ea.line2;
2087 ea.line2 = lnum;
2088 }
2089 if ((errormsg = invalid_range(&ea)) != NULL)
2090 goto doend;
2091 }
2092
2093 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2094 ea.line2 = 1;
2095
2096 correct_range(&ea);
2097
2098#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002099 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2100 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
2102 /* Put the first line at the start of a closed fold, put the last line
2103 * at the end of a closed fold. */
2104 (void)hasFolding(ea.line1, &ea.line1, NULL);
2105 (void)hasFolding(ea.line2, NULL, &ea.line2);
2106 }
2107#endif
2108
2109#ifdef FEAT_QUICKFIX
2110 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002111 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 * option here, so things like % get expanded.
2113 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002114 p = replace_makeprg(&ea, p, cmdlinep);
2115 if (p == NULL)
2116 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#endif
2118
2119 /*
2120 * Skip to start of argument.
2121 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2122 */
2123 if (ea.cmdidx == CMD_bang)
2124 ea.arg = p;
2125 else
2126 ea.arg = skipwhite(p);
2127
Bram Moolenaar379fb762018-08-30 15:58:28 +02002128 // ":file" cannot be run with an argument when "curbuf_lock" is set
2129 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2130 goto doend;
2131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 /*
2133 * Check for "++opt=val" argument.
2134 * Must be first, allow ":w ++enc=utf8 !cmd"
2135 */
2136 if (ea.argt & ARGOPT)
2137 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2138 if (getargopt(&ea) == FAIL && !ni)
2139 {
2140 errormsg = (char_u *)_(e_invarg);
2141 goto doend;
2142 }
2143
2144 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2145 {
2146 if (*ea.arg == '>') /* append */
2147 {
2148 if (*++ea.arg != '>') /* typed wrong */
2149 {
2150 errormsg = (char_u *)_("E494: Use w or w>>");
2151 goto doend;
2152 }
2153 ea.arg = skipwhite(ea.arg + 1);
2154 ea.append = TRUE;
2155 }
2156 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2157 {
2158 ++ea.arg;
2159 ea.usefilter = TRUE;
2160 }
2161 }
2162
2163 if (ea.cmdidx == CMD_read)
2164 {
2165 if (ea.forceit)
2166 {
2167 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2168 ea.forceit = FALSE;
2169 }
2170 else if (*ea.arg == '!') /* :r !filter */
2171 {
2172 ++ea.arg;
2173 ea.usefilter = TRUE;
2174 }
2175 }
2176
2177 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2178 {
2179 ea.amount = 1;
2180 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2181 {
2182 ++ea.arg;
2183 ++ea.amount;
2184 }
2185 ea.arg = skipwhite(ea.arg);
2186 }
2187
2188 /*
2189 * Check for "+command" argument, before checking for next command.
2190 * Don't do this for ":read !cmd" and ":write !cmd".
2191 */
2192 if ((ea.argt & EDITCMD) && !ea.usefilter)
2193 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2194
2195 /*
2196 * Check for '|' to separate commands and '"' to start comments.
2197 * Don't do this for ":read !cmd" and ":write !cmd".
2198 */
2199 if ((ea.argt & TRLBAR) && !ea.usefilter)
2200 separate_nextcmd(&ea);
2201
2202 /*
2203 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002204 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2205 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002207 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002208 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002209 || ea.cmdidx == CMD_global
2210 || ea.cmdidx == CMD_vglobal
2211 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 for (p = ea.arg; *p; ++p)
2214 {
2215 /* Remove one backslash before a newline, so that it's possible to
2216 * pass a newline to the shell and also a newline that is preceded
2217 * with a backslash. This makes it impossible to end a shell
2218 * command in a backslash, but that doesn't appear useful.
2219 * Halving the number of backslashes is incompatible with previous
2220 * versions. */
2221 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002222 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 else if (*p == '\n')
2224 {
2225 ea.nextcmd = p + 1;
2226 *p = NUL;
2227 break;
2228 }
2229 }
2230 }
2231
2232 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2233 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002234 buf_T *buf;
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002237 switch (ea.addr_type)
2238 {
2239 case ADDR_LINES:
2240 ea.line2 = curbuf->b_ml.ml_line_count;
2241 break;
2242 case ADDR_LOADED_BUFFERS:
2243 buf = firstbuf;
2244 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2245 buf = buf->b_next;
2246 ea.line1 = buf->b_fnum;
2247 buf = lastbuf;
2248 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2249 buf = buf->b_prev;
2250 ea.line2 = buf->b_fnum;
2251 break;
2252 case ADDR_BUFFERS:
2253 ea.line1 = firstbuf->b_fnum;
2254 ea.line2 = lastbuf->b_fnum;
2255 break;
2256 case ADDR_WINDOWS:
2257 ea.line2 = LAST_WIN_NR;
2258 break;
2259 case ADDR_TABS:
2260 ea.line2 = LAST_TAB_NR;
2261 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002262 case ADDR_TABS_RELATIVE:
2263 ea.line2 = 1;
2264 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002265 case ADDR_ARGUMENTS:
2266 if (ARGCOUNT == 0)
2267 ea.line1 = ea.line2 = 0;
2268 else
2269 ea.line2 = ARGCOUNT;
2270 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002271#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002272 case ADDR_QUICKFIX:
2273 ea.line2 = qf_get_size(&ea);
2274 if (ea.line2 == 0)
2275 ea.line2 = 1;
2276 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002277#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280
2281 /* accept numbered register only when no count allowed (:put) */
2282 if ( (ea.argt & REGSTR)
2283 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002284 /* Do not allow register = for user commands */
2285 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2287 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002288#ifndef FEAT_CLIPBOARD
2289 /* check these explicitly for a more specific error message */
2290 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002292 errormsg = (char_u *)_(e_invalidreg);
2293 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002296 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2297 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002298 {
2299 ea.regname = *ea.arg++;
2300#ifdef FEAT_EVAL
2301 /* for '=' register: accept the rest of the line as an expression */
2302 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2303 {
2304 set_expr_line(vim_strsave(ea.arg));
2305 ea.arg += STRLEN(ea.arg);
2306 }
2307#endif
2308 ea.arg = skipwhite(ea.arg);
2309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
2311
2312 /*
2313 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2314 * count, it's a buffer name.
2315 */
2316 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2317 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002318 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 n = getdigits(&ea.arg);
2321 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002322 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 errormsg = (char_u *)_(e_zerocount);
2325 goto doend;
2326 }
2327 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2328 {
2329 ea.line2 = n;
2330 if (ea.addr_count == 0)
2331 ea.addr_count = 1;
2332 }
2333 else
2334 {
2335 ea.line1 = ea.line2;
2336 ea.line2 += n - 1;
2337 ++ea.addr_count;
2338 /*
2339 * Be vi compatible: no error message for out of range.
2340 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002341 if (ea.addr_type == ADDR_LINES
2342 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 ea.line2 = curbuf->b_ml.ml_line_count;
2344 }
2345 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002346
2347 /*
2348 * Check for flags: 'l', 'p' and '#'.
2349 */
2350 if (ea.argt & EXFLAGS)
2351 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002353 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002354 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
2356 errormsg = (char_u *)_(e_trailing);
2357 goto doend;
2358 }
2359
2360 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2361 {
2362 errormsg = (char_u *)_(e_argreq);
2363 goto doend;
2364 }
2365
2366#ifdef FEAT_EVAL
2367 /*
2368 * Skip the command when it's not going to be executed.
2369 * The commands like :if, :endif, etc. always need to be executed.
2370 * Also make an exception for commands that handle a trailing command
2371 * themselves.
2372 */
2373 if (ea.skip)
2374 {
2375 switch (ea.cmdidx)
2376 {
2377 /* commands that need evaluation */
2378 case CMD_while:
2379 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002380 case CMD_for:
2381 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 case CMD_if:
2383 case CMD_elseif:
2384 case CMD_else:
2385 case CMD_endif:
2386 case CMD_try:
2387 case CMD_catch:
2388 case CMD_finally:
2389 case CMD_endtry:
2390 case CMD_function:
2391 break;
2392
2393 /* Commands that handle '|' themselves. Check: A command should
2394 * either have the TRLBAR flag, appear in this list or appear in
2395 * the list at ":help :bar". */
2396 case CMD_aboveleft:
2397 case CMD_and:
2398 case CMD_belowright:
2399 case CMD_botright:
2400 case CMD_browse:
2401 case CMD_call:
2402 case CMD_confirm:
2403 case CMD_delfunction:
2404 case CMD_djump:
2405 case CMD_dlist:
2406 case CMD_dsearch:
2407 case CMD_dsplit:
2408 case CMD_echo:
2409 case CMD_echoerr:
2410 case CMD_echomsg:
2411 case CMD_echon:
2412 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002413 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_help:
2415 case CMD_hide:
2416 case CMD_ijump:
2417 case CMD_ilist:
2418 case CMD_isearch:
2419 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002420 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 case CMD_keepjumps:
2422 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002423 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_leftabove:
2425 case CMD_let:
2426 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002427 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002429 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002430 case CMD_noautocmd:
2431 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CMD_perl:
2433 case CMD_psearch:
2434 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002435 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002436 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_return:
2438 case CMD_rightbelow:
2439 case CMD_ruby:
2440 case CMD_silent:
2441 case CMD_smagic:
2442 case CMD_snomagic:
2443 case CMD_substitute:
2444 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002445 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 case CMD_tcl:
2447 case CMD_throw:
2448 case CMD_tilde:
2449 case CMD_topleft:
2450 case CMD_unlet:
2451 case CMD_verbose:
2452 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002453 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 break;
2455
2456 default: goto doend;
2457 }
2458 }
2459#endif
2460
2461 if (ea.argt & XFILE)
2462 {
2463 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2464 goto doend;
2465 }
2466
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 /*
2468 * Accept buffer name. Cannot be used at the same time with a buffer
2469 * number. Don't do this for a user command.
2470 */
2471 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002472 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 /*
2475 * :bdelete, :bwipeout and :bunload take several arguments, separated
2476 * by spaces: find next space (skipping over escaped characters).
2477 * The others take one argument: ignore trailing spaces.
2478 */
2479 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2480 || ea.cmdidx == CMD_bunload)
2481 p = skiptowhite_esc(ea.arg);
2482 else
2483 {
2484 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002485 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 --p;
2487 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002488 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2489 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (ea.line2 < 0) /* failed */
2491 goto doend;
2492 ea.addr_count = 1;
2493 ea.arg = skipwhite(p);
2494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaar2be57332018-02-13 18:05:18 +01002496 /* The :try command saves the emsg_silent flag, reset it here when
2497 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002498 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002499 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002500 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002501 if (emsg_silent < 0)
2502 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002503 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002504 }
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002507 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002511 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
2513 /*
2514 * Execute a user-defined command.
2515 */
2516 do_ucmd(&ea);
2517 }
2518 else
2519#endif
2520 {
2521 /*
2522 * Call the function to execute the command.
2523 */
2524 ea.errmsg = NULL;
2525 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2526 if (ea.errmsg != NULL)
2527 errormsg = (char_u *)_(ea.errmsg);
2528 }
2529
2530#ifdef FEAT_EVAL
2531 /*
2532 * If the command just executed called do_cmdline(), any throw or ":return"
2533 * or ":finish" encountered there must also check the cstack of the still
2534 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2535 * exception, or reanimate a returned function or finished script file and
2536 * return or finish it again.
2537 */
2538 if (need_rethrow)
2539 do_throw(cstack);
2540 else if (check_cstack)
2541 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002542 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002544 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 && current_func_returned())
2546 do_return(&ea, TRUE, FALSE, NULL);
2547 }
2548 need_rethrow = check_cstack = FALSE;
2549#endif
2550
2551doend:
2552 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002555 curwin->w_cursor.col = 0;
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2559 {
2560 if (sourcing)
2561 {
2562 if (errormsg != IObuff)
2563 {
2564 STRCPY(IObuff, errormsg);
2565 errormsg = IObuff;
2566 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002567 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 emsg(errormsg);
2570 }
2571#ifdef FEAT_EVAL
2572 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002573 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2574 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575#endif
2576
Bram Moolenaareffed932018-08-14 13:38:17 +02002577 if (ea.verbose_save >= 0)
2578 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002579
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002580 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 cmdmod = save_cmdmod;
2582
Bram Moolenaareffed932018-08-14 13:38:17 +02002583 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
2585 /* messages could be enabled for a serious error, need to check if the
2586 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002587 if (!did_emsg || msg_silent > ea.save_msg_silent)
2588 msg_silent = ea.save_msg_silent;
2589 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (emsg_silent < 0)
2591 emsg_silent = 0;
2592 /* Restore msg_scroll, it's set by file I/O commands, even when no
2593 * message is actually displayed. */
2594 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002595
2596 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2597 * somewhere in the line. Put it back in the first column. */
2598 if (redirecting())
2599 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
2601
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002602#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002604 --sandbox;
2605#endif
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2608 ea.nextcmd = NULL;
2609
2610#ifdef FEAT_EVAL
2611 --ex_nesting_level;
2612#endif
2613
2614 return ea.nextcmd;
2615}
2616#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002617 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#endif
2619
2620/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 * Parse and skip over command modifiers:
2622 * - update eap->cmd
2623 * - store flags in "cmdmod".
2624 * - Set ex_pressedreturn for an empty command line.
2625 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002626 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002627 * - set p_verbose for ":verbose"
2628 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002629 * When "skip_only" is TRUE the global variables are not changed, except for
2630 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002631 * Return FAIL when the command is not to be executed.
2632 * May set "errormsg" to an error message.
2633 */
2634 int
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002635parse_command_modifiers(exarg_T *eap, char_u **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002636{
2637 char_u *p;
2638
2639 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2640 eap->verbose_save = -1;
2641 eap->save_msg_silent = -1;
2642
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002643 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002644 for (;;)
2645 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2647 ++eap->cmd;
2648
2649 /* in ex mode, an empty line works like :+ */
2650 if (*eap->cmd == NUL && exmode_active
2651 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2652 || getline_equal(eap->getline, eap->cookie, getexline))
2653 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2654 {
2655 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002656 if (!skip_only)
2657 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002658 }
2659
2660 /* ignore comment and empty lines */
2661 if (*eap->cmd == '"')
2662 return FAIL;
2663 if (*eap->cmd == NUL)
2664 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002665 if (!skip_only)
2666 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002667 return FAIL;
2668 }
2669
Bram Moolenaareffed932018-08-14 13:38:17 +02002670 p = skip_range(eap->cmd, NULL);
2671 switch (*p)
2672 {
2673 /* When adding an entry, also modify cmd_exists(). */
2674 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2675 break;
2676 cmdmod.split |= WSP_ABOVE;
2677 continue;
2678
2679 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2680 {
2681 cmdmod.split |= WSP_BELOW;
2682 continue;
2683 }
2684 if (checkforcmd(&eap->cmd, "browse", 3))
2685 {
2686#ifdef FEAT_BROWSE_CMD
2687 cmdmod.browse = TRUE;
2688#endif
2689 continue;
2690 }
2691 if (!checkforcmd(&eap->cmd, "botright", 2))
2692 break;
2693 cmdmod.split |= WSP_BOT;
2694 continue;
2695
2696 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2697 break;
2698#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2699 cmdmod.confirm = TRUE;
2700#endif
2701 continue;
2702
2703 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2704 {
2705 cmdmod.keepmarks = TRUE;
2706 continue;
2707 }
2708 if (checkforcmd(&eap->cmd, "keepalt", 5))
2709 {
2710 cmdmod.keepalt = TRUE;
2711 continue;
2712 }
2713 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2714 {
2715 cmdmod.keeppatterns = TRUE;
2716 continue;
2717 }
2718 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2719 break;
2720 cmdmod.keepjumps = TRUE;
2721 continue;
2722
2723 case 'f': /* only accept ":filter {pat} cmd" */
2724 {
2725 char_u *reg_pat;
2726
2727 if (!checkforcmd(&p, "filter", 4)
2728 || *p == NUL || ends_excmd(*p))
2729 break;
2730 if (*p == '!')
2731 {
2732 cmdmod.filter_force = TRUE;
2733 p = skipwhite(p + 1);
2734 if (*p == NUL || ends_excmd(*p))
2735 break;
2736 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002737 if (skip_only)
2738 p = skip_vimgrep_pat(p, NULL, NULL);
2739 else
2740 // NOTE: This puts a NUL after the pattern.
2741 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002742 if (p == NULL || *p == NUL)
2743 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002744 if (!skip_only)
2745 {
2746 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002747 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002748 if (cmdmod.filter_regmatch.regprog == NULL)
2749 break;
2750 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 eap->cmd = p;
2752 continue;
2753 }
2754
2755 /* ":hide" and ":hide | cmd" are not modifiers */
2756 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2757 || *p == NUL || ends_excmd(*p))
2758 break;
2759 eap->cmd = p;
2760 cmdmod.hide = TRUE;
2761 continue;
2762
2763 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2764 {
2765 cmdmod.lockmarks = TRUE;
2766 continue;
2767 }
2768
2769 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2770 break;
2771 cmdmod.split |= WSP_ABOVE;
2772 continue;
2773
2774 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2775 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 {
2778 /* Set 'eventignore' to "all". Restore the
2779 * existing option value later. */
2780 cmdmod.save_ei = vim_strsave(p_ei);
2781 set_string_option_direct((char_u *)"ei", -1,
2782 (char_u *)"all", OPT_FREE, SID_NONE);
2783 }
2784 continue;
2785 }
2786 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2787 break;
2788 cmdmod.noswapfile = TRUE;
2789 continue;
2790
2791 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2792 break;
2793 cmdmod.split |= WSP_BELOW;
2794 continue;
2795
2796 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2797 {
2798#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002799 if (!skip_only)
2800 {
2801 if (!eap->did_sandbox)
2802 ++sandbox;
2803 eap->did_sandbox = TRUE;
2804 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002805#endif
2806 continue;
2807 }
2808 if (!checkforcmd(&eap->cmd, "silent", 3))
2809 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002810 if (!skip_only)
2811 {
2812 if (eap->save_msg_silent == -1)
2813 eap->save_msg_silent = msg_silent;
2814 ++msg_silent;
2815 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2817 {
2818 /* ":silent!", but not "silent !cmd" */
2819 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002820 if (!skip_only)
2821 {
2822 ++emsg_silent;
2823 ++eap->did_esilent;
2824 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 }
2826 continue;
2827
2828 case 't': if (checkforcmd(&p, "tab", 3))
2829 {
2830 long tabnr = get_address(eap, &eap->cmd, ADDR_TABS,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002831 eap->skip, skip_only, FALSE, 1);
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 if (tabnr == MAXLNUM)
2833 cmdmod.tab = tabpage_index(curtab) + 1;
2834 else
2835 {
2836 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2837 {
2838 *errormsg = (char_u *)_(e_invrange);
2839 return FAIL;
2840 }
2841 cmdmod.tab = tabnr + 1;
2842 }
2843 eap->cmd = p;
2844 continue;
2845 }
2846 if (!checkforcmd(&eap->cmd, "topleft", 2))
2847 break;
2848 cmdmod.split |= WSP_TOP;
2849 continue;
2850
2851 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2852 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002853 if (!skip_only)
2854 {
2855 if (eap->save_msg_silent == -1)
2856 eap->save_msg_silent = msg_silent;
2857 msg_silent = 0;
2858 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 continue;
2860
2861 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2862 {
2863 cmdmod.split |= WSP_VERT;
2864 continue;
2865 }
2866 if (!checkforcmd(&p, "verbose", 4))
2867 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002868 if (!skip_only)
2869 {
2870 if (eap->verbose_save < 0)
2871 eap->verbose_save = p_verbose;
2872 if (vim_isdigit(*eap->cmd))
2873 p_verbose = atoi((char *)eap->cmd);
2874 else
2875 p_verbose = 1;
2876 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 eap->cmd = p;
2878 continue;
2879 }
2880 break;
2881 }
2882
2883 return OK;
2884}
2885
2886/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002887 * Free contents of "cmdmod".
2888 */
2889 static void
2890free_cmdmod(void)
2891{
2892 if (cmdmod.save_ei != NULL)
2893 {
2894 /* Restore 'eventignore' to the value before ":noautocmd". */
2895 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2896 OPT_FREE, SID_NONE);
2897 free_string_option(cmdmod.save_ei);
2898 }
2899
2900 if (cmdmod.filter_regmatch.regprog != NULL)
2901 vim_regfree(cmdmod.filter_regmatch.regprog);
2902}
2903
2904/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002905 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002906 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002907 * Return FAIL and set "errormsg" or return OK.
2908 */
2909 int
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002910parse_cmd_address(exarg_T *eap, char_u **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002911{
2912 int address_count = 1;
2913 linenr_T lnum;
2914
2915 // Repeat for all ',' or ';' separated addresses.
2916 for (;;)
2917 {
2918 eap->line1 = eap->line2;
2919 switch (eap->addr_type)
2920 {
2921 case ADDR_LINES:
2922 // default is current line number
2923 eap->line2 = curwin->w_cursor.lnum;
2924 break;
2925 case ADDR_WINDOWS:
2926 eap->line2 = CURRENT_WIN_NR;
2927 break;
2928 case ADDR_ARGUMENTS:
2929 eap->line2 = curwin->w_arg_idx + 1;
2930 if (eap->line2 > ARGCOUNT)
2931 eap->line2 = ARGCOUNT;
2932 break;
2933 case ADDR_LOADED_BUFFERS:
2934 case ADDR_BUFFERS:
2935 eap->line2 = curbuf->b_fnum;
2936 break;
2937 case ADDR_TABS:
2938 eap->line2 = CURRENT_TAB_NR;
2939 break;
2940 case ADDR_TABS_RELATIVE:
2941 eap->line2 = 1;
2942 break;
2943#ifdef FEAT_QUICKFIX
2944 case ADDR_QUICKFIX:
2945 eap->line2 = qf_get_cur_valid_idx(eap);
2946 break;
2947#endif
2948 }
2949 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002950 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002951 eap->addr_count == 0, address_count++);
2952 if (eap->cmd == NULL) // error detected
2953 return FAIL;
2954 if (lnum == MAXLNUM)
2955 {
2956 if (*eap->cmd == '%') // '%' - all lines
2957 {
2958 ++eap->cmd;
2959 switch (eap->addr_type)
2960 {
2961 case ADDR_LINES:
2962 eap->line1 = 1;
2963 eap->line2 = curbuf->b_ml.ml_line_count;
2964 break;
2965 case ADDR_LOADED_BUFFERS:
2966 {
2967 buf_T *buf = firstbuf;
2968
2969 while (buf->b_next != NULL
2970 && buf->b_ml.ml_mfp == NULL)
2971 buf = buf->b_next;
2972 eap->line1 = buf->b_fnum;
2973 buf = lastbuf;
2974 while (buf->b_prev != NULL
2975 && buf->b_ml.ml_mfp == NULL)
2976 buf = buf->b_prev;
2977 eap->line2 = buf->b_fnum;
2978 break;
2979 }
2980 case ADDR_BUFFERS:
2981 eap->line1 = firstbuf->b_fnum;
2982 eap->line2 = lastbuf->b_fnum;
2983 break;
2984 case ADDR_WINDOWS:
2985 case ADDR_TABS:
2986 if (IS_USER_CMDIDX(eap->cmdidx))
2987 {
2988 eap->line1 = 1;
2989 eap->line2 = eap->addr_type == ADDR_WINDOWS
2990 ? LAST_WIN_NR : LAST_TAB_NR;
2991 }
2992 else
2993 {
2994 // there is no Vim command which uses '%' and
2995 // ADDR_WINDOWS or ADDR_TABS
2996 *errormsg = (char_u *)_(e_invrange);
2997 return FAIL;
2998 }
2999 break;
3000 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01003001 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003002 *errormsg = (char_u *)_(e_invrange);
3003 return FAIL;
3004 case ADDR_ARGUMENTS:
3005 if (ARGCOUNT == 0)
3006 eap->line1 = eap->line2 = 0;
3007 else
3008 {
3009 eap->line1 = 1;
3010 eap->line2 = ARGCOUNT;
3011 }
3012 break;
3013#ifdef FEAT_QUICKFIX
3014 case ADDR_QUICKFIX:
3015 eap->line1 = 1;
3016 eap->line2 = qf_get_size(eap);
3017 if (eap->line2 == 0)
3018 eap->line2 = 1;
3019 break;
3020#endif
3021 }
3022 ++eap->addr_count;
3023 }
3024 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3025 {
3026 pos_T *fp;
3027
3028 // '*' - visual area
3029 if (eap->addr_type != ADDR_LINES)
3030 {
3031 *errormsg = (char_u *)_(e_invrange);
3032 return FAIL;
3033 }
3034
3035 ++eap->cmd;
3036 if (!eap->skip)
3037 {
3038 fp = getmark('<', FALSE);
3039 if (check_mark(fp) == FAIL)
3040 return FAIL;
3041 eap->line1 = fp->lnum;
3042 fp = getmark('>', FALSE);
3043 if (check_mark(fp) == FAIL)
3044 return FAIL;
3045 eap->line2 = fp->lnum;
3046 ++eap->addr_count;
3047 }
3048 }
3049 }
3050 else
3051 eap->line2 = lnum;
3052 eap->addr_count++;
3053
3054 if (*eap->cmd == ';')
3055 {
3056 if (!eap->skip)
3057 {
3058 curwin->w_cursor.lnum = eap->line2;
3059 // don't leave the cursor on an illegal line or column
3060 check_cursor();
3061 }
3062 }
3063 else if (*eap->cmd != ',')
3064 break;
3065 ++eap->cmd;
3066 }
3067
3068 // One address given: set start and end lines.
3069 if (eap->addr_count == 1)
3070 {
3071 eap->line1 = eap->line2;
3072 // ... but only implicit: really no address given
3073 if (lnum == MAXLNUM)
3074 eap->addr_count = 0;
3075 }
3076 return OK;
3077}
3078
3079/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003080 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 * If there is a match advance "pp" to the argument and return TRUE.
3082 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003083 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003084checkforcmd(
3085 char_u **pp, /* start of command */
3086 char *cmd, /* name of command */
3087 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 int i;
3090
3091 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003092 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 break;
3094 if (i >= len && !isalpha((*pp)[i]))
3095 {
3096 *pp = skipwhite(*pp + i);
3097 return TRUE;
3098 }
3099 return FALSE;
3100}
3101
3102/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003103 * Append "cmd" to the error message in IObuff.
3104 * Takes care of limiting the length and handling 0xa0, which would be
3105 * invisible otherwise.
3106 */
3107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003108append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003109{
3110 char_u *s = cmd;
3111 char_u *d;
3112
3113 STRCAT(IObuff, ": ");
3114 d = IObuff + STRLEN(IObuff);
3115 while (*s != NUL && d - IObuff < IOSIZE - 7)
3116 {
3117 if (
3118#ifdef FEAT_MBYTE
3119 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
3120#endif
3121 *s == 0xa0)
3122 {
3123 s +=
3124#ifdef FEAT_MBYTE
3125 enc_utf8 ? 2 :
3126#endif
3127 1;
3128 STRCPY(d, "<a0>");
3129 d += 4;
3130 }
3131 else
3132 MB_COPY_CHAR(s, d);
3133 }
3134 *d = NUL;
3135}
3136
3137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003139 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003141 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * Returns NULL for an ambiguous user command.
3143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003145find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
3147 int len;
3148 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003149 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 /*
3152 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003153 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 * - the 'k' command can directly be followed by any character.
3155 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003156 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003158 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 */
3160 p = eap->cmd;
3161 if (*p == 'k')
3162 {
3163 eap->cmdidx = CMD_k;
3164 ++p;
3165 }
3166 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003167 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3168 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 || p[1] == 'g'
3170 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3171 || p[1] == 'I'
3172 || (p[1] == 'r' && p[2] != 'e')))
3173 {
3174 eap->cmdidx = CMD_substitute;
3175 ++p;
3176 }
3177 else
3178 {
3179 while (ASCII_ISALPHA(*p))
3180 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003181 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003182 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003183 while (ASCII_ISALNUM(*p))
3184 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 /* check for non-alpha command */
3187 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3188 ++p;
3189 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003190 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3191 {
3192 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3193 * :delete with the 'l' flag. Same for 'p'. */
3194 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003195 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003196 break;
3197 if (i == len - 1)
3198 {
3199 --len;
3200 if (p[-1] == 'l')
3201 eap->flags |= EXFLAG_LIST;
3202 else
3203 eap->flags |= EXFLAG_PRINT;
3204 }
3205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003207 if (ASCII_ISLOWER(eap->cmd[0]))
3208 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003209 int c1 = eap->cmd[0];
3210 int c2 = eap->cmd[1];
3211
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003212 if (command_count != (int)CMD_SIZE)
3213 {
3214 iemsg((char_u *)_("E943: Command table needs to be updated, run 'make cmdidxs'"));
3215 getout(1);
3216 }
3217
3218 /* Use a precomputed index for fast look-up in cmdnames[]
3219 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003220 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3221 if (ASCII_ISLOWER(c2))
3222 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003225 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226
3227 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3228 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3229 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3230 (size_t)len) == 0)
3231 {
3232#ifdef FEAT_EVAL
3233 if (full != NULL
3234 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3235 *full = TRUE;
3236#endif
3237 break;
3238 }
3239
3240#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003241 /* Look for a user defined command as a last resort. Let ":Print" be
3242 * overruled by a user defined command. */
3243 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3244 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003246 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 while (ASCII_ISALNUM(*p))
3248 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003249 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 eap->cmdidx = CMD_SIZE;
3254 }
3255
3256 return p;
3257}
3258
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003259#ifdef FEAT_USR_CMDS
3260/*
3261 * Search for a user command that matches "eap->cmd".
3262 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3263 * Return a pointer to just after the command.
3264 * Return NULL if there is no matching command.
3265 */
3266 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003267find_ucmd(
3268 exarg_T *eap,
3269 char_u *p, /* end of the command (possibly including count) */
3270 int *full, /* set to TRUE for a full match */
3271 expand_T *xp, /* used for completion, NULL otherwise */
3272 int *compl) /* completion flags or NULL */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003273{
3274 int len = (int)(p - eap->cmd);
3275 int j, k, matchlen = 0;
3276 ucmd_T *uc;
3277 int found = FALSE;
3278 int possible = FALSE;
3279 char_u *cp, *np; /* Point into typed cmd and test name */
3280 garray_T *gap;
3281 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3282 only full match global is accepted. */
3283
3284 /*
3285 * Look for buffer-local user commands first, then global ones.
3286 */
3287 gap = &curbuf->b_ucmds;
3288 for (;;)
3289 {
3290 for (j = 0; j < gap->ga_len; ++j)
3291 {
3292 uc = USER_CMD_GA(gap, j);
3293 cp = eap->cmd;
3294 np = uc->uc_name;
3295 k = 0;
3296 while (k < len && *np != NUL && *cp++ == *np++)
3297 k++;
3298 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3299 {
3300 /* If finding a second match, the command is ambiguous. But
3301 * not if a buffer-local command wasn't a full match and a
3302 * global command is a full match. */
3303 if (k == len && found && *np != NUL)
3304 {
3305 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003306 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003307 amb_local = TRUE;
3308 }
3309
3310 if (!found || (k == len && *np == NUL))
3311 {
3312 /* If we matched up to a digit, then there could
3313 * be another command including the digit that we
3314 * should use instead.
3315 */
3316 if (k == len)
3317 found = TRUE;
3318 else
3319 possible = TRUE;
3320
3321 if (gap == &ucmds)
3322 eap->cmdidx = CMD_USER;
3323 else
3324 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003325 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003326 eap->useridx = j;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003327 eap->addr_type = uc->uc_addr_type;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003328
3329# ifdef FEAT_CMDL_COMPL
3330 if (compl != NULL)
3331 *compl = uc->uc_compl;
3332# ifdef FEAT_EVAL
3333 if (xp != NULL)
3334 {
3335 xp->xp_arg = uc->uc_compl_arg;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003336 xp->xp_script_ctx = uc->uc_script_ctx;
3337 xp->xp_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003338 }
3339# endif
3340# endif
3341 /* Do not search for further abbreviations
3342 * if this is an exact match. */
3343 matchlen = k;
3344 if (k == len && *np == NUL)
3345 {
3346 if (full != NULL)
3347 *full = TRUE;
3348 amb_local = FALSE;
3349 break;
3350 }
3351 }
3352 }
3353 }
3354
3355 /* Stop if we found a full match or searched all. */
3356 if (j < gap->ga_len || gap == &ucmds)
3357 break;
3358 gap = &ucmds;
3359 }
3360
3361 /* Only found ambiguous matches. */
3362 if (amb_local)
3363 {
3364 if (xp != NULL)
3365 xp->xp_context = EXPAND_UNSUCCESSFUL;
3366 return NULL;
3367 }
3368
3369 /* The match we found may be followed immediately by a number. Move "p"
3370 * back to point to it. */
3371 if (found || possible)
3372 return p + (matchlen - len);
3373 return p;
3374}
3375#endif
3376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003378static struct cmdmod
3379{
3380 char *name;
3381 int minlen;
3382 int has_count; /* :123verbose :3tab */
3383} cmdmods[] = {
3384 {"aboveleft", 3, FALSE},
3385 {"belowright", 3, FALSE},
3386 {"botright", 2, FALSE},
3387 {"browse", 3, FALSE},
3388 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003389 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003390 {"hide", 3, FALSE},
3391 {"keepalt", 5, FALSE},
3392 {"keepjumps", 5, FALSE},
3393 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003394 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003395 {"leftabove", 5, FALSE},
3396 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003397 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003398 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003399 {"rightbelow", 6, FALSE},
3400 {"sandbox", 3, FALSE},
3401 {"silent", 3, FALSE},
3402 {"tab", 3, TRUE},
3403 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003404 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003405 {"verbose", 4, TRUE},
3406 {"vertical", 4, FALSE},
3407};
3408
3409/*
3410 * Return length of a command modifier (including optional count).
3411 * Return zero when it's not a modifier.
3412 */
3413 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003414modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003415{
3416 int i, j;
3417 char_u *p = cmd;
3418
3419 if (VIM_ISDIGIT(*cmd))
3420 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003421 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003422 {
3423 for (j = 0; p[j] != NUL; ++j)
3424 if (p[j] != cmdmods[i].name[j])
3425 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003426 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003427 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003428 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003429 }
3430 return 0;
3431}
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433/*
3434 * Return > 0 if an Ex command "name" exists.
3435 * Return 2 if there is an exact match.
3436 * Return 3 if there is an ambiguous match.
3437 */
3438 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003439cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 exarg_T ea;
3442 int full = FALSE;
3443 int i;
3444 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003445 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003448 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 for (j = 0; name[j] != NUL; ++j)
3451 if (name[j] != cmdmods[i].name[j])
3452 break;
3453 if (name[j] == NUL && j >= cmdmods[i].minlen)
3454 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3455 }
3456
Bram Moolenaara9587612006-05-04 21:47:50 +00003457 /* Check built-in commands and user defined commands.
3458 * For ":2match" and ":3match" we need to skip the number. */
3459 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003461 p = find_command(&ea, &full);
3462 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003464 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3465 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003466 if (*skipwhite(p) != NUL)
3467 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3469}
3470#endif
3471
3472/*
3473 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3474 * we don't need/want deleted. Maybe this could be done better if we didn't
3475 * repeat all this stuff. The only problem is that they may not stay
3476 * perfectly compatible with each other, but then the command line syntax
3477 * probably won't change that much -- webb.
3478 */
3479 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003480set_one_cmd_context(
3481 expand_T *xp,
3482 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 char_u *p;
3485 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003486 int len = 0;
3487 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3489 int compl = EXPAND_NOTHING;
3490#endif
3491#ifdef FEAT_CMDL_COMPL
3492 int delim;
3493#endif
3494 int forceit = FALSE;
3495 int usefilter = FALSE; /* filter instead of file name */
3496
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003497 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 xp->xp_pattern = buff;
3499 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003500 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003503 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 */
3505 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3506 ;
3507 xp->xp_pattern = cmd;
3508
3509 if (*cmd == NUL)
3510 return NULL;
3511 if (*cmd == '"') /* ignore comment lines */
3512 {
3513 xp->xp_context = EXPAND_NOTHING;
3514 return NULL;
3515 }
3516
3517/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003518 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 */
3520 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 xp->xp_pattern = cmd;
3522 if (*cmd == NUL)
3523 return NULL;
3524 if (*cmd == '"')
3525 {
3526 xp->xp_context = EXPAND_NOTHING;
3527 return NULL;
3528 }
3529
3530 if (*cmd == '|' || *cmd == '\n')
3531 return cmd + 1; /* There's another command */
3532
3533 /*
3534 * Isolate the command and search for it in the command table.
3535 * Exceptions:
3536 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003537 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3539 */
3540 if (*cmd == 'k' && cmd[1] != 'e')
3541 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003542 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 p = cmd + 1;
3544 }
3545 else
3546 {
3547 p = cmd;
3548 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3549 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003550 /* a user command may contain digits */
3551 if (ASCII_ISUPPER(cmd[0]))
3552 while (ASCII_ISALNUM(*p) || *p == '*')
3553 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003554 /* for python 3.x: ":py3*" commands completion */
3555 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003556 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003557 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003558 while (ASCII_ISALPHA(*p) || *p == '*')
3559 ++p;
3560 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003561 /* check for non-alpha command */
3562 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3563 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003566 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 xp->xp_context = EXPAND_UNSUCCESSFUL;
3569 return NULL;
3570 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003572 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3573 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3574 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 break;
3576
3577#ifdef FEAT_USR_CMDS
3578 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3580 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581#endif
3582 }
3583
3584 /*
3585 * If the cursor is touching the command, and it ends in an alpha-numeric
3586 * character, complete the command name.
3587 */
3588 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3589 return NULL;
3590
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003591 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
3593 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3594 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 p = cmd + 1;
3597 }
3598#ifdef FEAT_USR_CMDS
3599 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3600 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003601 ea.cmd = cmd;
3602 p = find_ucmd(&ea, p, NULL, xp,
3603# if defined(FEAT_CMDL_COMPL)
3604 &compl
3605# else
3606 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003608 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003609 if (p == NULL)
3610 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612#endif
3613 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003614 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
3616 /* Not still touching the command and it was an illegal one */
3617 xp->xp_context = EXPAND_UNSUCCESSFUL;
3618 return NULL;
3619 }
3620
3621 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3622
3623 if (*p == '!') /* forced commands */
3624 {
3625 forceit = TRUE;
3626 ++p;
3627 }
3628
3629/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003630 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003632 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003633 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
3635 arg = skipwhite(p);
3636
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003637 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
3639 if (*arg == '>') /* append */
3640 {
3641 if (*++arg == '>')
3642 ++arg;
3643 arg = skipwhite(arg);
3644 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003645 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
3647 ++arg;
3648 usefilter = TRUE;
3649 }
3650 }
3651
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003652 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
3654 usefilter = forceit; /* :r! filter if forced */
3655 if (*arg == '!') /* :r !filter */
3656 {
3657 ++arg;
3658 usefilter = TRUE;
3659 }
3660 }
3661
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003662 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 while (*arg == *cmd) /* allow any number of '>' or '<' */
3665 ++arg;
3666 arg = skipwhite(arg);
3667 }
3668
3669 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003670 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
3672 /* Check if we're in the +command */
3673 p = arg + 1;
3674 arg = skip_cmd_arg(arg, FALSE);
3675
3676 /* Still touching the command after '+'? */
3677 if (*arg == NUL)
3678 return p;
3679
3680 /* Skip space(s) after +command to get to the real argument */
3681 arg = skipwhite(arg);
3682 }
3683
3684 /*
3685 * Check for '|' to separate commands and '"' to start comments.
3686 * Don't do this for ":read !cmd" and ":write !cmd".
3687 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003688 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
3690 p = arg;
3691 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003692 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 p += 2;
3694 while (*p)
3695 {
3696 if (*p == Ctrl_V)
3697 {
3698 if (p[1] != NUL)
3699 ++p;
3700 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003701 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 || *p == '|' || *p == '\n')
3703 {
3704 if (*(p - 1) != '\\')
3705 {
3706 if (*p == '|' || *p == '\n')
3707 return p + 1;
3708 return NULL; /* It's a comment */
3709 }
3710 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003711 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
3713 }
3714
3715 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003716 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 vim_strchr((char_u *)"|\"", *arg) == NULL)
3718 return NULL;
3719
3720 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003721 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003723 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003724 while (*p && p < buff + len)
3725 {
3726 if (*p == ' ' || *p == TAB)
3727 {
3728 /* argument starts after a space */
3729 xp->xp_pattern = ++p;
3730 }
3731 else
3732 {
3733 if (*p == '\\' && *(p + 1) != NUL)
3734 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003735 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003736 }
3737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003739 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003741 int c;
3742 int in_quote = FALSE;
3743 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 /*
3746 * Allow spaces within back-quotes to count as part of the argument
3747 * being expanded.
3748 */
3749 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003750 p = xp->xp_pattern;
3751 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003753#ifdef FEAT_MBYTE
3754 if (has_mbyte)
3755 c = mb_ptr2char(p);
3756 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003758 c = *p;
3759 if (c == '\\' && p[1] != NUL)
3760 ++p;
3761 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
3763 if (!in_quote)
3764 {
3765 xp->xp_pattern = p;
3766 bow = p + 1;
3767 }
3768 in_quote = !in_quote;
3769 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003770 /* An argument can contain just about everything, except
3771 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003772 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003773#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003774 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003775#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003776 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003777 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003778 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003779 while (*p != NUL)
3780 {
3781#ifdef FEAT_MBYTE
3782 if (has_mbyte)
3783 c = mb_ptr2char(p);
3784 else
3785#endif
3786 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003787 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003788 break;
3789#ifdef FEAT_MBYTE
3790 if (has_mbyte)
3791 len = (*mb_ptr2len)(p);
3792 else
3793#endif
3794 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003795 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003796 }
3797 if (in_quote)
3798 bow = p;
3799 else
3800 xp->xp_pattern = p;
3801 p -= len;
3802 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003803 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805
3806 /*
3807 * If we are still inside the quotes, and we passed a space, just
3808 * expand from there.
3809 */
3810 if (bow != NULL && in_quote)
3811 xp->xp_pattern = bow;
3812 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003813
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003814 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003815 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003816 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003817#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003818 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003819#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003820 /* When still after the command name expand executables. */
3821 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003822 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
3825 /* Check for environment variable */
3826 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003827#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 || *xp->xp_pattern == '%'
3829#endif
3830 )
3831 {
3832 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3833 if (!vim_isIDc(*p))
3834 break;
3835 if (*p == NUL)
3836 {
3837 xp->xp_context = EXPAND_ENV_VARS;
3838 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003839#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3840 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003841 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003842 compl = EXPAND_ENV_VARS;
3843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003846#if defined(FEAT_CMDL_COMPL)
3847 /* Check for user names */
3848 if (*xp->xp_pattern == '~')
3849 {
3850 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3851 ;
3852 /* Complete ~user only if it partially matches a user name.
3853 * A full match ~user<Tab> will be replaced by user's home
3854 * directory i.e. something like ~user<Tab> -> /home/user/ */
3855 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003856 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003857 {
3858 xp->xp_context = EXPAND_USER;
3859 ++xp->xp_pattern;
3860 }
3861 }
3862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864
3865/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003866 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003868 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003870 case CMD_find:
3871 case CMD_sfind:
3872 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003873 if (xp->xp_context == EXPAND_FILES)
3874 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003875 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 case CMD_cd:
3877 case CMD_chdir:
3878 case CMD_lcd:
3879 case CMD_lchdir:
3880 if (xp->xp_context == EXPAND_FILES)
3881 xp->xp_context = EXPAND_DIRECTORIES;
3882 break;
3883 case CMD_help:
3884 xp->xp_context = EXPAND_HELP;
3885 xp->xp_pattern = arg;
3886 break;
3887
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003888 /* Command modifiers: return the argument.
3889 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003891 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 case CMD_belowright:
3893 case CMD_botright:
3894 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003895 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003896 case CMD_cdo:
3897 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003899 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 case CMD_folddoclosed:
3901 case CMD_folddoopen:
3902 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003903 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 case CMD_keepjumps:
3905 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003906 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003907 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003909 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003911 case CMD_noautocmd:
3912 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003914 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003916 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003917 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 case CMD_topleft:
3919 case CMD_verbose:
3920 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003921 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 return arg;
3923
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003924 case CMD_filter:
3925 if (*arg != NUL)
3926 arg = skip_vimgrep_pat(arg, NULL, NULL);
3927 if (arg == NULL || *arg == NUL)
3928 {
3929 xp->xp_context = EXPAND_NOTHING;
3930 return NULL;
3931 }
3932 return skipwhite(arg);
3933
Bram Moolenaar4f688582007-07-24 12:34:30 +00003934#ifdef FEAT_CMDL_COMPL
3935# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 case CMD_match:
3937 if (*arg == NUL || !ends_excmd(*arg))
3938 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003939 /* also complete "None" */
3940 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 arg = skipwhite(skiptowhite(arg));
3942 if (*arg != NUL)
3943 {
3944 xp->xp_context = EXPAND_NOTHING;
3945 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3946 }
3947 }
3948 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951/*
3952 * All completion for the +cmdline_compl feature goes here.
3953 */
3954
3955# ifdef FEAT_USR_CMDS
3956 case CMD_command:
3957 /* Check for attributes */
3958 while (*arg == '-')
3959 {
3960 arg++; /* Skip "-" */
3961 p = skiptowhite(arg);
3962 if (*p == NUL)
3963 {
3964 /* Cursor is still in the attribute */
3965 p = vim_strchr(arg, '=');
3966 if (p == NULL)
3967 {
3968 /* No "=", so complete attribute names */
3969 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3970 xp->xp_pattern = arg;
3971 return NULL;
3972 }
3973
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003974 /* For the -complete, -nargs and -addr attributes, we complete
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 * their arguments as well.
3976 */
3977 if (STRNICMP(arg, "complete", p - arg) == 0)
3978 {
3979 xp->xp_context = EXPAND_USER_COMPLETE;
3980 xp->xp_pattern = p + 1;
3981 return NULL;
3982 }
3983 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3984 {
3985 xp->xp_context = EXPAND_USER_NARGS;
3986 xp->xp_pattern = p + 1;
3987 return NULL;
3988 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003989 else if (STRNICMP(arg, "addr", p - arg) == 0)
3990 {
3991 xp->xp_context = EXPAND_USER_ADDR_TYPE;
3992 xp->xp_pattern = p + 1;
3993 return NULL;
3994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 return NULL;
3996 }
3997 arg = skipwhite(p);
3998 }
3999
4000 /* After the attributes comes the new command name */
4001 p = skiptowhite(arg);
4002 if (*p == NUL)
4003 {
4004 xp->xp_context = EXPAND_USER_COMMANDS;
4005 xp->xp_pattern = arg;
4006 break;
4007 }
4008
4009 /* And finally comes a normal command */
4010 return skipwhite(p);
4011
4012 case CMD_delcommand:
4013 xp->xp_context = EXPAND_USER_COMMANDS;
4014 xp->xp_pattern = arg;
4015 break;
4016# endif
4017
4018 case CMD_global:
4019 case CMD_vglobal:
4020 delim = *arg; /* get the delimiter */
4021 if (delim)
4022 ++arg; /* skip delimiter if there is one */
4023
4024 while (arg[0] != NUL && arg[0] != delim)
4025 {
4026 if (arg[0] == '\\' && arg[1] != NUL)
4027 ++arg;
4028 ++arg;
4029 }
4030 if (arg[0] != NUL)
4031 return arg + 1;
4032 break;
4033 case CMD_and:
4034 case CMD_substitute:
4035 delim = *arg;
4036 if (delim)
4037 {
4038 /* skip "from" part */
4039 ++arg;
4040 arg = skip_regexp(arg, delim, p_magic, NULL);
4041 }
4042 /* skip "to" part */
4043 while (arg[0] != NUL && arg[0] != delim)
4044 {
4045 if (arg[0] == '\\' && arg[1] != NUL)
4046 ++arg;
4047 ++arg;
4048 }
4049 if (arg[0] != NUL) /* skip delimiter */
4050 ++arg;
4051 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
4052 ++arg;
4053 if (arg[0] != NUL)
4054 return arg;
4055 break;
4056 case CMD_isearch:
4057 case CMD_dsearch:
4058 case CMD_ilist:
4059 case CMD_dlist:
4060 case CMD_ijump:
4061 case CMD_psearch:
4062 case CMD_djump:
4063 case CMD_isplit:
4064 case CMD_dsplit:
4065 arg = skipwhite(skipdigits(arg)); /* skip count */
4066 if (*arg == '/') /* Match regexp, not just whole words */
4067 {
4068 for (++arg; *arg && *arg != '/'; arg++)
4069 if (*arg == '\\' && arg[1] != NUL)
4070 arg++;
4071 if (*arg)
4072 {
4073 arg = skipwhite(arg + 1);
4074
4075 /* Check for trailing illegal characters */
4076 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
4077 xp->xp_context = EXPAND_NOTHING;
4078 else
4079 return arg;
4080 }
4081 }
4082 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004083
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 case CMD_autocmd:
4085 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00004087 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 case CMD_set:
4090 set_context_in_set_cmd(xp, arg, 0);
4091 break;
4092 case CMD_setglobal:
4093 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
4094 break;
4095 case CMD_setlocal:
4096 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
4097 break;
4098 case CMD_tag:
4099 case CMD_stag:
4100 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00004101 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 case CMD_tselect:
4103 case CMD_stselect:
4104 case CMD_ptselect:
4105 case CMD_tjump:
4106 case CMD_stjump:
4107 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004108 if (*p_wop != NUL)
4109 xp->xp_context = EXPAND_TAGS_LISTFILES;
4110 else
4111 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 xp->xp_pattern = arg;
4113 break;
4114 case CMD_augroup:
4115 xp->xp_context = EXPAND_AUGROUP;
4116 xp->xp_pattern = arg;
4117 break;
4118#ifdef FEAT_SYN_HL
4119 case CMD_syntax:
4120 set_context_in_syntax_cmd(xp, arg);
4121 break;
4122#endif
4123#ifdef FEAT_EVAL
4124 case CMD_let:
4125 case CMD_if:
4126 case CMD_elseif:
4127 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00004128 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 case CMD_echo:
4130 case CMD_echon:
4131 case CMD_execute:
4132 case CMD_echomsg:
4133 case CMD_echoerr:
4134 case CMD_call:
4135 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01004136 case CMD_cexpr:
4137 case CMD_caddexpr:
4138 case CMD_cgetexpr:
4139 case CMD_lexpr:
4140 case CMD_laddexpr:
4141 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004142 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 break;
4144
4145 case CMD_unlet:
4146 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4147 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02004148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 xp->xp_context = EXPAND_USER_VARS;
4150 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02004151
4152 if (*xp->xp_pattern == '$')
4153 {
4154 xp->xp_context = EXPAND_ENV_VARS;
4155 ++xp->xp_pattern;
4156 }
4157
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 break;
4159
4160 case CMD_function:
4161 case CMD_delfunction:
4162 xp->xp_context = EXPAND_USER_FUNC;
4163 xp->xp_pattern = arg;
4164 break;
4165
4166 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00004167 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 break;
4169#endif
4170 case CMD_highlight:
4171 set_context_in_highlight_cmd(xp, arg);
4172 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004173#ifdef FEAT_CSCOPE
4174 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00004175 case CMD_lcscope:
4176 case CMD_scscope:
4177 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004178 break;
4179#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004180#ifdef FEAT_SIGNS
4181 case CMD_sign:
4182 set_context_in_sign_cmd(xp, arg);
4183 break;
4184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 case CMD_bdelete:
4186 case CMD_bwipeout:
4187 case CMD_bunload:
4188 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4189 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004190 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 case CMD_buffer:
4192 case CMD_sbuffer:
4193 case CMD_checktime:
4194 xp->xp_context = EXPAND_BUFFERS;
4195 xp->xp_pattern = arg;
4196 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197#ifdef FEAT_USR_CMDS
4198 case CMD_USER:
4199 case CMD_USER_BUF:
4200 if (compl != EXPAND_NOTHING)
4201 {
4202 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004203 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
4205# ifdef FEAT_MENU
4206 if (compl == EXPAND_MENUS)
4207 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4208# endif
4209 if (compl == EXPAND_COMMANDS)
4210 return arg;
4211 if (compl == EXPAND_MAPPINGS)
4212 return set_context_in_map_cmd(xp, (char_u *)"map",
4213 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004214 /* Find start of last argument. */
4215 p = arg;
4216 while (*p)
4217 {
4218 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004219 /* argument starts after a space */
4220 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004221 else if (*p == '\\' && *(p + 1) != NUL)
4222 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004223 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 xp->xp_pattern = arg;
4226 }
4227 xp->xp_context = compl;
4228 }
4229 break;
4230#endif
4231 case CMD_map: case CMD_noremap:
4232 case CMD_nmap: case CMD_nnoremap:
4233 case CMD_vmap: case CMD_vnoremap:
4234 case CMD_omap: case CMD_onoremap:
4235 case CMD_imap: case CMD_inoremap:
4236 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004237 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004238 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004239 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004240 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004242 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 case CMD_unmap:
4244 case CMD_nunmap:
4245 case CMD_vunmap:
4246 case CMD_ounmap:
4247 case CMD_iunmap:
4248 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004249 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004250 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004251 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004252 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004254 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004255 case CMD_mapclear:
4256 case CMD_nmapclear:
4257 case CMD_vmapclear:
4258 case CMD_omapclear:
4259 case CMD_imapclear:
4260 case CMD_cmapclear:
4261 case CMD_lmapclear:
4262 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004263 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004264 case CMD_xmapclear:
4265 xp->xp_context = EXPAND_MAPCLEAR;
4266 xp->xp_pattern = arg;
4267 break;
4268
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 case CMD_abbreviate: case CMD_noreabbrev:
4270 case CMD_cabbrev: case CMD_cnoreabbrev:
4271 case CMD_iabbrev: case CMD_inoreabbrev:
4272 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004273 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 case CMD_unabbreviate:
4275 case CMD_cunabbrev:
4276 case CMD_iunabbrev:
4277 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004278 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279#ifdef FEAT_MENU
4280 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4281 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4282 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4283 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4284 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4285 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4286 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004287 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 case CMD_tmenu: case CMD_tunmenu:
4289 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4290 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4291#endif
4292
4293 case CMD_colorscheme:
4294 xp->xp_context = EXPAND_COLORS;
4295 xp->xp_pattern = arg;
4296 break;
4297
4298 case CMD_compiler:
4299 xp->xp_context = EXPAND_COMPILER;
4300 xp->xp_pattern = arg;
4301 break;
4302
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004303 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004304 xp->xp_context = EXPAND_OWNSYNTAX;
4305 xp->xp_pattern = arg;
4306 break;
4307
4308 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004309 xp->xp_context = EXPAND_FILETYPE;
4310 xp->xp_pattern = arg;
4311 break;
4312
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004313 case CMD_packadd:
4314 xp->xp_context = EXPAND_PACKADD;
4315 xp->xp_pattern = arg;
4316 break;
4317
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4319 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4320 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004321 p = skiptowhite(arg);
4322 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
4324 xp->xp_context = EXPAND_LANGUAGE;
4325 xp->xp_pattern = arg;
4326 }
4327 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004328 {
4329 if ( STRNCMP(arg, "messages", p - arg) == 0
4330 || STRNCMP(arg, "ctype", p - arg) == 0
4331 || STRNCMP(arg, "time", p - arg) == 0)
4332 {
4333 xp->xp_context = EXPAND_LOCALES;
4334 xp->xp_pattern = skipwhite(p);
4335 }
4336 else
4337 xp->xp_context = EXPAND_NOTHING;
4338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 break;
4340#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004341#if defined(FEAT_PROFILE)
4342 case CMD_profile:
4343 set_context_in_profile_cmd(xp, arg);
4344 break;
4345#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004346 case CMD_behave:
4347 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004348 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004351 case CMD_messages:
4352 xp->xp_context = EXPAND_MESSAGES;
4353 xp->xp_pattern = arg;
4354 break;
4355
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004356#if defined(FEAT_CMDHIST)
4357 case CMD_history:
4358 xp->xp_context = EXPAND_HISTORY;
4359 xp->xp_pattern = arg;
4360 break;
4361#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004362#if defined(FEAT_PROFILE)
4363 case CMD_syntime:
4364 xp->xp_context = EXPAND_SYNTIME;
4365 xp->xp_pattern = arg;
4366 break;
4367#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004368
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004369 case CMD_argdelete:
4370 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4371 arg = xp->xp_pattern + 1;
4372 xp->xp_context = EXPAND_ARGLIST;
4373 xp->xp_pattern = arg;
4374 break;
4375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376#endif /* FEAT_CMDL_COMPL */
4377
4378 default:
4379 break;
4380 }
4381 return NULL;
4382}
4383
4384/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004385 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 *
4387 * Backslashed delimiters after / or ? will be skipped, and commands will
4388 * not be expanded between /'s and ?'s or after "'".
4389 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004390 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 * Returns the "cmd" pointer advanced to beyond the range.
4392 */
4393 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004394skip_range(
4395 char_u *cmd,
4396 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004398 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004400 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004402 if (*cmd == '\\')
4403 {
4404 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4405 ++cmd;
4406 else
4407 break;
4408 }
4409 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
4411 if (*++cmd == NUL && ctx != NULL)
4412 *ctx = EXPAND_NOTHING;
4413 }
4414 else if (*cmd == '/' || *cmd == '?')
4415 {
4416 delim = *cmd++;
4417 while (*cmd != NUL && *cmd != delim)
4418 if (*cmd++ == '\\' && *cmd != NUL)
4419 ++cmd;
4420 if (*cmd == NUL && ctx != NULL)
4421 *ctx = EXPAND_NOTHING;
4422 }
4423 if (*cmd != NUL)
4424 ++cmd;
4425 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004426
4427 /* Skip ":" and white space. */
4428 while (*cmd == ':')
4429 cmd = skipwhite(cmd + 1);
4430
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 return cmd;
4432}
4433
4434/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004435 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 *
4437 * Set ptr to the next character after the part that was interpreted.
4438 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004439 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 *
4441 * Return MAXLNUM when no Ex address was found.
4442 */
4443 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004444get_address(
4445 exarg_T *eap UNUSED,
4446 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004447 int addr_type, // flag: one of ADDR_LINES, ...
4448 int skip, // only skip the address, don't use it
4449 int silent, // no errors or side effects
4450 int to_other_file, // flag: may jump to other file
4451 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452{
4453 int c;
4454 int i;
4455 long n;
4456 char_u *cmd;
4457 pos_T pos;
4458 pos_T *fp;
4459 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004460 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 cmd = skipwhite(*ptr);
4463 lnum = MAXLNUM;
4464 do
4465 {
4466 switch (*cmd)
4467 {
4468 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004469 ++cmd;
4470 switch (addr_type)
4471 {
4472 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 lnum = curwin->w_cursor.lnum;
4474 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004475 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004476 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004477 break;
4478 case ADDR_ARGUMENTS:
4479 lnum = curwin->w_arg_idx + 1;
4480 break;
4481 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004482 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 lnum = curbuf->b_fnum;
4484 break;
4485 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004486 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004488 case ADDR_TABS_RELATIVE:
4489 EMSG(_(e_invrange));
4490 cmd = NULL;
4491 goto error;
4492 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004493#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004494 case ADDR_QUICKFIX:
4495 lnum = qf_get_cur_valid_idx(eap);
4496 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004497#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004498 }
4499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004502 ++cmd;
4503 switch (addr_type)
4504 {
4505 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 lnum = curbuf->b_ml.ml_line_count;
4507 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004509 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 break;
4511 case ADDR_ARGUMENTS:
4512 lnum = ARGCOUNT;
4513 break;
4514 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004515 buf = lastbuf;
4516 while (buf->b_ml.ml_mfp == NULL)
4517 {
4518 if (buf->b_prev == NULL)
4519 break;
4520 buf = buf->b_prev;
4521 }
4522 lnum = buf->b_fnum;
4523 break;
4524 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004525 lnum = lastbuf->b_fnum;
4526 break;
4527 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004528 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004529 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004530 case ADDR_TABS_RELATIVE:
4531 EMSG(_(e_invrange));
4532 cmd = NULL;
4533 goto error;
4534 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004535#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004536 case ADDR_QUICKFIX:
4537 lnum = qf_get_size(eap);
4538 if (lnum == 0)
4539 lnum = 1;
4540 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004541#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004542 }
4543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004546 if (*++cmd == NUL)
4547 {
4548 cmd = NULL;
4549 goto error;
4550 }
4551 if (addr_type != ADDR_LINES)
4552 {
4553 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004554 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004555 goto error;
4556 }
4557 if (skip)
4558 ++cmd;
4559 else
4560 {
4561 /* Only accept a mark in another file when it is
4562 * used by itself: ":'M". */
4563 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4564 ++cmd;
4565 if (fp == (pos_T *)-1)
4566 /* Jumped to another file. */
4567 lnum = curwin->w_cursor.lnum;
4568 else
4569 {
4570 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
4572 cmd = NULL;
4573 goto error;
4574 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004575 lnum = fp->lnum;
4576 }
4577 }
4578 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
4580 case '/':
4581 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 c = *cmd++;
4583 if (addr_type != ADDR_LINES)
4584 {
4585 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004586 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004587 goto error;
4588 }
4589 if (skip) /* skip "/pat/" */
4590 {
4591 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4592 if (*cmd == c)
4593 ++cmd;
4594 }
4595 else
4596 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004597 int flags;
4598
4599 pos = curwin->w_cursor; // save curwin->w_cursor
4600
4601 // When '/' or '?' follows another address, start from
4602 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 if (lnum != MAXLNUM)
4604 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004605
4606 // Start a forward search at the end of the line (unless
4607 // before the first line).
4608 // Start a backward search at the start of the line.
4609 // This makes sure we never match in the current
4610 // line, and can match anywhere in the
4611 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004612 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004613 curwin->w_cursor.col = MAXCOL;
4614 else
4615 curwin->w_cursor.col = 0;
4616 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004617 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4618 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004619 {
4620 curwin->w_cursor = pos;
4621 cmd = NULL;
4622 goto error;
4623 }
4624 lnum = curwin->w_cursor.lnum;
4625 curwin->w_cursor = pos;
4626 /* adjust command string pointer */
4627 cmd += searchcmdlen;
4628 }
4629 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004632 ++cmd;
4633 if (addr_type != ADDR_LINES)
4634 {
4635 EMSG(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004636 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 goto error;
4638 }
4639 if (*cmd == '&')
4640 i = RE_SUBST;
4641 else if (*cmd == '?' || *cmd == '/')
4642 i = RE_SEARCH;
4643 else
4644 {
4645 EMSG(_(e_backslash));
4646 cmd = NULL;
4647 goto error;
4648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004650 if (!skip)
4651 {
4652 /*
4653 * When search follows another address, start from
4654 * there.
4655 */
4656 if (lnum != MAXLNUM)
4657 pos.lnum = lnum;
4658 else
4659 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004661 /*
4662 * Start the search just like for the above
4663 * do_search().
4664 */
4665 if (*cmd != '?')
4666 pos.col = MAXCOL;
4667 else
4668 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004669#ifdef FEAT_VIRTUALEDIT
4670 pos.coladd = 0;
4671#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004672 if (searchit(curwin, curbuf, &pos,
4673 *cmd == '?' ? BACKWARD : FORWARD,
4674 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004675 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004676 lnum = pos.lnum;
4677 else
4678 {
4679 cmd = NULL;
4680 goto error;
4681 }
4682 }
4683 ++cmd;
4684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
4686 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004687 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4688 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
4690
4691 for (;;)
4692 {
4693 cmd = skipwhite(cmd);
4694 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4695 break;
4696
4697 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004698 {
4699 switch (addr_type)
4700 {
4701 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004702 /* "+1" is same as ".+1" */
4703 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004704 break;
4705 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004706 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004707 break;
4708 case ADDR_ARGUMENTS:
4709 lnum = curwin->w_arg_idx + 1;
4710 break;
4711 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004712 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004713 lnum = curbuf->b_fnum;
4714 break;
4715 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004716 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004717 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004718 case ADDR_TABS_RELATIVE:
4719 lnum = 1;
4720 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004721#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004722 case ADDR_QUICKFIX:
4723 lnum = qf_get_cur_valid_idx(eap);
4724 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004725#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004726 }
4727 }
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (VIM_ISDIGIT(*cmd))
4730 i = '+'; /* "number" is same as "+number" */
4731 else
4732 i = *cmd++;
4733 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4734 n = 1;
4735 else
4736 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004737
4738 if (addr_type == ADDR_TABS_RELATIVE)
4739 {
4740 EMSG(_(e_invrange));
4741 cmd = NULL;
4742 goto error;
4743 }
4744 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004745 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004746 lnum = compute_buffer_local_count(
4747 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004749 {
4750#ifdef FEAT_FOLDING
4751 /* Relative line addressing, need to adjust for folded lines
4752 * now, but only do it after the first address. */
4753 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4754 && address_count >= 2)
4755 (void)hasFolding(lnum, NULL, &lnum);
4756#endif
4757 if (i == '-')
4758 lnum -= n;
4759 else
4760 lnum += n;
4761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763 } while (*cmd == '/' || *cmd == '?');
4764
4765error:
4766 *ptr = cmd;
4767 return lnum;
4768}
4769
4770/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004771 * Get flags from an Ex command argument.
4772 */
4773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004774get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004775{
4776 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4777 {
4778 if (*eap->arg == 'l')
4779 eap->flags |= EXFLAG_LIST;
4780 else if (*eap->arg == 'p')
4781 eap->flags |= EXFLAG_PRINT;
4782 else
4783 eap->flags |= EXFLAG_NR;
4784 eap->arg = skipwhite(eap->arg + 1);
4785 }
4786}
4787
4788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 * Function called for command which is Not Implemented. NI!
4790 */
4791 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004792ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 if (!eap->skip)
4795 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4796}
4797
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004798#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799/*
4800 * Function called for script command which is Not Implemented. NI!
4801 * Skips over ":perl <<EOF" constructs.
4802 */
4803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004804ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
4806 if (!eap->skip)
4807 ex_ni(eap);
4808 else
4809 vim_free(script_get(eap, eap->arg));
4810}
4811#endif
4812
4813/*
4814 * Check range in Ex command for validity.
4815 * Return NULL when valid, error message when invalid.
4816 */
4817 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004818invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004820 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if ( eap->line1 < 0
4822 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004823 || eap->line1 > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return (char_u *)_(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004825
4826 if (eap->argt & RANGE)
4827 {
4828 switch(eap->addr_type)
4829 {
4830 case ADDR_LINES:
4831 if (!(eap->argt & NOTADR)
4832 && eap->line2 > curbuf->b_ml.ml_line_count
4833#ifdef FEAT_DIFF
4834 + (eap->cmdidx == CMD_diffget)
4835#endif
4836 )
4837 return (char_u *)_(e_invrange);
4838 break;
4839 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004840 /* add 1 if ARGCOUNT is 0 */
4841 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004842 return (char_u *)_(e_invrange);
4843 break;
4844 case ADDR_BUFFERS:
4845 if (eap->line1 < firstbuf->b_fnum
4846 || eap->line2 > lastbuf->b_fnum)
4847 return (char_u *)_(e_invrange);
4848 break;
4849 case ADDR_LOADED_BUFFERS:
4850 buf = firstbuf;
4851 while (buf->b_ml.ml_mfp == NULL)
4852 {
4853 if (buf->b_next == NULL)
4854 return (char_u *)_(e_invrange);
4855 buf = buf->b_next;
4856 }
4857 if (eap->line1 < buf->b_fnum)
4858 return (char_u *)_(e_invrange);
4859 buf = lastbuf;
4860 while (buf->b_ml.ml_mfp == NULL)
4861 {
4862 if (buf->b_prev == NULL)
4863 return (char_u *)_(e_invrange);
4864 buf = buf->b_prev;
4865 }
4866 if (eap->line2 > buf->b_fnum)
4867 return (char_u *)_(e_invrange);
4868 break;
4869 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004870 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004871 return (char_u *)_(e_invrange);
4872 break;
4873 case ADDR_TABS:
4874 if (eap->line2 > LAST_TAB_NR)
4875 return (char_u *)_(e_invrange);
4876 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004877 case ADDR_TABS_RELATIVE:
4878 /* Do nothing */
4879 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004880#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004881 case ADDR_QUICKFIX:
4882 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
4883 return (char_u *)_(e_invrange);
4884 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004885#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004886 }
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return NULL;
4889}
4890
4891/*
4892 * Correct the range for zero line number, if required.
4893 */
4894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004895correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
4897 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4898 {
4899 if (eap->line1 == 0)
4900 eap->line1 = 1;
4901 if (eap->line2 == 0)
4902 eap->line2 = 1;
4903 }
4904}
4905
Bram Moolenaar748bf032005-02-02 23:04:36 +00004906#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004907/*
4908 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4909 * pattern. Otherwise return eap->arg.
4910 */
4911 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004912skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004913{
4914 char_u *p = eap->arg;
4915
Bram Moolenaara37420f2006-02-04 22:37:47 +00004916 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4917 || eap->cmdidx == CMD_vimgrepadd
4918 || eap->cmdidx == CMD_lvimgrepadd
4919 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004920 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004921 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004922 if (p == NULL)
4923 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004924 }
4925 return p;
4926}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004927
4928/*
4929 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4930 * in the command line, so that things like % get expanded.
4931 */
4932 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004933replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004934{
4935 char_u *new_cmdline;
4936 char_u *program;
4937 char_u *pos;
4938 char_u *ptr;
4939 int len;
4940 int i;
4941
4942 /*
4943 * Don't do it when ":vimgrep" is used for ":grep".
4944 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004945 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4946 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4947 || eap->cmdidx == CMD_grepadd
4948 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004949 && !grep_internal(eap->cmdidx))
4950 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004951 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4952 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004953 {
4954 if (*curbuf->b_p_gp == NUL)
4955 program = p_gp;
4956 else
4957 program = curbuf->b_p_gp;
4958 }
4959 else
4960 {
4961 if (*curbuf->b_p_mp == NUL)
4962 program = p_mp;
4963 else
4964 program = curbuf->b_p_mp;
4965 }
4966
4967 p = skipwhite(p);
4968
4969 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4970 {
4971 /* replace $* by given arguments */
4972 i = 1;
4973 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4974 ++i;
4975 len = (int)STRLEN(p);
4976 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4977 if (new_cmdline == NULL)
4978 return NULL; /* out of memory */
4979 ptr = new_cmdline;
4980 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4981 {
4982 i = (int)(pos - program);
4983 STRNCPY(ptr, program, i);
4984 STRCPY(ptr += i, p);
4985 ptr += len;
4986 program = pos + 2;
4987 }
4988 STRCPY(ptr, program);
4989 }
4990 else
4991 {
4992 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4993 if (new_cmdline == NULL)
4994 return NULL; /* out of memory */
4995 STRCPY(new_cmdline, program);
4996 STRCAT(new_cmdline, " ");
4997 STRCAT(new_cmdline, p);
4998 }
4999 msg_make(p);
5000
5001 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
5002 vim_free(*cmdlinep);
5003 *cmdlinep = new_cmdline;
5004 p = new_cmdline;
5005 }
5006 return p;
5007}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005008#endif
5009
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010/*
5011 * Expand file name in Ex command argument.
5012 * Return FAIL for failure, OK otherwise.
5013 */
5014 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005015expand_filename(
5016 exarg_T *eap,
5017 char_u **cmdlinep,
5018 char_u **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
5020 int has_wildcards; /* need to expand wildcards */
5021 char_u *repl;
5022 int srclen;
5023 char_u *p;
5024 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005025 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
Bram Moolenaar748bf032005-02-02 23:04:36 +00005027#ifdef FEAT_QUICKFIX
5028 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
5029 p = skip_grep_pat(eap);
5030#else
5031 p = eap->arg;
5032#endif
5033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 /*
5035 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5036 * the file name contains a wildcard it should not cause expanding.
5037 * (it will be expanded anyway if there is a wildcard before replacing).
5038 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005039 has_wildcards = mch_has_wildcard(p);
5040 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005042#ifdef FEAT_EVAL
5043 /* Skip over `=expr`, wildcards in it are not expanded. */
5044 if (p[0] == '`' && p[1] == '=')
5045 {
5046 p += 2;
5047 (void)skip_expr(&p);
5048 if (*p == '`')
5049 ++p;
5050 continue;
5051 }
5052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 /*
5054 * Quick check if this cannot be the start of a special string.
5055 * Also removes backslash before '%', '#' and '<'.
5056 */
5057 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5058 {
5059 ++p;
5060 continue;
5061 }
5062
5063 /*
5064 * Try to find a match at this position.
5065 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005066 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
5067 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (*errormsgp != NULL) /* error detected */
5069 return FAIL;
5070 if (repl == NULL) /* no match found */
5071 {
5072 p += srclen;
5073 continue;
5074 }
5075
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005076 /* Wildcards won't be expanded below, the replacement is taken
5077 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
5078 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5079 {
5080 char_u *l = repl;
5081
5082 repl = expand_env_save(repl);
5083 vim_free(l);
5084 }
5085
Bram Moolenaar63b92542007-03-27 14:57:09 +00005086 /* Need to escape white space et al. with a backslash.
5087 * Don't do this for:
5088 * - replacement that already has been escaped: "##"
5089 * - shell commands (may have to use quotes instead).
5090 * - non-unix systems when there is a single argument (spaces don't
5091 * separate arguments then).
5092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005094 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 && eap->cmdidx != CMD_grep
5097 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005098 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005099 && eap->cmdidx != CMD_lgrep
5100 && eap->cmdidx != CMD_lgrepadd
5101 && eap->cmdidx != CMD_lmake
5102 && eap->cmdidx != CMD_make
5103 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#ifndef UNIX
5105 && !(eap->argt & NOSPC)
5106#endif
5107 )
5108 {
5109 char_u *l;
5110#ifdef BACKSLASH_IN_FILENAME
5111 /* Don't escape a backslash here, because rem_backslash() doesn't
5112 * remove it later. */
5113 static char_u *nobslash = (char_u *)" \t\"|";
5114# define ESCAPE_CHARS nobslash
5115#else
5116# define ESCAPE_CHARS escape_chars
5117#endif
5118
5119 for (l = repl; *l; ++l)
5120 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5121 {
5122 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5123 if (l != NULL)
5124 {
5125 vim_free(repl);
5126 repl = l;
5127 }
5128 break;
5129 }
5130 }
5131
5132 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02005133 if ((eap->usefilter || eap->cmdidx == CMD_bang
5134 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005135 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
5137 char_u *l;
5138
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005139 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (l != NULL)
5141 {
5142 vim_free(repl);
5143 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 }
5146
5147 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5148 vim_free(repl);
5149 if (p == NULL)
5150 return FAIL;
5151 }
5152
5153 /*
5154 * One file argument: Expand wildcards.
5155 * Don't do this with ":r !command" or ":w !command".
5156 */
5157 if ((eap->argt & NOSPC) && !eap->usefilter)
5158 {
5159 /*
5160 * May do this twice:
5161 * 1. Replace environment variables.
5162 * 2. Replace any other wildcards, remove backslashes.
5163 */
5164 for (n = 1; n <= 2; ++n)
5165 {
5166 if (n == 2)
5167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 /*
5169 * Halve the number of backslashes (this is Vi compatible).
5170 * For Unix and OS/2, when wildcards are expanded, this is
5171 * done by ExpandOne() below.
5172 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005173#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (!has_wildcards)
5175#endif
5176 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178
5179 if (has_wildcards)
5180 {
5181 if (n == 1)
5182 {
5183 /*
5184 * First loop: May expand environment variables. This
5185 * can be done much faster with expand_env() than with
5186 * something else (e.g., calling a shell).
5187 * After expanding environment variables, check again
5188 * if there are still wildcards present.
5189 */
5190 if (vim_strchr(eap->arg, '$') != NULL
5191 || vim_strchr(eap->arg, '~') != NULL)
5192 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005193 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005194 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 has_wildcards = mch_has_wildcard(NameBuff);
5196 p = NameBuff;
5197 }
5198 else
5199 p = NULL;
5200 }
5201 else /* n == 2 */
5202 {
5203 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005204 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
5206 ExpandInit(&xpc);
5207 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005208 if (p_wic)
5209 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005211 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (p == NULL)
5213 return FAIL;
5214 }
5215 if (p != NULL)
5216 {
5217 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5218 p, cmdlinep);
5219 if (n == 2) /* p came from ExpandOne() */
5220 vim_free(p);
5221 }
5222 }
5223 }
5224 }
5225 return OK;
5226}
5227
5228/*
5229 * Replace part of the command line, keeping eap->cmd, eap->arg and
5230 * eap->nextcmd correct.
5231 * "src" points to the part that is to be replaced, of length "srclen".
5232 * "repl" is the replacement string.
5233 * Returns a pointer to the character after the replaced string.
5234 * Returns NULL for failure.
5235 */
5236 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005237repl_cmdline(
5238 exarg_T *eap,
5239 char_u *src,
5240 int srclen,
5241 char_u *repl,
5242 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244 int len;
5245 int i;
5246 char_u *new_cmdline;
5247
5248 /*
5249 * The new command line is build in new_cmdline[].
5250 * First allocate it.
5251 * Careful: a "+cmd" argument may have been NUL terminated.
5252 */
5253 len = (int)STRLEN(repl);
5254 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005255 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5257 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5258 return NULL; /* out of memory! */
5259
5260 /*
5261 * Copy the stuff before the expanded part.
5262 * Copy the expanded stuff.
5263 * Copy what came after the expanded part.
5264 * Copy the next commands, if there are any.
5265 */
5266 i = (int)(src - *cmdlinep); /* length of part before match */
5267 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 mch_memmove(new_cmdline + i, repl, (size_t)len);
5270 i += len; /* remember the end of the string */
5271 STRCPY(new_cmdline + i, src + srclen);
5272 src = new_cmdline + i; /* remember where to continue */
5273
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005274 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
5276 i = (int)STRLEN(new_cmdline) + 1;
5277 STRCPY(new_cmdline + i, eap->nextcmd);
5278 eap->nextcmd = new_cmdline + i;
5279 }
5280 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5281 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5282 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5283 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5284 vim_free(*cmdlinep);
5285 *cmdlinep = new_cmdline;
5286
5287 return src;
5288}
5289
5290/*
5291 * Check for '|' to separate commands and '"' to start comments.
5292 */
5293 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005294separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
5296 char_u *p;
5297
Bram Moolenaar86b68352004-12-27 21:59:20 +00005298#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005299 p = skip_grep_pat(eap);
5300#else
5301 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005302#endif
5303
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005304 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
5306 if (*p == Ctrl_V)
5307 {
5308 if (eap->argt & (USECTRLV | XFILE))
5309 ++p; /* skip CTRL-V and next char */
5310 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005311 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005312 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (*p == NUL) /* stop at NUL after CTRL-V */
5314 break;
5315 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005316
5317#ifdef FEAT_EVAL
5318 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005319 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005320 {
5321 p += 2;
5322 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005323 }
5324#endif
5325
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 /* Check for '"': start of comment or '|': next command */
5327 /* :@" and :*" do not start a comment!
5328 * :redir @" doesn't either. */
5329 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5330 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5331 || p != eap->arg)
5332 && (eap->cmdidx != CMD_redir
5333 || p != eap->arg + 1 || p[-1] != '@'))
5334 || *p == '|' || *p == '\n')
5335 {
5336 /*
5337 * We remove the '\' before the '|', unless USECTRLV is used
5338 * AND 'b' is present in 'cpoptions'.
5339 */
5340 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5341 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5342 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005343 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 --p;
5345 }
5346 else
5347 {
5348 eap->nextcmd = check_nextcmd(p);
5349 *p = NUL;
5350 break;
5351 }
5352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005354
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5356 del_trailing_spaces(eap->arg);
5357}
5358
5359/*
5360 * get + command from ex argument
5361 */
5362 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005363getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
5365 char_u *arg = *argp;
5366 char_u *command = NULL;
5367
5368 if (*arg == '+') /* +[command] */
5369 {
5370 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005371 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 command = dollar_command;
5373 else
5374 {
5375 command = arg;
5376 arg = skip_cmd_arg(command, TRUE);
5377 if (*arg != NUL)
5378 *arg++ = NUL; /* terminate command with NUL */
5379 }
5380
5381 arg = skipwhite(arg); /* skip over spaces */
5382 *argp = arg;
5383 }
5384 return command;
5385}
5386
5387/*
5388 * Find end of "+command" argument. Skip over "\ " and "\\".
5389 */
5390 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005391skip_cmd_arg(
5392 char_u *p,
5393 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
5395 while (*p && !vim_isspace(*p))
5396 {
5397 if (*p == '\\' && p[1] != NUL)
5398 {
5399 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005400 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 else
5402 ++p;
5403 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005404 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406 return p;
5407}
5408
Bram Moolenaar86676c92018-04-05 18:56:48 +02005409#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005410 int
5411get_bad_opt(char_u *p, exarg_T *eap)
5412{
5413 if (STRICMP(p, "keep") == 0)
5414 eap->bad_char = BAD_KEEP;
5415 else if (STRICMP(p, "drop") == 0)
5416 eap->bad_char = BAD_DROP;
5417 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5418 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005419 else
5420 return FAIL;
5421 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005422}
Bram Moolenaar86676c92018-04-05 18:56:48 +02005423#endif
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425/*
5426 * Get "++opt=arg" argument.
5427 * Return FAIL or OK.
5428 */
5429 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005430getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 char_u *arg = eap->arg + 2;
5433 int *pp = NULL;
5434#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005435 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 char_u *p;
5437#endif
5438
5439 /* ":edit ++[no]bin[ary] file" */
5440 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5441 {
5442 if (*arg == 'n')
5443 {
5444 arg += 2;
5445 eap->force_bin = FORCE_NOBIN;
5446 }
5447 else
5448 eap->force_bin = FORCE_BIN;
5449 if (!checkforcmd(&arg, "binary", 3))
5450 return FAIL;
5451 eap->arg = skipwhite(arg);
5452 return OK;
5453 }
5454
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005455 /* ":read ++edit file" */
5456 if (STRNCMP(arg, "edit", 4) == 0)
5457 {
5458 eap->read_edit = TRUE;
5459 eap->arg = skipwhite(arg + 4);
5460 return OK;
5461 }
5462
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (STRNCMP(arg, "ff", 2) == 0)
5464 {
5465 arg += 2;
5466 pp = &eap->force_ff;
5467 }
5468 else if (STRNCMP(arg, "fileformat", 10) == 0)
5469 {
5470 arg += 10;
5471 pp = &eap->force_ff;
5472 }
5473#ifdef FEAT_MBYTE
5474 else if (STRNCMP(arg, "enc", 3) == 0)
5475 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005476 if (STRNCMP(arg, "encoding", 8) == 0)
5477 arg += 8;
5478 else
5479 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 pp = &eap->force_enc;
5481 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005482 else if (STRNCMP(arg, "bad", 3) == 0)
5483 {
5484 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005485 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487#endif
5488
5489 if (pp == NULL || *arg != '=')
5490 return FAIL;
5491
5492 ++arg;
5493 *pp = (int)(arg - eap->cmd);
5494 arg = skip_cmd_arg(arg, FALSE);
5495 eap->arg = skipwhite(arg);
5496 *arg = NUL;
5497
5498#ifdef FEAT_MBYTE
5499 if (pp == &eap->force_ff)
5500 {
5501#endif
5502 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5503 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005504 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505#ifdef FEAT_MBYTE
5506 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005507 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 {
5509 /* Make 'fileencoding' lower case. */
5510 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5511 *p = TOLOWER_ASC(*p);
5512 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005513 else
5514 {
5515 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5516 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005517 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005518 return FAIL;
5519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520#endif
5521
5522 return OK;
5523}
5524
5525/*
5526 * ":abbreviate" and friends.
5527 */
5528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 do_exmap(eap, TRUE); /* almost the same as mapping */
5532}
5533
5534/*
5535 * ":map" and friends.
5536 */
5537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005538ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 /*
5541 * If we are sourcing .exrc or .vimrc in current directory we
5542 * print the mappings for security reasons.
5543 */
5544 if (secure)
5545 {
5546 secure = 2;
5547 msg_outtrans(eap->cmd);
5548 msg_putchar('\n');
5549 }
5550 do_exmap(eap, FALSE);
5551}
5552
5553/*
5554 * ":unmap" and friends.
5555 */
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 do_exmap(eap, FALSE);
5560}
5561
5562/*
5563 * ":mapclear" and friends.
5564 */
5565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005566ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5569}
5570
5571/*
5572 * ":abclear" and friends.
5573 */
5574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005575ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576{
5577 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5578}
5579
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005581ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005584 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 * directory for security reasons.
5586 */
5587 if (secure)
5588 {
5589 secure = 2;
5590 eap->errmsg = e_curdir;
5591 }
5592 else if (eap->cmdidx == CMD_autocmd)
5593 do_autocmd(eap->arg, eap->forceit);
5594 else
5595 do_augroup(eap->arg, eap->forceit);
5596}
5597
5598/*
5599 * ":doautocmd": Apply the automatic commands to the current buffer.
5600 */
5601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005602ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005604 char_u *arg = eap->arg;
5605 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005606 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005607
Bram Moolenaar1610d052016-06-09 22:53:01 +02005608 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5609 /* Only when there is no <nomodeline>. */
5610 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005611 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614/*
5615 * :[N]bunload[!] [N] [bufname] unload buffer
5616 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5617 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5618 */
5619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005620ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
5622 eap->errmsg = do_bufdel(
5623 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5624 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5625 : DOBUF_UNLOAD, eap->arg,
5626 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5627}
5628
5629/*
5630 * :[N]buffer [N] to buffer N
5631 * :[N]sbuffer [N] to buffer N
5632 */
5633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005634ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 if (*eap->arg)
5637 eap->errmsg = e_trailing;
5638 else
5639 {
5640 if (eap->addr_count == 0) /* default is current buffer */
5641 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5642 else
5643 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005644 if (eap->do_ecmd_cmd != NULL)
5645 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647}
5648
5649/*
5650 * :[N]bmodified [N] to next mod. buffer
5651 * :[N]sbmodified [N] to next mod. buffer
5652 */
5653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005657 if (eap->do_ecmd_cmd != NULL)
5658 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659}
5660
5661/*
5662 * :[N]bnext [N] to next buffer
5663 * :[N]sbnext [N] split and to next buffer
5664 */
5665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005666ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005669 if (eap->do_ecmd_cmd != NULL)
5670 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671}
5672
5673/*
5674 * :[N]bNext [N] to previous buffer
5675 * :[N]bprevious [N] to previous buffer
5676 * :[N]sbNext [N] split and to previous buffer
5677 * :[N]sbprevious [N] split and to previous buffer
5678 */
5679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005680ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005683 if (eap->do_ecmd_cmd != NULL)
5684 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685}
5686
5687/*
5688 * :brewind to first buffer
5689 * :bfirst to first buffer
5690 * :sbrewind split and to first buffer
5691 * :sbfirst split and to first buffer
5692 */
5693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005694ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
5696 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005697 if (eap->do_ecmd_cmd != NULL)
5698 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699}
5700
5701/*
5702 * :blast to last buffer
5703 * :sblast split and to last buffer
5704 */
5705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005706ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005709 if (eap->do_ecmd_cmd != NULL)
5710 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
5713 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005714ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715{
5716 return (c == NUL || c == '|' || c == '"' || c == '\n');
5717}
5718
5719#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5720 || defined(PROTO)
5721/*
5722 * Return the next command, after the first '|' or '\n'.
5723 * Return NULL if not found.
5724 */
5725 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005726find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 while (*p != '|' && *p != '\n')
5729 {
5730 if (*p == NUL)
5731 return NULL;
5732 ++p;
5733 }
5734 return (p + 1);
5735}
5736#endif
5737
5738/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005739 * Check if *p is a separator between Ex commands, skipping over white space.
5740 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 */
5742 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005743check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005745 char_u *s = skipwhite(p);
5746
5747 if (*s == '|' || *s == '\n')
5748 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 else
5750 return NULL;
5751}
5752
5753/*
5754 * - if there are more files to edit
5755 * - and this is the last window
5756 * - and forceit not used
5757 * - and not repeated twice on a row
5758 * return FAIL and give error message if 'message' TRUE
5759 * return OK otherwise
5760 */
5761 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005762check_more(
5763 int message, /* when FALSE check only, no messages */
5764 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5767
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005768 if (!forceit && only_one_window()
5769 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 if (message)
5772 {
5773#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5774 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5775 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005776 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005778 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5779 NGETTEXT("%d more file to edit. Quit anyway?",
5780 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5782 return OK;
5783 return FAIL;
5784 }
5785#endif
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005786 EMSGN(NGETTEXT("E173: %ld more file to edit",
5787 "E173: %ld more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 quitmore = 2; /* next try to quit is allowed */
5789 }
5790 return FAIL;
5791 }
5792 return OK;
5793}
5794
5795#ifdef FEAT_CMDL_COMPL
5796/*
5797 * Function given to ExpandGeneric() to obtain the list of command names.
5798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005800get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801{
5802 if (idx >= (int)CMD_SIZE)
5803# ifdef FEAT_USR_CMDS
5804 return get_user_command_name(idx);
5805# else
5806 return NULL;
5807# endif
5808 return cmdnames[idx].cmd_name;
5809}
5810#endif
5811
5812#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005814uc_add_command(
5815 char_u *name,
5816 size_t name_len,
5817 char_u *rep,
5818 long argt,
5819 long def,
5820 int flags,
5821 int compl,
5822 char_u *compl_arg,
5823 int addr_type,
5824 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 ucmd_T *cmd = NULL;
5827 char_u *p;
5828 int i;
5829 int cmp = 1;
5830 char_u *rep_buf = NULL;
5831 garray_T *gap;
5832
Bram Moolenaar9c102382006-05-03 21:26:49 +00005833 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (rep_buf == NULL)
5835 {
5836 /* Can't replace termcodes - try using the string as is */
5837 rep_buf = vim_strsave(rep);
5838
5839 /* Give up if out of memory */
5840 if (rep_buf == NULL)
5841 return FAIL;
5842 }
5843
5844 /* get address of growarray: global or in curbuf */
5845 if (flags & UC_BUFFER)
5846 {
5847 gap = &curbuf->b_ucmds;
5848 if (gap->ga_itemsize == 0)
5849 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5850 }
5851 else
5852 gap = &ucmds;
5853
5854 /* Search for the command in the already defined commands. */
5855 for (i = 0; i < gap->ga_len; ++i)
5856 {
5857 size_t len;
5858
5859 cmd = USER_CMD_GA(gap, i);
5860 len = STRLEN(cmd->uc_name);
5861 cmp = STRNCMP(name, cmd->uc_name, name_len);
5862 if (cmp == 0)
5863 {
5864 if (name_len < len)
5865 cmp = -1;
5866 else if (name_len > len)
5867 cmp = 1;
5868 }
5869
5870 if (cmp == 0)
5871 {
5872 if (!force)
5873 {
5874 EMSG(_("E174: Command already exists: add ! to replace it"));
5875 goto fail;
5876 }
5877
Bram Moolenaard23a8232018-02-10 18:45:26 +01005878 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005879#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005880 VIM_CLEAR(cmd->uc_compl_arg);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 break;
5883 }
5884
5885 /* Stop as soon as we pass the name to add */
5886 if (cmp < 0)
5887 break;
5888 }
5889
5890 /* Extend the array unless we're replacing an existing command */
5891 if (cmp != 0)
5892 {
5893 if (ga_grow(gap, 1) != OK)
5894 goto fail;
5895 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5896 goto fail;
5897
5898 cmd = USER_CMD_GA(gap, i);
5899 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5900
5901 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
5903 cmd->uc_name = p;
5904 }
5905
5906 cmd->uc_rep = rep_buf;
5907 cmd->uc_argt = argt;
5908 cmd->uc_def = def;
5909 cmd->uc_compl = compl;
5910#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005911 cmd->uc_script_ctx = current_sctx;
5912 cmd->uc_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913# ifdef FEAT_CMDL_COMPL
5914 cmd->uc_compl_arg = compl_arg;
5915# endif
5916#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005917 cmd->uc_addr_type = addr_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918
5919 return OK;
5920
5921fail:
5922 vim_free(rep_buf);
5923#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5924 vim_free(compl_arg);
5925#endif
5926 return FAIL;
5927}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005930#if defined(FEAT_USR_CMDS)
5931static struct
5932{
5933 int expand;
5934 char *name;
5935} addr_type_complete[] =
5936{
5937 {ADDR_ARGUMENTS, "arguments"},
5938 {ADDR_LINES, "lines"},
5939 {ADDR_LOADED_BUFFERS, "loaded_buffers"},
5940 {ADDR_TABS, "tabs"},
5941 {ADDR_BUFFERS, "buffers"},
5942 {ADDR_WINDOWS, "windows"},
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005943 {ADDR_QUICKFIX, "quickfix"},
Bram Moolenaar51a74542018-12-02 18:21:49 +01005944 {ADDR_OTHER, "other"},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005945 {-1, NULL}
5946};
5947#endif
5948
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005949#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950/*
5951 * List of names for completion for ":command" with the EXPAND_ flag.
5952 * Must be alphabetical for completion.
5953 */
5954static struct
5955{
5956 int expand;
5957 char *name;
5958} command_complete[] =
5959{
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005960 {EXPAND_ARGLIST, "arglist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005962 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005964 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005966 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005967#if defined(FEAT_CSCOPE)
5968 {EXPAND_CSCOPE, "cscope"},
5969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5971 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005972 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#endif
5974 {EXPAND_DIRECTORIES, "dir"},
5975 {EXPAND_ENV_VARS, "environment"},
5976 {EXPAND_EVENTS, "event"},
5977 {EXPAND_EXPRESSION, "expression"},
5978 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005979 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005980 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {EXPAND_FUNCTIONS, "function"},
5982 {EXPAND_HELP, "help"},
5983 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005984#if defined(FEAT_CMDHIST)
5985 {EXPAND_HISTORY, "history"},
5986#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005987#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005988 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005989 {EXPAND_LOCALES, "locale"},
5990#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005991 {EXPAND_MAPCLEAR, "mapclear"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {EXPAND_MAPPINGS, "mapping"},
5993 {EXPAND_MENUS, "menu"},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005994 {EXPAND_MESSAGES, "messages"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005995 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005996#if defined(FEAT_PROFILE)
5997 {EXPAND_SYNTIME, "syntime"},
5998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 {EXPAND_SETTINGS, "option"},
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01006000 {EXPAND_PACKADD, "packadd"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006001 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00006002#if defined(FEAT_SIGNS)
6003 {EXPAND_SIGN, "sign"},
6004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {EXPAND_TAGS, "tag"},
6006 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02006007 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {EXPAND_USER_VARS, "var"},
6009 {0, NULL}
6010};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006013#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006015uc_list(char_u *name, size_t name_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 int i, j;
6018 int found = FALSE;
6019 ucmd_T *cmd;
6020 int len;
6021 long a;
6022 garray_T *gap;
6023
6024 gap = &curbuf->b_ucmds;
6025 for (;;)
6026 {
6027 for (i = 0; i < gap->ga_len; ++i)
6028 {
6029 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006030 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031
Bram Moolenaard29459b2016-08-26 22:29:11 +02006032 /* Skip commands which don't match the requested prefix and
6033 * commands filtered out. */
6034 if (STRNCMP(name, cmd->uc_name, name_len) != 0
6035 || message_filtered(cmd->uc_name))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 continue;
6037
6038 /* Put out the title first time */
6039 if (!found)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006040 MSG_PUTS_TITLE(_("\n Name Args Address Complete Definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 found = TRUE;
6042 msg_putchar('\n');
6043 if (got_int)
6044 break;
6045
6046 /* Special cases */
6047 msg_putchar(a & BANG ? '!' : ' ');
6048 msg_putchar(a & REGSTR ? '"' : ' ');
6049 msg_putchar(gap != &ucmds ? 'b' : ' ');
6050 msg_putchar(' ');
6051
Bram Moolenaar8820b482017-03-16 17:23:31 +01006052 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 len = (int)STRLEN(cmd->uc_name) + 4;
6054
6055 do {
6056 msg_putchar(' ');
6057 ++len;
6058 } while (len < 16);
6059
6060 len = 0;
6061
6062 /* Arguments */
6063 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
6064 {
6065 case 0: IObuff[len++] = '0'; break;
6066 case (EXTRA): IObuff[len++] = '*'; break;
6067 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
6068 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
6069 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
6070 }
6071
6072 do {
6073 IObuff[len++] = ' ';
6074 } while (len < 5);
6075
6076 /* Range */
6077 if (a & (RANGE|COUNT))
6078 {
6079 if (a & COUNT)
6080 {
6081 /* -count=N */
6082 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
6083 len += (int)STRLEN(IObuff + len);
6084 }
6085 else if (a & DFLALL)
6086 IObuff[len++] = '%';
6087 else if (cmd->uc_def >= 0)
6088 {
6089 /* -range=N */
6090 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
6091 len += (int)STRLEN(IObuff + len);
6092 }
6093 else
6094 IObuff[len++] = '.';
6095 }
6096
6097 do {
6098 IObuff[len++] = ' ';
6099 } while (len < 11);
6100
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006101 /* Address Type */
6102 for (j = 0; addr_type_complete[j].expand != -1; ++j)
6103 if (addr_type_complete[j].expand != ADDR_LINES
6104 && addr_type_complete[j].expand == cmd->uc_addr_type)
6105 {
6106 STRCPY(IObuff + len, addr_type_complete[j].name);
6107 len += (int)STRLEN(IObuff + len);
6108 break;
6109 }
6110
6111 do {
6112 IObuff[len++] = ' ';
6113 } while (len < 21);
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 /* Completion */
6116 for (j = 0; command_complete[j].expand != 0; ++j)
6117 if (command_complete[j].expand == cmd->uc_compl)
6118 {
6119 STRCPY(IObuff + len, command_complete[j].name);
6120 len += (int)STRLEN(IObuff + len);
6121 break;
6122 }
6123
6124 do {
6125 IObuff[len++] = ' ';
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006126 } while (len < 35);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 IObuff[len] = '\0';
6129 msg_outtrans(IObuff);
6130
6131 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006132#ifdef FEAT_EVAL
6133 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006134 last_set_msg(cmd->uc_script_ctx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 out_flush();
6137 ui_breakcheck();
6138 if (got_int)
6139 break;
6140 }
6141 if (gap == &ucmds || i < gap->ga_len)
6142 break;
6143 gap = &ucmds;
6144 }
6145
6146 if (!found)
6147 MSG(_("No user-defined commands found"));
6148}
6149
6150 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006151uc_fun_cmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
6153 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
6154 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
6155 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
6156 0xb9, 0x7f, 0};
6157 int i;
6158
6159 for (i = 0; fcmd[i]; ++i)
6160 IObuff[i] = fcmd[i] - 0x40;
6161 IObuff[i] = 0;
6162 return IObuff;
6163}
6164
6165 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006166uc_scan_attr(
6167 char_u *attr,
6168 size_t len,
6169 long *argt,
6170 long *def,
6171 int *flags,
6172 int *compl,
6173 char_u **compl_arg,
6174 int *addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
6176 char_u *p;
6177
6178 if (len == 0)
6179 {
6180 EMSG(_("E175: No attribute specified"));
6181 return FAIL;
6182 }
6183
6184 /* First, try the simple attributes (no arguments) */
6185 if (STRNICMP(attr, "bang", len) == 0)
6186 *argt |= BANG;
6187 else if (STRNICMP(attr, "buffer", len) == 0)
6188 *flags |= UC_BUFFER;
6189 else if (STRNICMP(attr, "register", len) == 0)
6190 *argt |= REGSTR;
6191 else if (STRNICMP(attr, "bar", len) == 0)
6192 *argt |= TRLBAR;
6193 else
6194 {
6195 int i;
6196 char_u *val = NULL;
6197 size_t vallen = 0;
6198 size_t attrlen = len;
6199
6200 /* Look for the attribute name - which is the part before any '=' */
6201 for (i = 0; i < (int)len; ++i)
6202 {
6203 if (attr[i] == '=')
6204 {
6205 val = &attr[i + 1];
6206 vallen = len - i - 1;
6207 attrlen = i;
6208 break;
6209 }
6210 }
6211
6212 if (STRNICMP(attr, "nargs", attrlen) == 0)
6213 {
6214 if (vallen == 1)
6215 {
6216 if (*val == '0')
6217 /* Do nothing - this is the default */;
6218 else if (*val == '1')
6219 *argt |= (EXTRA | NOSPC | NEEDARG);
6220 else if (*val == '*')
6221 *argt |= EXTRA;
6222 else if (*val == '?')
6223 *argt |= (EXTRA | NOSPC);
6224 else if (*val == '+')
6225 *argt |= (EXTRA | NEEDARG);
6226 else
6227 goto wrong_nargs;
6228 }
6229 else
6230 {
6231wrong_nargs:
6232 EMSG(_("E176: Invalid number of arguments"));
6233 return FAIL;
6234 }
6235 }
6236 else if (STRNICMP(attr, "range", attrlen) == 0)
6237 {
6238 *argt |= RANGE;
6239 if (vallen == 1 && *val == '%')
6240 *argt |= DFLALL;
6241 else if (val != NULL)
6242 {
6243 p = val;
6244 if (*def >= 0)
6245 {
6246two_count:
6247 EMSG(_("E177: Count cannot be specified twice"));
6248 return FAIL;
6249 }
6250
6251 *def = getdigits(&p);
6252 *argt |= (ZEROR | NOTADR);
6253
6254 if (p != val + vallen || vallen == 0)
6255 {
6256invalid_count:
6257 EMSG(_("E178: Invalid default value for count"));
6258 return FAIL;
6259 }
6260 }
6261 }
6262 else if (STRNICMP(attr, "count", attrlen) == 0)
6263 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00006264 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
6266 if (val != NULL)
6267 {
6268 p = val;
6269 if (*def >= 0)
6270 goto two_count;
6271
6272 *def = getdigits(&p);
6273
6274 if (p != val + vallen)
6275 goto invalid_count;
6276 }
6277
6278 if (*def < 0)
6279 *def = 0;
6280 }
6281 else if (STRNICMP(attr, "complete", attrlen) == 0)
6282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 if (val == NULL)
6284 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006285 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 return FAIL;
6287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006289 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6290 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 }
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006293 else if (STRNICMP(attr, "addr", attrlen) == 0)
6294 {
6295 *argt |= RANGE;
6296 if (val == NULL)
6297 {
6298 EMSG(_("E179: argument required for -addr"));
6299 return FAIL;
6300 }
6301 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg)
6302 == FAIL)
6303 return FAIL;
6304 if (addr_type_arg != ADDR_LINES)
6305 *argt |= (ZEROR | NOTADR) ;
6306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 else
6308 {
6309 char_u ch = attr[len];
6310 attr[len] = '\0';
6311 EMSG2(_("E181: Invalid attribute: %s"), attr);
6312 attr[len] = ch;
6313 return FAIL;
6314 }
6315 }
6316
6317 return OK;
6318}
6319
Bram Moolenaara850a712009-01-28 14:42:59 +00006320/*
6321 * ":command ..."
6322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324ex_command(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 char_u *name;
6327 char_u *end;
6328 char_u *p;
6329 long argt = 0;
6330 long def = -1;
6331 int flags = 0;
6332 int compl = EXPAND_NOTHING;
6333 char_u *compl_arg = NULL;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01006334 int addr_type_arg = ADDR_LINES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006336 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
6338 p = eap->arg;
6339
6340 /* Check for attributes */
6341 while (*p == '-')
6342 {
6343 ++p;
6344 end = skiptowhite(p);
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006345 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
6346 &compl_arg, &addr_type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 == FAIL)
6348 return;
6349 p = skipwhite(end);
6350 }
6351
6352 /* Get the name (if any) and skip to the following argument */
6353 name = p;
6354 if (ASCII_ISALPHA(*p))
6355 while (ASCII_ISALNUM(*p))
6356 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006357 if (!ends_excmd(*p) && !VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 {
6359 EMSG(_("E182: Invalid command name"));
6360 return;
6361 }
6362 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006363 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365 /* If there is nothing after the name, and no attributes were specified,
6366 * we are listing commands
6367 */
6368 p = skipwhite(end);
6369 if (!has_attr && ends_excmd(*p))
6370 {
6371 uc_list(name, end - name);
6372 }
6373 else if (!ASCII_ISUPPER(*name))
6374 {
6375 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6376 return;
6377 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006378 else if ((name_len == 1 && *name == 'X')
6379 || (name_len <= 4
6380 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6381 {
6382 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6383 return;
6384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 else
6386 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006387 addr_type_arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388}
6389
6390/*
6391 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 * Clear all user commands, global and for current buffer.
6393 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006394 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006395ex_comclear(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397 uc_clear(&ucmds);
6398 uc_clear(&curbuf->b_ucmds);
6399}
6400
6401/*
6402 * Clear all user commands for "gap".
6403 */
6404 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006405uc_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 int i;
6408 ucmd_T *cmd;
6409
6410 for (i = 0; i < gap->ga_len; ++i)
6411 {
6412 cmd = USER_CMD_GA(gap, i);
6413 vim_free(cmd->uc_name);
6414 vim_free(cmd->uc_rep);
6415# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6416 vim_free(cmd->uc_compl_arg);
6417# endif
6418 }
6419 ga_clear(gap);
6420}
6421
6422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006423ex_delcommand(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
6425 int i = 0;
6426 ucmd_T *cmd = NULL;
6427 int cmp = -1;
6428 garray_T *gap;
6429
6430 gap = &curbuf->b_ucmds;
6431 for (;;)
6432 {
6433 for (i = 0; i < gap->ga_len; ++i)
6434 {
6435 cmd = USER_CMD_GA(gap, i);
6436 cmp = STRCMP(eap->arg, cmd->uc_name);
6437 if (cmp <= 0)
6438 break;
6439 }
6440 if (gap == &ucmds || cmp == 0)
6441 break;
6442 gap = &ucmds;
6443 }
6444
6445 if (cmp != 0)
6446 {
6447 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6448 return;
6449 }
6450
6451 vim_free(cmd->uc_name);
6452 vim_free(cmd->uc_rep);
6453# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6454 vim_free(cmd->uc_compl_arg);
6455# endif
6456
6457 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459 if (i < gap->ga_len)
6460 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6461}
6462
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006463/*
6464 * split and quote args for <f-args>
6465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006467uc_split_args(char_u *arg, size_t *lenp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 char_u *buf;
6470 char_u *p;
6471 char_u *q;
6472 int len;
6473
6474 /* Precalculate length */
6475 p = arg;
6476 len = 2; /* Initial and final quotes */
6477
6478 while (*p)
6479 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006480 if (p[0] == '\\' && p[1] == '\\')
6481 {
6482 len += 2;
6483 p += 2;
6484 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006485 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 {
6487 len += 1;
6488 p += 2;
6489 }
6490 else if (*p == '\\' || *p == '"')
6491 {
6492 len += 2;
6493 p += 1;
6494 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006495 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 {
6497 p = skipwhite(p);
6498 if (*p == NUL)
6499 break;
6500 len += 3; /* "," */
6501 }
6502 else
6503 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006504#ifdef FEAT_MBYTE
6505 int charlen = (*mb_ptr2len)(p);
6506 len += charlen;
6507 p += charlen;
6508#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 ++len;
6510 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 }
6513 }
6514
6515 buf = alloc(len + 1);
6516 if (buf == NULL)
6517 {
6518 *lenp = 0;
6519 return buf;
6520 }
6521
6522 p = arg;
6523 q = buf;
6524 *q++ = '"';
6525 while (*p)
6526 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006527 if (p[0] == '\\' && p[1] == '\\')
6528 {
6529 *q++ = '\\';
6530 *q++ = '\\';
6531 p += 2;
6532 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006533 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
6535 *q++ = p[1];
6536 p += 2;
6537 }
6538 else if (*p == '\\' || *p == '"')
6539 {
6540 *q++ = '\\';
6541 *q++ = *p++;
6542 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01006543 else if (VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
6545 p = skipwhite(p);
6546 if (*p == NUL)
6547 break;
6548 *q++ = '"';
6549 *q++ = ',';
6550 *q++ = '"';
6551 }
6552 else
6553 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006554 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556 }
6557 *q++ = '"';
6558 *q = 0;
6559
6560 *lenp = len;
6561 return buf;
6562}
6563
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006564 static size_t
6565add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
6566{
6567 size_t result;
6568
6569 result = STRLEN(mod_str);
6570 if (*multi_mods)
6571 result += 1;
6572 if (buf != NULL)
6573 {
6574 if (*multi_mods)
6575 STRCAT(buf, " ");
6576 STRCAT(buf, mod_str);
6577 }
6578
6579 *multi_mods = 1;
6580
6581 return result;
6582}
6583
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584/*
6585 * Check for a <> code in a user command.
6586 * "code" points to the '<'. "len" the length of the <> (inclusive).
6587 * "buf" is where the result is to be added.
6588 * "split_buf" points to a buffer used for splitting, caller should free it.
6589 * "split_len" is the length of what "split_buf" contains.
6590 * Returns the length of the replacement, which has been added to "buf".
6591 * Returns -1 if there was no match, and only the "<" has been copied.
6592 */
6593 static size_t
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006594uc_check_code(
6595 char_u *code,
6596 size_t len,
6597 char_u *buf,
6598 ucmd_T *cmd, /* the user command we're expanding */
6599 exarg_T *eap, /* ex arguments */
6600 char_u **split_buf,
6601 size_t *split_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
6603 size_t result = 0;
6604 char_u *p = code + 1;
6605 size_t l = len - 2;
6606 int quote = 0;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006607 enum {
6608 ct_ARGS,
6609 ct_BANG,
6610 ct_COUNT,
6611 ct_LINE1,
6612 ct_LINE2,
6613 ct_RANGE,
6614 ct_MODS,
6615 ct_REGISTER,
6616 ct_LT,
6617 ct_NONE
6618 } type = ct_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
6620 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6621 {
6622 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6623 p += 2;
6624 l -= 2;
6625 }
6626
Bram Moolenaar371d5402006-03-20 21:47:49 +00006627 ++l;
6628 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006630 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006632 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006634 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006636 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006638 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 type = ct_LINE2;
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006640 else if (STRNICMP(p, "range>", l) == 0)
6641 type = ct_RANGE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006642 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006644 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 type = ct_REGISTER;
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006646 else if (STRNICMP(p, "mods>", l) == 0)
6647 type = ct_MODS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648
6649 switch (type)
6650 {
6651 case ct_ARGS:
6652 /* Simple case first */
6653 if (*eap->arg == NUL)
6654 {
6655 if (quote == 1)
6656 {
6657 result = 2;
6658 if (buf != NULL)
6659 STRCPY(buf, "''");
6660 }
6661 else
6662 result = 0;
6663 break;
6664 }
6665
6666 /* When specified there is a single argument don't split it.
6667 * Works for ":Cmd %" when % is "a b c". */
6668 if ((eap->argt & NOSPC) && quote == 2)
6669 quote = 1;
6670
6671 switch (quote)
6672 {
6673 case 0: /* No quoting, no splitting */
6674 result = STRLEN(eap->arg);
6675 if (buf != NULL)
6676 STRCPY(buf, eap->arg);
6677 break;
6678 case 1: /* Quote, but don't split */
6679 result = STRLEN(eap->arg) + 2;
6680 for (p = eap->arg; *p; ++p)
6681 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006682#ifdef FEAT_MBYTE
6683 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6684 /* DBCS can contain \ in a trail byte, skip the
6685 * double-byte character. */
6686 ++p;
6687 else
6688#endif
6689 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 ++result;
6691 }
6692
6693 if (buf != NULL)
6694 {
6695 *buf++ = '"';
6696 for (p = eap->arg; *p; ++p)
6697 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006698#ifdef FEAT_MBYTE
6699 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6700 /* DBCS can contain \ in a trail byte, copy the
6701 * double-byte character to avoid escaping. */
6702 *buf++ = *p++;
6703 else
6704#endif
6705 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 *buf++ = '\\';
6707 *buf++ = *p;
6708 }
6709 *buf = '"';
6710 }
6711
6712 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006713 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 /* This is hard, so only do it once, and cache the result */
6715 if (*split_buf == NULL)
6716 *split_buf = uc_split_args(eap->arg, split_len);
6717
6718 result = *split_len;
6719 if (buf != NULL && result != 0)
6720 STRCPY(buf, *split_buf);
6721
6722 break;
6723 }
6724 break;
6725
6726 case ct_BANG:
6727 result = eap->forceit ? 1 : 0;
6728 if (quote)
6729 result += 2;
6730 if (buf != NULL)
6731 {
6732 if (quote)
6733 *buf++ = '"';
6734 if (eap->forceit)
6735 *buf++ = '!';
6736 if (quote)
6737 *buf = '"';
6738 }
6739 break;
6740
6741 case ct_LINE1:
6742 case ct_LINE2:
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006743 case ct_RANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 case ct_COUNT:
6745 {
6746 char num_buf[20];
6747 long num = (type == ct_LINE1) ? eap->line1 :
6748 (type == ct_LINE2) ? eap->line2 :
Bram Moolenaarc168bd42017-09-10 17:34:35 +02006749 (type == ct_RANGE) ? eap->addr_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6751 size_t num_len;
6752
6753 sprintf(num_buf, "%ld", num);
6754 num_len = STRLEN(num_buf);
6755 result = num_len;
6756
6757 if (quote)
6758 result += 2;
6759
6760 if (buf != NULL)
6761 {
6762 if (quote)
6763 *buf++ = '"';
6764 STRCPY(buf, num_buf);
6765 buf += num_len;
6766 if (quote)
6767 *buf = '"';
6768 }
6769
6770 break;
6771 }
6772
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006773 case ct_MODS:
6774 {
6775 int multi_mods = 0;
6776 typedef struct {
6777 int *varp;
6778 char *name;
6779 } mod_entry_T;
6780 static mod_entry_T mod_entries[] = {
6781#ifdef FEAT_BROWSE_CMD
6782 {&cmdmod.browse, "browse"},
6783#endif
6784#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6785 {&cmdmod.confirm, "confirm"},
6786#endif
6787 {&cmdmod.hide, "hide"},
6788 {&cmdmod.keepalt, "keepalt"},
6789 {&cmdmod.keepjumps, "keepjumps"},
6790 {&cmdmod.keepmarks, "keepmarks"},
6791 {&cmdmod.keeppatterns, "keeppatterns"},
6792 {&cmdmod.lockmarks, "lockmarks"},
6793 {&cmdmod.noswapfile, "noswapfile"},
6794 {NULL, NULL}
6795 };
6796 int i;
6797
6798 result = quote ? 2 : 0;
6799 if (buf != NULL)
6800 {
6801 if (quote)
6802 *buf++ = '"';
6803 *buf = '\0';
6804 }
6805
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006806 /* :aboveleft and :leftabove */
6807 if (cmdmod.split & WSP_ABOVE)
6808 result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
6809 /* :belowright and :rightbelow */
6810 if (cmdmod.split & WSP_BELOW)
6811 result += add_cmd_modifier(buf, "belowright", &multi_mods);
6812 /* :botright */
6813 if (cmdmod.split & WSP_BOT)
6814 result += add_cmd_modifier(buf, "botright", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006815
6816 /* the modifiers that are simple flags */
6817 for (i = 0; mod_entries[i].varp != NULL; ++i)
6818 if (*mod_entries[i].varp)
6819 result += add_cmd_modifier(buf, mod_entries[i].name,
6820 &multi_mods);
6821
6822 /* TODO: How to support :noautocmd? */
6823#ifdef HAVE_SANDBOX
6824 /* TODO: How to support :sandbox?*/
6825#endif
6826 /* :silent */
6827 if (msg_silent > 0)
6828 result += add_cmd_modifier(buf,
6829 emsg_silent > 0 ? "silent!" : "silent", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006830 /* :tab */
6831 if (cmdmod.tab > 0)
6832 result += add_cmd_modifier(buf, "tab", &multi_mods);
6833 /* :topleft */
6834 if (cmdmod.split & WSP_TOP)
6835 result += add_cmd_modifier(buf, "topleft", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006836 /* TODO: How to support :unsilent?*/
6837 /* :verbose */
6838 if (p_verbose > 0)
6839 result += add_cmd_modifier(buf, "verbose", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006840 /* :vertical */
6841 if (cmdmod.split & WSP_VERT)
6842 result += add_cmd_modifier(buf, "vertical", &multi_mods);
Bram Moolenaar63a60de2016-06-04 22:08:55 +02006843 if (quote && buf != NULL)
6844 {
6845 buf += result - 2;
6846 *buf = '"';
6847 }
6848 break;
6849 }
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 case ct_REGISTER:
6852 result = eap->regname ? 1 : 0;
6853 if (quote)
6854 result += 2;
6855 if (buf != NULL)
6856 {
6857 if (quote)
6858 *buf++ = '\'';
6859 if (eap->regname)
6860 *buf++ = eap->regname;
6861 if (quote)
6862 *buf = '\'';
6863 }
6864 break;
6865
6866 case ct_LT:
6867 result = 1;
6868 if (buf != NULL)
6869 *buf = '<';
6870 break;
6871
6872 default:
6873 /* Not recognized: just copy the '<' and return -1. */
6874 result = (size_t)-1;
6875 if (buf != NULL)
6876 *buf = '<';
6877 break;
6878 }
6879
6880 return result;
6881}
6882
6883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006884do_ucmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
6886 char_u *buf;
6887 char_u *p;
6888 char_u *q;
6889
6890 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006891 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006892 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 size_t len, totlen;
6894
6895 size_t split_len = 0;
6896 char_u *split_buf = NULL;
6897 ucmd_T *cmd;
6898#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006899 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900#endif
6901
6902 if (eap->cmdidx == CMD_USER)
6903 cmd = USER_CMD(eap->useridx);
6904 else
6905 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6906
6907 /*
6908 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006909 * First round: "buf" is NULL, compute length, allocate "buf".
6910 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 */
6912 buf = NULL;
6913 for (;;)
6914 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006915 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006916 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006918
6919 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006921 start = vim_strchr(p, '<');
6922 if (start != NULL)
6923 end = vim_strchr(start + 1, '>');
6924 if (buf != NULL)
6925 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006926 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6927 ;
6928 if (*ksp == K_SPECIAL
6929 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006930 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6931# ifdef FEAT_GUI
6932 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6933# endif
6934 ))
6935 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006936 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006937 * KS_SPECIAL KE_FILLER, like for mappings, but
6938 * do_cmdline() doesn't handle that, so convert it back.
6939 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6940 len = ksp - p;
6941 if (len > 0)
6942 {
6943 mch_memmove(q, p, len);
6944 q += len;
6945 }
6946 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6947 p = ksp + 3;
6948 continue;
6949 }
6950 }
6951
6952 /* break if there no <item> is found */
6953 if (start == NULL || end == NULL)
6954 break;
6955
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 /* Include the '>' */
6957 ++end;
6958
6959 /* Take everything up to the '<' */
6960 len = start - p;
6961 if (buf == NULL)
6962 totlen += len;
6963 else
6964 {
6965 mch_memmove(q, p, len);
6966 q += len;
6967 }
6968
6969 len = uc_check_code(start, end - start, q, cmd, eap,
6970 &split_buf, &split_len);
6971 if (len == (size_t)-1)
6972 {
6973 /* no match, continue after '<' */
6974 p = start + 1;
6975 len = 1;
6976 }
6977 else
6978 p = end;
6979 if (buf == NULL)
6980 totlen += len;
6981 else
6982 q += len;
6983 }
6984 if (buf != NULL) /* second time here, finished */
6985 {
6986 STRCPY(q, p);
6987 break;
6988 }
6989
6990 totlen += STRLEN(p); /* Add on the trailing characters */
6991 buf = alloc((unsigned)(totlen + 1));
6992 if (buf == NULL)
6993 {
6994 vim_free(split_buf);
6995 return;
6996 }
6997 }
6998
6999#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007000 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001#endif
7002 (void)do_cmdline(buf, eap->getline, eap->cookie,
7003 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
7004#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007005 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006#endif
7007 vim_free(buf);
7008 vim_free(split_buf);
7009}
7010
7011# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7012 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013get_user_command_name(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 return get_user_commands(NULL, idx - (int)CMD_SIZE);
7016}
7017
7018/*
7019 * Function given to ExpandGeneric() to obtain the list of user command names.
7020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007022get_user_commands(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 if (idx < curbuf->b_ucmds.ga_len)
7025 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
7026 idx -= curbuf->b_ucmds.ga_len;
7027 if (idx < ucmds.ga_len)
7028 return USER_CMD(idx)->uc_name;
7029 return NULL;
7030}
7031
7032/*
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007033 * Function given to ExpandGeneric() to obtain the list of user address type names.
7034 */
7035 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007036get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007037{
7038 return (char_u *)addr_type_complete[idx].name;
7039}
7040
7041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 * Function given to ExpandGeneric() to obtain the list of user command
7043 * attributes.
7044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007046get_user_cmd_flags(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 static char *user_cmd_flags[] =
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007049 {"addr", "bang", "bar", "buffer", "complete",
7050 "count", "nargs", "range", "register"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007052 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 return NULL;
7054 return (char_u *)user_cmd_flags[idx];
7055}
7056
7057/*
7058 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
7059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007061get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
7063 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
7064
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00007065 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 return NULL;
7067 return (char_u *)user_cmd_nargs[idx];
7068}
7069
7070/*
7071 * Function given to ExpandGeneric() to obtain the list of values for -complete.
7072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007074get_user_cmd_complete(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 return (char_u *)command_complete[idx].name;
7077}
7078# endif /* FEAT_CMDL_COMPL */
7079
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007080/*
7081 * Parse address type argument
7082 */
7083 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007084parse_addr_type_arg(
7085 char_u *value,
7086 int vallen,
7087 long *argt,
7088 int *addr_type_arg)
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007089{
7090 int i, a, b;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007091
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007092 for (i = 0; addr_type_complete[i].expand != -1; ++i)
7093 {
7094 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
7095 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
7096 if (a && b)
7097 {
7098 *addr_type_arg = addr_type_complete[i].expand;
7099 break;
7100 }
7101 }
7102
7103 if (addr_type_complete[i].expand == -1)
7104 {
7105 char_u *err = value;
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007106
Bram Moolenaar1c465442017-03-12 20:10:05 +01007107 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
Bram Moolenaar05fe0172016-01-10 13:54:48 +01007108 ;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01007109 err[i] = NUL;
7110 EMSG2(_("E180: Invalid address type value: %s"), err);
7111 return FAIL;
7112 }
7113
7114 if (*addr_type_arg != ADDR_LINES)
7115 *argt |= NOTADR;
7116
7117 return OK;
7118}
7119
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120#endif /* FEAT_USR_CMDS */
7121
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007122#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
7123/*
7124 * Parse a completion argument "value[vallen]".
7125 * The detected completion goes in "*complp", argument type in "*argt".
7126 * When there is an argument, for function and user defined completion, it's
7127 * copied to allocated memory and stored in "*compl_arg".
7128 * Returns FAIL if something is wrong.
7129 */
7130 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007131parse_compl_arg(
7132 char_u *value,
7133 int vallen,
7134 int *complp,
7135 long *argt,
7136 char_u **compl_arg UNUSED)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007137{
7138 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007139# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007140 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007141# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007142 int i;
7143 int valend = vallen;
7144
7145 /* Look for any argument part - which is the part after any ',' */
7146 for (i = 0; i < vallen; ++i)
7147 {
7148 if (value[i] == ',')
7149 {
7150 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007151# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007152 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01007153# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007154 valend = i;
7155 break;
7156 }
7157 }
7158
7159 for (i = 0; command_complete[i].expand != 0; ++i)
7160 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007161 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007162 && STRNCMP(value, command_complete[i].name, valend) == 0)
7163 {
7164 *complp = command_complete[i].expand;
7165 if (command_complete[i].expand == EXPAND_BUFFERS)
7166 *argt |= BUFNAME;
7167 else if (command_complete[i].expand == EXPAND_DIRECTORIES
7168 || command_complete[i].expand == EXPAND_FILES)
7169 *argt |= XFILE;
7170 break;
7171 }
7172 }
7173
7174 if (command_complete[i].expand == 0)
7175 {
7176 EMSG2(_("E180: Invalid complete value: %s"), value);
7177 return FAIL;
7178 }
7179
7180# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7181 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
7182 && arg != NULL)
7183# else
7184 if (arg != NULL)
7185# endif
7186 {
7187 EMSG(_("E468: Completion argument only allowed for custom completion"));
7188 return FAIL;
7189 }
7190
7191# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
7192 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
7193 && arg == NULL)
7194 {
7195 EMSG(_("E467: Custom completion requires a function argument"));
7196 return FAIL;
7197 }
7198
7199 if (arg != NULL)
7200 *compl_arg = vim_strnsave(arg, (int)arglen);
7201# endif
7202 return OK;
7203}
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02007204
7205 int
7206cmdcomplete_str_to_type(char_u *complete_str)
7207{
7208 int i;
7209
7210 for (i = 0; command_complete[i].expand != 0; ++i)
7211 if (STRCMP(complete_str, command_complete[i].name) == 0)
7212 return command_complete[i].expand;
7213
7214 return EXPAND_NOTHING;
7215}
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007216#endif
7217
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007219ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
Bram Moolenaare6850792010-05-14 15:28:44 +02007221 if (*eap->arg == NUL)
7222 {
7223#ifdef FEAT_EVAL
7224 char_u *expr = vim_strsave((char_u *)"g:colors_name");
7225 char_u *p = NULL;
7226
7227 if (expr != NULL)
7228 {
7229 ++emsg_off;
7230 p = eval_to_string(expr, NULL, FALSE);
7231 --emsg_off;
7232 vim_free(expr);
7233 }
7234 if (p != NULL)
7235 {
7236 MSG(p);
7237 vim_free(p);
7238 }
7239 else
7240 MSG("default");
7241#else
7242 MSG(_("unknown"));
7243#endif
7244 }
7245 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02007246 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247}
7248
7249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007250ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 if (*eap->arg == NUL && eap->cmd[2] == '!')
7253 MSG(_("Greetings, Vim user!"));
7254 do_highlight(eap->arg, eap->forceit, FALSE);
7255}
7256
7257
7258/*
7259 * Call this function if we thought we were going to exit, but we won't
7260 * (because of an error). May need to restore the terminal mode.
7261 */
7262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007263not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
7265 exiting = FALSE;
7266 settmode(TMODE_RAW);
7267}
7268
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007269 static int
7270before_quit_autocmds(win_T *wp, int quit_all, int forceit)
7271{
7272 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7273
7274 /* Bail out when autocommands closed the window.
7275 * Refuse to quit when the buffer in the last window is being closed (can
7276 * only happen in autocommands). */
7277 if (!win_valid(wp)
7278 || curbuf_locked()
7279 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
7280 return TRUE;
7281
7282 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
7283 {
7284 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
7285 /* Refuse to quit when locked or when the buffer in the last window is
7286 * being closed (can only happen in autocommands). */
7287 if (curbuf_locked()
7288 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
7289 return TRUE;
7290 }
7291
7292 return FALSE;
7293}
7294
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01007296 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007297 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 */
7299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007300ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007302 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007303
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304#ifdef FEAT_CMDWIN
7305 if (cmdwin_type != 0)
7306 {
7307 cmdwin_result = Ctrl_C;
7308 return;
7309 }
7310#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007311 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007312 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007313 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007314 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007315 return;
7316 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007317 if (eap->addr_count > 0)
7318 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01007319 int wnr = eap->line2;
7320
7321 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
7322 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007323 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007324 }
7325 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007326 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007327
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02007328 /* Refuse to quit when locked. */
7329 if (curbuf_locked())
7330 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007331
7332 /* Trigger QuitPre and maybe ExitPre */
7333 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007334 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
7336#ifdef FEAT_NETBEANS_INTG
7337 netbeansForcedQuit = eap->forceit;
7338#endif
7339
7340 /*
7341 * If there are more files or windows we won't exit.
7342 */
7343 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7344 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02007345 if ((!buf_hide(wp->w_buffer)
7346 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007347 | (eap->forceit ? CCGD_FORCEIT : 0)
7348 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007350 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 {
7352 not_exiting();
7353 }
7354 else
7355 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02007356 /* quit last window
7357 * Note: only_one_window() returns true, even so a help window is
7358 * still open. In that case only quit, if no address has been
7359 * specified. Example:
7360 * :h|wincmd w|1q - don't quit
7361 * :h|wincmd w|q - quit
7362 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01007363 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007365 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02007366#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007370 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 }
7372}
7373
7374/*
7375 * ":cquit".
7376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007378ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 getout(1); /* this does not always pass on the exit code to the Manx
7381 compiler. why? */
7382}
7383
7384/*
7385 * ":qall": try to quit all windows
7386 */
7387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007388ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389{
7390# ifdef FEAT_CMDWIN
7391 if (cmdwin_type != 0)
7392 {
7393 if (eap->forceit)
7394 cmdwin_result = K_XF1; /* ex_window() takes care of this */
7395 else
7396 cmdwin_result = K_XF2;
7397 return;
7398 }
7399# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007400
7401 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007402 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007403 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007404 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007405 return;
7406 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007407
7408 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007409 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01007412 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 getout(0);
7414 not_exiting();
7415}
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417/*
7418 * ":close": close current window, unless it is the last one
7419 */
7420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007421ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007423 win_T *win;
7424 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007425#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007427 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007429#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007430 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007431 {
7432 if (eap->addr_count == 0)
7433 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02007434 else
7435 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007436 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007437 {
7438 winnr++;
7439 if (winnr == eap->line2)
7440 break;
7441 }
7442 if (win == NULL)
7443 win = lastwin;
7444 ex_win_close(eap->forceit, win, NULL);
7445 }
7446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447}
7448
Bram Moolenaar4033c552017-09-16 20:54:51 +02007449#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007450/*
7451 * ":pclose": Close any preview window.
7452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007454ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007455{
7456 win_T *win;
7457
Bram Moolenaar29323592016-07-24 22:04:11 +02007458 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007459 if (win->w_p_pvw)
7460 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007461 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007462 break;
7463 }
7464}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007465#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007466
Bram Moolenaarf740b292006-02-16 22:11:02 +00007467/*
7468 * Close window "win" and take care of handling closing the last window for a
7469 * modified buffer.
7470 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007472ex_win_close(
7473 int forceit,
7474 win_T *win,
7475 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476{
7477 int need_hide;
7478 buf_T *buf = win->w_buffer;
7479
7480 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007481 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007483#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 if ((p_confirm || cmdmod.confirm) && p_write)
7485 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007486 bufref_T bufref;
7487
7488 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007490 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 return;
7492 need_hide = FALSE;
7493 }
7494 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02007495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02007497 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 return;
7499 }
7500 }
7501
Bram Moolenaar4033c552017-09-16 20:54:51 +02007502#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007504#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007505
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007507 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02007508 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007509 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02007510 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511}
7512
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513/*
Bram Moolenaardea25702017-01-29 15:18:10 +01007514 * Handle the argument for a tabpage related ex command.
7515 * Returns a tabpage number.
7516 * When an error is encountered then eap->errmsg is set.
7517 */
7518 static int
7519get_tabpage_arg(exarg_T *eap)
7520{
7521 int tab_number;
7522 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
7523
7524 if (eap->arg && *eap->arg != NUL)
7525 {
7526 char_u *p = eap->arg;
7527 char_u *p_save;
7528 int relative = 0; /* argument +N/-N means: go to N places to the
7529 * right/left relative to the current position. */
7530
7531 if (*p == '-')
7532 {
7533 relative = -1;
7534 p++;
7535 }
7536 else if (*p == '+')
7537 {
7538 relative = 1;
7539 p++;
7540 }
7541
7542 p_save = p;
7543 tab_number = getdigits(&p);
7544
7545 if (relative == 0)
7546 {
7547 if (STRCMP(p, "$") == 0)
7548 tab_number = LAST_TAB_NR;
7549 else if (p == p_save || *p_save == '-' || *p != NUL
7550 || tab_number > LAST_TAB_NR)
7551 {
7552 /* No numbers as argument. */
7553 eap->errmsg = e_invarg;
7554 goto theend;
7555 }
7556 }
7557 else
7558 {
7559 if (*p_save == NUL)
7560 tab_number = 1;
7561 else if (p == p_save || *p_save == '-' || *p != NUL
7562 || tab_number == 0)
7563 {
7564 /* No numbers as argument. */
7565 eap->errmsg = e_invarg;
7566 goto theend;
7567 }
7568 tab_number = tab_number * relative + tabpage_index(curtab);
7569 if (!unaccept_arg0 && relative == -1)
7570 --tab_number;
7571 }
7572 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
7573 eap->errmsg = e_invarg;
7574 }
7575 else if (eap->addr_count > 0)
7576 {
7577 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01007578 {
Bram Moolenaardea25702017-01-29 15:18:10 +01007579 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01007580 tab_number = 0;
7581 }
Bram Moolenaardea25702017-01-29 15:18:10 +01007582 else
7583 {
7584 tab_number = eap->line2;
7585 if (!unaccept_arg0 && **eap->cmdlinep == '-')
7586 {
7587 --tab_number;
7588 if (tab_number < unaccept_arg0)
7589 eap->errmsg = e_invarg;
7590 }
7591 }
7592 }
7593 else
7594 {
7595 switch (eap->cmdidx)
7596 {
7597 case CMD_tabnext:
7598 tab_number = tabpage_index(curtab) + 1;
7599 if (tab_number > LAST_TAB_NR)
7600 tab_number = 1;
7601 break;
7602 case CMD_tabmove:
7603 tab_number = LAST_TAB_NR;
7604 break;
7605 default:
7606 tab_number = tabpage_index(curtab);
7607 }
7608 }
7609
7610theend:
7611 return tab_number;
7612}
7613
7614/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007615 * ":tabclose": close current tab page, unless it is the last one.
7616 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 */
7618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007619ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007621 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007622 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007623
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007624# ifdef FEAT_CMDWIN
7625 if (cmdwin_type != 0)
7626 cmdwin_result = K_IGNORE;
7627 else
7628# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007629 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007630 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007631 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007633 tab_number = get_tabpage_arg(eap);
7634 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007635 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007636 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007637 if (tp == NULL)
7638 {
7639 beep_flush();
7640 return;
7641 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007642 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007643 {
7644 tabpage_close_other(tp, eap->forceit);
7645 return;
7646 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007647 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007648 tabpage_close(eap->forceit);
7649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007651}
7652
7653/*
7654 * ":tabonly": close all tab pages except the current one
7655 */
7656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007657ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007658{
7659 tabpage_T *tp;
7660 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007661 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007662
7663# ifdef FEAT_CMDWIN
7664 if (cmdwin_type != 0)
7665 cmdwin_result = K_IGNORE;
7666 else
7667# endif
7668 if (first_tabpage->tp_next == NULL)
7669 MSG(_("Already only one tab page"));
7670 else
7671 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007672 tab_number = get_tabpage_arg(eap);
7673 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007674 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007675 goto_tabpage(tab_number);
7676 /* Repeat this up to a 1000 times, because autocommands may
7677 * mess up the lists. */
7678 for (done = 0; done < 1000; ++done)
7679 {
7680 FOR_ALL_TABPAGES(tp)
7681 if (tp->tp_topframe != topframe)
7682 {
7683 tabpage_close_other(tp, eap->forceit);
7684 /* if we failed to close it quit */
7685 if (valid_tabpage(tp))
7686 done = 1000;
7687 /* start over, "tp" is now invalid */
7688 break;
7689 }
7690 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007691 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007692 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007693 }
7694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696
7697/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007698 * Close the current tab page.
7699 */
7700 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007701tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007702{
7703 /* First close all the windows but the current one. If that worked then
7704 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01007705 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007706 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01007707 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007708 ex_win_close(forceit, curwin, NULL);
7709# ifdef FEAT_GUI
7710 need_mouse_correct = TRUE;
7711# endif
7712}
7713
7714/*
7715 * Close tab page "tp", which is not the current tab page.
7716 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007717 * Also takes care of the tab pages line disappearing when closing the
7718 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007719 */
7720 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007721tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007722{
7723 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007724 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007725 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007726
7727 /* Limit to 1000 windows, autocommands may add a window while we close
7728 * one. OK, so I'm paranoid... */
7729 while (++done < 1000)
7730 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007731 wp = tp->tp_firstwin;
7732 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007733
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007734 /* Autocommands may delete the tab page under our fingers and we may
7735 * fail to close a window with a modified buffer. */
7736 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007737 break;
7738 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007739
Bram Moolenaar29323592016-07-24 22:04:11 +02007740 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02007741
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007742 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007743 if (h != tabline_height())
7744 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007745}
7746
7747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 * ":only".
7749 */
7750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007751ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007753 win_T *wp;
7754 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755# ifdef FEAT_GUI
7756 need_mouse_correct = TRUE;
7757# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007758 if (eap->addr_count > 0)
7759 {
7760 wnr = eap->line2;
7761 for (wp = firstwin; --wnr > 0; )
7762 {
7763 if (wp->w_next == NULL)
7764 break;
7765 else
7766 wp = wp->w_next;
7767 }
7768 win_goto(wp);
7769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 close_others(TRUE, eap->forceit);
7771}
7772
7773/*
7774 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007775 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007777 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007778ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 if (eap->addr_count == 0)
7781 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007782 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
7785 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01007786ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
Bram Moolenaar2256c992016-11-15 21:17:07 +01007788 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01007789 if (!eap->skip)
7790 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02007791#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01007792 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007793#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01007794 if (eap->addr_count == 0)
7795 win_close(curwin, FALSE); /* don't free buffer */
7796 else
7797 {
7798 int winnr = 0;
7799 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01007800
Bram Moolenaar2256c992016-11-15 21:17:07 +01007801 FOR_ALL_WINDOWS(win)
7802 {
7803 winnr++;
7804 if (winnr == eap->line2)
7805 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007806 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01007807 if (win == NULL)
7808 win = lastwin;
7809 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 }
7812}
7813
7814/*
7815 * ":stop" and ":suspend": Suspend Vim.
7816 */
7817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007818ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 /*
7821 * Disallow suspending for "rvim".
7822 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007823 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
7825 if (!eap->forceit)
7826 autowrite_all();
7827 windgoto((int)Rows - 1, 0);
7828 out_char('\n');
7829 out_flush();
7830 stoptermcap();
7831 out_flush(); /* needed for SUN to restore xterm buffer */
7832#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007833 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834#endif
7835 ui_suspend(); /* call machine specific function */
7836#ifdef FEAT_TITLE
7837 maketitle();
7838 resettitle(); /* force updating the title */
7839#endif
7840 starttermcap();
7841 scroll_start(); /* scroll screen before redrawing */
7842 redraw_later_clear();
7843 shell_resized(); /* may have resized window */
7844 }
7845}
7846
7847/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007848 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 */
7850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007851ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853#ifdef FEAT_CMDWIN
7854 if (cmdwin_type != 0)
7855 {
7856 cmdwin_result = Ctrl_C;
7857 return;
7858 }
7859#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007860 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007861 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007862 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007863 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007864 return;
7865 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01007866
7867 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007868 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869
7870 /*
7871 * if more files or windows we won't exit
7872 */
7873 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7874 exiting = TRUE;
7875 if ( ((eap->cmdidx == CMD_wq
7876 || curbufIsChanged())
7877 && do_write(eap) == FAIL)
7878 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01007879 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {
7881 not_exiting();
7882 }
7883 else
7884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02007887 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888# ifdef FEAT_GUI
7889 need_mouse_correct = TRUE;
7890# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007891 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02007892 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894}
7895
7896/*
7897 * ":print", ":list", ":number".
7898 */
7899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007902 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7903 EMSG(_(e_emptybuf));
7904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007906 for ( ;!got_int; ui_breakcheck())
7907 {
7908 print_line(eap->line1,
7909 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7910 || (eap->flags & EXFLAG_NR)),
7911 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7912 if (++eap->line1 > eap->line2)
7913 break;
7914 out_flush(); /* show one line at a time */
7915 }
7916 setpcmark();
7917 /* put cursor at last line */
7918 curwin->w_cursor.lnum = eap->line2;
7919 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 }
7921
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923}
7924
7925#ifdef FEAT_BYTEOFF
7926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007927ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
7929 goto_byte(eap->line2);
7930}
7931#endif
7932
7933/*
7934 * ":shell".
7935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007937ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938{
7939 do_shell(NULL, 0);
7940}
7941
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007942#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007944static int drop_busy = FALSE;
7945static int drop_filec;
7946static char_u **drop_filev = NULL;
7947static int drop_split;
7948static void (*drop_callback)(void *);
7949static void *drop_cookie;
7950
7951 static void
7952handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953{
7954 exarg_T ea;
7955 int save_msg_scroll = msg_scroll;
7956
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007957 // Setting the argument list may cause screen updates and being called
7958 // recursively. Avoid that by setting drop_busy.
7959 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960
7961 /* Check whether the current buffer is changed. If so, we will need
7962 * to split the current window or data could be lost.
7963 * We don't need to check if the 'hidden' option is set, as in this
7964 * case the buffer won't be lost.
7965 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007966 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {
7968 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007969 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 --emsg_off;
7971 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007972 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (win_split(0, 0) == FAIL)
7975 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007976 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977
7978 /* When splitting the window, create a new alist. Otherwise the
7979 * existing one is overwritten. */
7980 alist_unlink(curwin->w_alist);
7981 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 }
7983
7984 /*
7985 * Set up the new argument list.
7986 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02007987 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988
7989 /*
7990 * Move to the first file.
7991 */
7992 /* Fake up a minimal "next" command for do_argfile() */
7993 vim_memset(&ea, 0, sizeof(ea));
7994 ea.cmd = (char_u *)"next";
7995 do_argfile(&ea, 0);
7996
7997 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7998 * mode that is not needed here. */
7999 need_start_insertmode = FALSE;
8000
8001 /* Restore msg_scroll, otherwise a following command may cause scrolling
8002 * unexpectedly. The screen will be redrawn by the caller, thus
8003 * msg_scroll being set by displaying a message is irrelevant. */
8004 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02008005
8006 if (drop_callback != NULL)
8007 drop_callback(drop_cookie);
8008
8009 drop_filev = NULL;
8010 drop_busy = FALSE;
8011}
8012
8013/*
8014 * Handle a file drop. The code is here because a drop is *nearly* like an
8015 * :args command, but not quite (we have a list of exact filenames, so we
8016 * don't want to (a) parse a command line, or (b) expand wildcards. So the
8017 * code is very similar to :args and hence needs access to a lot of the static
8018 * functions in this file.
8019 *
8020 * The "filev" list must have been allocated using alloc(), as should each item
8021 * in the list. This function takes over responsibility for freeing the "filev"
8022 * list.
8023 */
8024 void
8025handle_drop(
8026 int filec, // the number of files dropped
8027 char_u **filev, // the list of files dropped
8028 int split, // force splitting the window
8029 void (*callback)(void *), // to be called after setting the argument
8030 // list
8031 void *cookie) // argument for "callback" (allocated)
8032{
8033 // Cannot handle recursive drops, finish the pending one.
8034 if (drop_busy)
8035 {
8036 FreeWild(filec, filev);
8037 vim_free(cookie);
8038 return;
8039 }
8040
8041 // When calling handle_drop() more than once in a row we only use the last
8042 // one.
8043 if (drop_filev != NULL)
8044 {
8045 FreeWild(drop_filec, drop_filev);
8046 vim_free(drop_cookie);
8047 }
8048
8049 drop_filec = filec;
8050 drop_filev = filev;
8051 drop_split = split;
8052 drop_callback = callback;
8053 drop_cookie = cookie;
8054
8055 // Postpone this when:
8056 // - editing the command line
8057 // - not possible to change the current buffer
8058 // - updating the screen
8059 // As it may change buffers and window structures that are in use and cause
8060 // freed memory to be used.
8061 if (text_locked() || curbuf_locked() || updating_screen)
8062 return;
8063
8064 handle_drop_internal();
8065}
8066
8067/*
8068 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
8069 * reset: Handle a postponed drop.
8070 */
8071 void
8072handle_any_postponed_drop(void)
8073{
8074 if (!drop_busy && drop_filev != NULL
8075 && !text_locked() && !curbuf_locked() && !updating_screen)
8076 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077}
8078#endif
8079
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080/*
8081 * Clear an argument list: free all file names and reset it to zero entries.
8082 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 while (--al->al_ga.ga_len >= 0)
8087 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
8088 ga_clear(&al->al_ga);
8089}
8090
8091/*
8092 * Init an argument list.
8093 */
8094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008095alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
8098}
8099
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100/*
8101 * Remove a reference from an argument list.
8102 * Ignored when the argument list is the global one.
8103 * If the argument list is no longer used by any window, free it.
8104 */
8105 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008106alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 if (al != &global_alist && --al->al_refcount <= 0)
8109 {
8110 alist_clear(al);
8111 vim_free(al);
8112 }
8113}
8114
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115/*
8116 * Create a new argument list and use it for the current window.
8117 */
8118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008119alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
8122 if (curwin->w_alist == NULL)
8123 {
8124 curwin->w_alist = &global_alist;
8125 ++global_alist.al_refcount;
8126 }
8127 else
8128 {
8129 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008130 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 alist_init(curwin->w_alist);
8132 }
8133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134
Bram Moolenaara06ecab2016-07-16 14:47:36 +02008135#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136/*
8137 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008138 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
8139 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 */
8141 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008142alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143{
8144 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008145 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 char_u **new_arg_files;
8147 int new_arg_file_count;
8148 char_u *save_p_su = p_su;
8149 int i;
8150
8151 /* Don't use 'suffixes' here. This should work like the shell did the
8152 * expansion. Also, the vimrc file isn't read yet, thus the user
8153 * can't set the options. */
8154 p_su = empty_option;
8155 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
8156 if (old_arg_files != NULL)
8157 {
8158 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008159 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
8160 old_arg_count = GARGCOUNT;
8161 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02008163 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 && new_arg_file_count > 0)
8165 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00008166 alist_set(&global_alist, new_arg_file_count, new_arg_files,
8167 TRUE, fnum_list, fnum_len);
8168 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 }
8171 p_su = save_p_su;
8172}
8173#endif
8174
8175/*
8176 * Set the argument list for the current window.
8177 * Takes over the allocated files[] and the allocated fnames in it.
8178 */
8179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008180alist_set(
8181 alist_T *al,
8182 int count,
8183 char_u **files,
8184 int use_curbuf,
8185 int *fnum_list,
8186 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187{
8188 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008189 static int recursive = 0;
8190
8191 if (recursive)
8192 {
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008193 EMSG(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008194 return;
8195 }
8196 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197
8198 alist_clear(al);
8199 if (ga_grow(&al->al_ga, count) == OK)
8200 {
8201 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008202 {
8203 if (got_int)
8204 {
8205 /* When adding many buffers this can take a long time. Allow
8206 * interrupting here. */
8207 while (i < count)
8208 vim_free(files[i++]);
8209 break;
8210 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00008211
8212 /* May set buffer name of a buffer previously used for the
8213 * argument list, so that it's re-used by alist_add. */
8214 if (fnum_list != NULL && i < fnum_len)
8215 buf_set_name(fnum_list[i], files[i]);
8216
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00008218 ui_breakcheck();
8219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 vim_free(files);
8221 }
8222 else
8223 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01008226
8227 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
8230/*
8231 * Add file "fname" to argument list "al".
8232 * "fname" must have been allocated and "al" must have been checked for room.
8233 */
8234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008235alist_add(
8236 alist_T *al,
8237 char_u *fname,
8238 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239{
8240 if (fname == NULL) /* don't add NULL file names */
8241 return;
8242#ifdef BACKSLASH_IN_FILENAME
8243 slash_adjust(fname);
8244#endif
8245 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
8246 if (set_fnum > 0)
8247 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
8248 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
8249 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250}
8251
8252#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
8253/*
8254 * Adjust slashes in file names. Called after 'shellslash' was set.
8255 */
8256 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008257alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008261 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262
8263 for (i = 0; i < GARGCOUNT; ++i)
8264 if (GARGLIST[i].ae_fname != NULL)
8265 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008266 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (wp->w_alist != &global_alist)
8268 for (i = 0; i < WARGCOUNT(wp); ++i)
8269 if (WARGLIST(wp)[i].ae_fname != NULL)
8270 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271}
8272#endif
8273
8274/*
8275 * ":preserve".
8276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008278ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008280 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 ml_preserve(curbuf, TRUE);
8282}
8283
8284/*
8285 * ":recover".
8286 */
8287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008288ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289{
8290 /* Set recoverymode right away to avoid the ATTENTION prompt. */
8291 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01008292 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
8293 | CCGD_MULTWIN
8294 | (eap->forceit ? CCGD_FORCEIT : 0)
8295 | CCGD_EXCMD)
8296
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 && (*eap->arg == NUL
8298 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
8299 ml_recover();
8300 recoverymode = FALSE;
8301}
8302
8303/*
8304 * Command modifier used in a wrong way.
8305 */
8306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008307ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
8309 eap->errmsg = e_invcmd;
8310}
8311
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312/*
8313 * :sview [+command] file split window with new file, read-only
8314 * :split [[+command] file] split window with current or new file
8315 * :vsplit [[+command] file] split window vertically with current or new file
8316 * :new [[+command] file] split window with no or new file
8317 * :vnew [[+command] file] split vertically window with no or new file
8318 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008319 *
8320 * :tabedit open new Tab page with empty window
8321 * :tabedit [+command] file open new Tab page and edit "file"
8322 * :tabnew [[+command] file] just like :tabedit
8323 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 */
8325 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008326ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008328 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008329#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008331#endif
8332#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008334#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02008335 int use_tab = eap->cmdidx == CMD_tabedit
8336 || eap->cmdidx == CMD_tabfind
8337 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
Bram Moolenaar4033c552017-09-16 20:54:51 +02008339#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342
Bram Moolenaar4033c552017-09-16 20:54:51 +02008343#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008345 * quickfix windows. But it's OK when doing ":tab split". */
8346 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {
8348 if (eap->cmdidx == CMD_split)
8349 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 if (eap->cmdidx == CMD_vsplit)
8351 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354
Bram Moolenaar4033c552017-09-16 20:54:51 +02008355#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008356 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {
8358 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
8359 FNAME_MESS, TRUE, curbuf->b_ffname);
8360 if (fname == NULL)
8361 goto theend;
8362 eap->arg = fname;
8363 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008364# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02008365 else
8366# endif
8367#endif
8368#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 && eap->cmdidx != CMD_new)
8372 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008373 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008374# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008375 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008376# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008377 au_has_group((char_u *)"FileExplorer"))
8378 {
8379 /* No browsing supported but we do have the file explorer:
8380 * Edit the directory. */
8381 if (*eap->arg == NUL || !mch_isdir(eap->arg))
8382 eap->arg = (char_u *)".";
8383 }
8384 else
8385 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02008386 fname = do_browse(0, (char_u *)(use_tab
8387 ? _("Edit File in new tab page")
8388 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008390 if (fname == NULL)
8391 goto theend;
8392 eap->arg = fname;
8393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 }
8395 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02008396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008398 /*
8399 * Either open new tab page or split the window.
8400 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02008401 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008402 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00008403 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
8404 : eap->addr_count == 0 ? 0
8405 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008406 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00008407 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008408
8409 /* set the alternate buffer for the window we came from */
8410 if (curwin != old_curwin
8411 && win_valid(old_curwin)
8412 && old_curwin->w_buffer != curbuf
8413 && !cmdmod.keepalt)
8414 old_curwin->w_alt_fnum = curbuf->b_fnum;
8415 }
8416 }
8417 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
8419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 /* Reset 'scrollbind' when editing another file, but keep it when
8421 * doing ":split" without arguments. */
8422 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008423# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01008425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02008427 {
8428 RESET_BINDING(curwin);
8429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 else
8431 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 do_exedit(eap, old_curwin);
8433 }
8434
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008435# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008439# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440theend:
8441 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008444
8445/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008446 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008447 */
8448 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008449tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008450{
8451 exarg_T ea;
8452
8453 vim_memset(&ea, 0, sizeof(ea));
8454 ea.cmdidx = CMD_tabnew;
8455 ea.cmd = (char_u *)"tabn";
8456 ea.arg = (char_u *)"";
8457 ex_splitview(&ea);
8458}
8459
8460/*
8461 * :tabnext command
8462 */
8463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008464ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008465{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008466 int tab_number;
8467
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008468 switch (eap->cmdidx)
8469 {
8470 case CMD_tabfirst:
8471 case CMD_tabrewind:
8472 goto_tabpage(1);
8473 break;
8474 case CMD_tablast:
8475 goto_tabpage(9999);
8476 break;
8477 case CMD_tabprevious:
8478 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008479 if (eap->arg && *eap->arg != NUL)
8480 {
8481 char_u *p = eap->arg;
8482 char_u *p_save = p;
8483
8484 tab_number = getdigits(&p);
8485 if (p == p_save || *p_save == '-' || *p != NUL
8486 || tab_number == 0)
8487 {
8488 /* No numbers as argument. */
8489 eap->errmsg = e_invarg;
8490 return;
8491 }
8492 }
8493 else
8494 {
8495 if (eap->addr_count == 0)
8496 tab_number = 1;
8497 else
8498 {
8499 tab_number = eap->line2;
8500 if (tab_number < 1)
8501 {
8502 eap->errmsg = e_invrange;
8503 return;
8504 }
8505 }
8506 }
8507 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008508 break;
8509 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008510 tab_number = get_tabpage_arg(eap);
8511 if (eap->errmsg == NULL)
8512 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008513 break;
8514 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008515}
8516
8517/*
8518 * :tabmove command
8519 */
8520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008521ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008522{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02008523 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02008524
Bram Moolenaar2f72c702017-01-29 14:48:10 +01008525 tab_number = get_tabpage_arg(eap);
8526 if (eap->errmsg == NULL)
8527 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008528}
8529
8530/*
8531 * :tabs command: List tabs and their contents.
8532 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008534ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008535{
8536 tabpage_T *tp;
8537 win_T *wp;
8538 int tabcount = 1;
8539
8540 msg_start();
8541 msg_scroll = TRUE;
8542 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
8543 {
8544 msg_putchar('\n');
8545 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008546 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008547 out_flush(); /* output one line at a time */
8548 ui_breakcheck();
8549
Bram Moolenaar030f0df2006-02-21 22:02:53 +00008550 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008551 wp = firstwin;
8552 else
8553 wp = tp->tp_firstwin;
8554 for ( ; wp != NULL && !got_int; wp = wp->w_next)
8555 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008556 msg_putchar('\n');
8557 msg_putchar(wp == curwin ? '>' : ' ');
8558 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008559 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8560 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008561 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008562 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008563 else
8564 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8565 IObuff, IOSIZE, TRUE);
8566 msg_outtrans(IObuff);
8567 out_flush(); /* output one line at a time */
8568 ui_breakcheck();
8569 }
8570 }
8571}
8572
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573/*
8574 * ":mode": Set screen mode.
8575 * If no argument given, just get the screen size and redraw.
8576 */
8577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008578ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
8580 if (*eap->arg == NUL)
8581 shell_resized();
8582 else
8583 mch_screenmode(eap->arg);
8584}
8585
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586/*
8587 * ":resize".
8588 * set, increment or decrement current window height
8589 */
8590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008591ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 int n;
8594 win_T *wp = curwin;
8595
8596 if (eap->addr_count > 0)
8597 {
8598 n = eap->line2;
8599 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8600 ;
8601 }
8602
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008603# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 if (cmdmod.split & WSP_VERT)
8608 {
8609 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02008610 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8612 n = 9999;
8613 win_setwidth_win((int)n, wp);
8614 }
8615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {
8617 if (*eap->arg == '-' || *eap->arg == '+')
8618 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02008619 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 n = 9999;
8621 win_setheight_win((int)n, wp);
8622 }
8623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624
8625/*
8626 * ":find [+command] <file>" command.
8627 */
8628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008629ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631#ifdef FEAT_SEARCHPATH
8632 char_u *fname;
8633 int count;
8634
8635 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8636 TRUE, curbuf->b_ffname);
8637 if (eap->addr_count > 0)
8638 {
8639 /* Repeat finding the file "count" times. This matters when it
8640 * appears several times in the path. */
8641 count = eap->line2;
8642 while (fname != NULL && --count > 0)
8643 {
8644 vim_free(fname);
8645 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8646 FALSE, curbuf->b_ffname);
8647 }
8648 }
8649
8650 if (fname != NULL)
8651 {
8652 eap->arg = fname;
8653#endif
8654 do_exedit(eap, NULL);
8655#ifdef FEAT_SEARCHPATH
8656 vim_free(fname);
8657 }
8658#endif
8659}
8660
8661/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008662 * ":open" simulation: for now just work like ":visual".
8663 */
8664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008665ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008666{
8667 regmatch_T regmatch;
8668 char_u *p;
8669
8670 curwin->w_cursor.lnum = eap->line2;
8671 beginline(BL_SOL | BL_FIX);
8672 if (*eap->arg == '/')
8673 {
8674 /* ":open /pattern/": put cursor in column found with pattern */
8675 ++eap->arg;
8676 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8677 *p = NUL;
8678 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8679 if (regmatch.regprog != NULL)
8680 {
8681 regmatch.rm_ic = p_ic;
8682 p = ml_get_curline();
8683 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008684 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008685 else
8686 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008687 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008688 }
8689 /* Move to the NUL, ignore any other arguments. */
8690 eap->arg += STRLEN(eap->arg);
8691 }
8692 check_cursor();
8693
8694 eap->cmdidx = CMD_visual;
8695 do_exedit(eap, NULL);
8696}
8697
8698/*
8699 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 */
8701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008702ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
8704 do_exedit(eap, NULL);
8705}
8706
8707/*
8708 * ":edit <file>" command and alikes.
8709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711do_exedit(
8712 exarg_T *eap,
8713 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
8715 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008717 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718
8719 /*
8720 * ":vi" command ends Ex mode.
8721 */
8722 if (exmode_active && (eap->cmdidx == CMD_visual
8723 || eap->cmdidx == CMD_view))
8724 {
8725 exmode_active = FALSE;
8726 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008727 {
8728 /* Special case: ":global/pat/visual\NLvi-commands" */
8729 if (global_busy)
8730 {
8731 int rd = RedrawingDisabled;
8732 int nwr = no_wait_return;
8733 int ms = msg_scroll;
8734#ifdef FEAT_GUI
8735 int he = hold_gui_events;
8736#endif
8737
8738 if (eap->nextcmd != NULL)
8739 {
8740 stuffReadbuff(eap->nextcmd);
8741 eap->nextcmd = NULL;
8742 }
8743
8744 if (exmode_was != EXMODE_VIM)
8745 settmode(TMODE_RAW);
8746 RedrawingDisabled = 0;
8747 no_wait_return = 0;
8748 need_wait_return = FALSE;
8749 msg_scroll = 0;
8750#ifdef FEAT_GUI
8751 hold_gui_events = 0;
8752#endif
8753 must_redraw = CLEAR;
8754
8755 main_loop(FALSE, TRUE);
8756
8757 RedrawingDisabled = rd;
8758 no_wait_return = nwr;
8759 msg_scroll = ms;
8760#ifdef FEAT_GUI
8761 hold_gui_events = he;
8762#endif
8763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 }
8767
8768 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008769 || eap->cmdidx == CMD_tabnew
8770 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02008771 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008773 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 setpcmark();
8775 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008776 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8777 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02008779 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 || *eap->arg != NUL
8781#ifdef FEAT_BROWSE
8782 || cmdmod.browse
8783#endif
8784 )
8785 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008786 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8787 * can bring us here, others are stopped earlier. */
8788 if (*eap->arg != NUL && curbuf_locked())
8789 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008790
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 n = readonlymode;
8792 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8793 readonlymode = TRUE;
8794 else if (eap->cmdidx == CMD_enew)
8795 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8796 empty buffer */
8797 setpcmark();
8798 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8799 NULL, eap,
8800 /* ":edit" goes to first line if Vi compatible */
8801 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8802 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8803 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02008804 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008806 /* after a split we can use an existing buffer */
8807 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008809 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {
8811 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 if (old_curwin != NULL)
8813 {
8814 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02008815 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008817#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008818 cleanup_T cs;
8819
8820 /* Reset the error/interrupt/exception state here so that
8821 * aborting() returns FALSE when closing a window. */
8822 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008823#endif
8824#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008826#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02008827 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008828
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008829#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008830 /* Restore the error/interrupt/exception state if not
8831 * discarded by a new aborting error, interrupt, or
8832 * uncaught exception. */
8833 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02008834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 }
8836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 }
8838 else if (readonlymode && curbuf->b_nwindows == 1)
8839 {
8840 /* When editing an already visited buffer, 'readonly' won't be set
8841 * but the previous value is kept. With ":view" and ":sview" we
8842 * want the file to be readonly, except when another window is
8843 * editing the same buffer. */
8844 curbuf->b_p_ro = TRUE;
8845 }
8846 readonlymode = n;
8847 }
8848 else
8849 {
8850 if (eap->do_ecmd_cmd != NULL)
8851 do_cmdline_cmd(eap->do_ecmd_cmd);
8852#ifdef FEAT_TITLE
8853 n = curwin->w_arg_idx_invalid;
8854#endif
8855 check_arg_idx(curwin);
8856#ifdef FEAT_TITLE
8857 if (n != curwin->w_arg_idx_invalid)
8858 maketitle();
8859#endif
8860 }
8861
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 /*
8863 * if ":split file" worked, set alternate file name in old window to new
8864 * file
8865 */
8866 if (old_curwin != NULL
8867 && *eap->arg != NUL
8868 && curwin != old_curwin
8869 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008870 && old_curwin->w_buffer != curbuf
8871 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873
8874 ex_no_reprint = TRUE;
8875}
8876
8877#ifndef FEAT_GUI
8878/*
8879 * ":gui" and ":gvim" when there is no GUI.
8880 */
8881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008882ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884 eap->errmsg = e_nogvim;
8885}
8886#endif
8887
8888#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008890ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891{
8892 gui_make_tearoff(eap->arg);
8893}
8894#endif
8895
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008896#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
8897 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008899ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01008901# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
8902 if (gui.in_use)
8903 gui_make_popup(eap->arg, eap->forceit);
8904# ifdef FEAT_TERM_POPUP_MENU
8905 else
8906# endif
8907# endif
8908# ifdef FEAT_TERM_POPUP_MENU
8909 pum_make_popup(eap->arg, eap->forceit);
8910# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911}
8912#endif
8913
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008915ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
8917 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8918 MSG(_("No swap file"));
8919 else
8920 msg(curbuf->b_ml.ml_mfp->mf_fname);
8921}
8922
8923/*
8924 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8925 * offset.
8926 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008929ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008932 win_T *save_curwin = curwin;
8933 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 long topline;
8935 long y;
8936 linenr_T old_linenr = curwin->w_cursor.lnum;
8937
8938 setpcmark();
8939
8940 /*
8941 * determine max topline
8942 */
8943 if (curwin->w_p_scb)
8944 {
8945 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02008946 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 {
8948 if (wp->w_p_scb && wp->w_buffer)
8949 {
8950 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8951 if (topline > y)
8952 topline = y;
8953 }
8954 }
8955 if (topline < 1)
8956 topline = 1;
8957 }
8958 else
8959 {
8960 topline = 1;
8961 }
8962
8963
8964 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008965 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 */
Bram Moolenaar29323592016-07-24 22:04:11 +02008967 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 {
8969 if (curwin->w_p_scb)
8970 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008971 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 y = topline - curwin->w_topline;
8973 if (y > 0)
8974 scrollup(y, TRUE);
8975 else
8976 scrolldown(-y, TRUE);
8977 curwin->w_scbind_pos = topline;
8978 redraw_later(VALID);
8979 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 }
8982 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008983 curwin = save_curwin;
8984 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 if (curwin->w_p_scb)
8986 {
8987 did_syncbind = TRUE;
8988 checkpcmark();
8989 if (old_linenr != curwin->w_cursor.lnum)
8990 {
8991 char_u ctrl_o[2];
8992
8993 ctrl_o[0] = Ctrl_O;
8994 ctrl_o[1] = 0;
8995 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8996 }
8997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998}
8999
9000
9001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009002ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
Bram Moolenaardf177f62005-02-22 08:39:57 +00009004 int i;
9005 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9006 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007
9008 if (eap->usefilter) /* :r!cmd */
9009 do_bang(1, eap, FALSE, FALSE, TRUE);
9010 else
9011 {
9012 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
9013 return;
9014
9015#ifdef FEAT_BROWSE
9016 if (cmdmod.browse)
9017 {
9018 char_u *browseFile;
9019
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009020 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 NULL, NULL, NULL, curbuf);
9022 if (browseFile != NULL)
9023 {
9024 i = readfile(browseFile, NULL,
9025 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9026 vim_free(browseFile);
9027 }
9028 else
9029 i = OK;
9030 }
9031 else
9032#endif
9033 if (*eap->arg == NUL)
9034 {
9035 if (check_fname() == FAIL) /* check for no file name */
9036 return;
9037 i = readfile(curbuf->b_ffname, curbuf->b_fname,
9038 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9039 }
9040 else
9041 {
9042 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
9043 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
9044 i = readfile(eap->arg, NULL,
9045 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
9046
9047 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01009048 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009050#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 if (!aborting())
9052#endif
9053 EMSG2(_(e_notopen), eap->arg);
9054 }
9055 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009056 {
9057 if (empty && exmode_active)
9058 {
9059 /* Delete the empty line that remains. Historically ex does
9060 * this but vi doesn't. */
9061 if (eap->line2 == 0)
9062 lnum = curbuf->b_ml.ml_line_count;
9063 else
9064 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009065 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009066 {
9067 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009068 if (curwin->w_cursor.lnum > 1
9069 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009070 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00009071 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009072 }
9073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 }
9077}
9078
Bram Moolenaarea408852005-06-25 22:49:46 +00009079static char_u *prev_dir = NULL;
9080
9081#if defined(EXITFREE) || defined(PROTO)
9082 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009083free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00009084{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009085 VIM_CLEAR(prev_dir);
9086 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00009087}
9088#endif
9089
Bram Moolenaarf4258302013-06-02 18:20:17 +02009090/*
9091 * Deal with the side effects of changing the current directory.
9092 * When "local" is TRUE then this was after an ":lcd" command.
9093 */
9094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009095post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02009096{
Bram Moolenaard23a8232018-02-10 18:45:26 +01009097 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009098 if (local)
9099 {
9100 /* If still in global directory, need to remember current
9101 * directory as global directory. */
9102 if (globaldir == NULL && prev_dir != NULL)
9103 globaldir = vim_strsave(prev_dir);
9104 /* Remember this local directory for the window. */
9105 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9106 curwin->w_localdir = vim_strsave(NameBuff);
9107 }
9108 else
9109 {
9110 /* We are now in the global directory, no need to remember its
9111 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01009112 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02009113 }
9114
9115 shorten_fnames(TRUE);
9116}
9117
Bram Moolenaarea408852005-06-25 22:49:46 +00009118
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119/*
9120 * ":cd", ":lcd", ":chdir" and ":lchdir".
9121 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00009122 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009123ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 char_u *new_dir;
9126 char_u *tofree;
9127
9128 new_dir = eap->arg;
9129#if !defined(UNIX) && !defined(VMS)
9130 /* for non-UNIX ":cd" means: print current directory */
9131 if (*new_dir == NUL)
9132 ex_pwd(NULL);
9133 else
9134#endif
9135 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00009136 if (allbuf_locked())
9137 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009138 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
9139 && !eap->forceit)
9140 {
Bram Moolenaar81870892007-11-11 18:17:28 +00009141 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00009142 return;
9143 }
9144
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 /* ":cd -": Change to previous directory */
9146 if (STRCMP(new_dir, "-") == 0)
9147 {
9148 if (prev_dir == NULL)
9149 {
9150 EMSG(_("E186: No previous directory"));
9151 return;
9152 }
9153 new_dir = prev_dir;
9154 }
9155
9156 /* Save current directory for next ":cd -" */
9157 tofree = prev_dir;
9158 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9159 prev_dir = vim_strsave(NameBuff);
9160 else
9161 prev_dir = NULL;
9162
9163#if defined(UNIX) || defined(VMS)
9164 /* for UNIX ":cd" means: go to home directory */
9165 if (*new_dir == NUL)
9166 {
9167 /* use NameBuff for home directory name */
9168# ifdef VMS
9169 char_u *p;
9170
9171 p = mch_getenv((char_u *)"SYS$LOGIN");
9172 if (p == NULL || *p == NUL) /* empty is the same as not set */
9173 NameBuff[0] = NUL;
9174 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00009175 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176# else
9177 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
9178# endif
9179 new_dir = NameBuff;
9180 }
9181#endif
9182 if (new_dir == NULL || vim_chdir(new_dir))
9183 EMSG(_(e_failed));
9184 else
9185 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009186 int is_local_chdir = eap->cmdidx == CMD_lcd
9187 || eap->cmdidx == CMD_lchdir;
9188
9189 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
9191 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00009192 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 ex_pwd(eap);
Bram Moolenaarb7407d32018-02-03 17:36:27 +01009194 apply_autocmds(EVENT_DIRCHANGED,
9195 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
9196 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 }
9198 vim_free(tofree);
9199 }
9200}
9201
9202/*
9203 * ":pwd".
9204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009206ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207{
9208 if (mch_dirname(NameBuff, MAXPATHL) == OK)
9209 {
9210#ifdef BACKSLASH_IN_FILENAME
9211 slash_adjust(NameBuff);
9212#endif
9213 msg(NameBuff);
9214 }
9215 else
9216 EMSG(_("E187: Unknown"));
9217}
9218
9219/*
9220 * ":=".
9221 */
9222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009223ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224{
9225 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009226 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227}
9228
9229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009230ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009232 int n;
9233 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
9235 if (cursor_valid())
9236 {
9237 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
9238 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02009239 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009241
9242 len = eap->line2;
9243 switch (*eap->arg)
9244 {
9245 case 'm': break;
9246 case NUL: len *= 1000L; break;
9247 default: EMSG2(_(e_invarg2), eap->arg); return;
9248 }
9249 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250}
9251
9252/*
9253 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
9254 */
9255 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009256do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
9258 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01009259 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260
9261 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02009262 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01009263 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01009265 wait_now = msec - done > 1000L ? 1000L : msec - done;
9266#ifdef FEAT_TIMERS
9267 {
9268 long due_time = check_due_timer();
9269
9270 if (due_time > 0 && due_time < wait_now)
9271 wait_now = due_time;
9272 }
9273#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009274#ifdef FEAT_JOB_CHANNEL
9275 if (has_any_channel() && wait_now > 100L)
9276 wait_now = 100L;
9277#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01009278 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02009279#ifdef FEAT_JOB_CHANNEL
9280 if (has_any_channel())
9281 ui_breakcheck_force(TRUE);
9282 else
9283#endif
9284 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02009285#ifdef MESSAGE_QUEUE
9286 /* Process the netbeans and clientserver messages that may have been
9287 * received in the call to ui_breakcheck() when the GUI is in use. This
9288 * may occur when running a test case. */
9289 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02009290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02009292
9293 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
9294 // input buffer, otherwise a following call to input() fails.
9295 if (got_int)
9296 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297}
9298
9299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009300do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 int mode;
9303 char_u *cmdp;
9304
9305 cmdp = eap->cmd;
9306 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
9307
9308 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
9309 eap->arg, mode, isabbrev))
9310 {
9311 case 1: EMSG(_(e_invarg));
9312 break;
9313 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
9314 break;
9315 }
9316}
9317
9318/*
9319 * ":winsize" command (obsolete).
9320 */
9321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009322ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323{
9324 int w, h;
9325 char_u *arg = eap->arg;
9326 char_u *p;
9327
9328 w = getdigits(&arg);
9329 arg = skipwhite(arg);
9330 p = arg;
9331 h = getdigits(&arg);
9332 if (*p != NUL && *arg == NUL)
9333 set_shellsize(w, h, TRUE);
9334 else
9335 EMSG(_("E465: :winsize requires two number arguments"));
9336}
9337
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009339ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341 int xchar = NUL;
9342 char_u *p;
9343
9344 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
9345 {
9346 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
9347 if (eap->arg[1] == NUL)
9348 {
9349 EMSG(_(e_invarg));
9350 return;
9351 }
9352 xchar = eap->arg[1];
9353 p = eap->arg + 2;
9354 }
9355 else
9356 p = eap->arg + 1;
9357
9358 eap->nextcmd = check_nextcmd(p);
9359 p = skipwhite(p);
9360 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
9361 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02009362 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 {
9364 /* Pass flags on for ":vertical wincmd ]". */
9365 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009366 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
9368 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009369 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 }
9371}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372
Bram Moolenaar843ee412004-06-30 16:16:41 +00009373#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374/*
9375 * ":winpos".
9376 */
9377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009378ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380 int x, y;
9381 char_u *arg = eap->arg;
9382 char_u *p;
9383
9384 if (*arg == NUL)
9385 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00009386# if defined(FEAT_GUI) || defined(MSWIN)
9387# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00009389# else
9390 if (mch_get_winpos(&x, &y) != FAIL)
9391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 {
9393 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
9394 msg(IObuff);
9395 }
9396 else
9397# endif
9398 EMSG(_("E188: Obtaining window position not implemented for this platform"));
9399 }
9400 else
9401 {
9402 x = getdigits(&arg);
9403 arg = skipwhite(arg);
9404 p = arg;
9405 y = getdigits(&arg);
9406 if (*p == NUL || *arg != NUL)
9407 {
9408 EMSG(_("E466: :winpos requires two number arguments"));
9409 return;
9410 }
9411# ifdef FEAT_GUI
9412 if (gui.in_use)
9413 gui_mch_set_winpos(x, y);
9414 else if (gui.starting)
9415 {
9416 /* Remember the coordinates for when the window is opened. */
9417 gui_win_x = x;
9418 gui_win_y = y;
9419 }
9420# ifdef HAVE_TGETENT
9421 else
9422# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009423# else
9424# ifdef MSWIN
9425 mch_set_winpos(x, y);
9426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427# endif
9428# ifdef HAVE_TGETENT
9429 if (*T_CWP)
9430 term_set_winpos(x, y);
9431# endif
9432 }
9433}
9434#endif
9435
9436/*
9437 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
9438 */
9439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009440ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442 oparg_T oa;
9443
9444 clear_oparg(&oa);
9445 oa.regname = eap->regname;
9446 oa.start.lnum = eap->line1;
9447 oa.end.lnum = eap->line2;
9448 oa.line_count = eap->line2 - eap->line1 + 1;
9449 oa.motion_type = MLINE;
9450#ifdef FEAT_VIRTUALEDIT
9451 virtual_op = FALSE;
9452#endif
9453 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
9454 {
9455 setpcmark();
9456 curwin->w_cursor.lnum = eap->line1;
9457 beginline(BL_SOL | BL_FIX);
9458 }
9459
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009460 if (VIsual_active)
9461 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01009462
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 switch (eap->cmdidx)
9464 {
9465 case CMD_delete:
9466 oa.op_type = OP_DELETE;
9467 op_delete(&oa);
9468 break;
9469
9470 case CMD_yank:
9471 oa.op_type = OP_YANK;
9472 (void)op_yank(&oa, FALSE, TRUE);
9473 break;
9474
9475 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02009476 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02009478 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
9479#else
9480 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02009482 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 oa.op_type = OP_RSHIFT;
9484 else
9485 oa.op_type = OP_LSHIFT;
9486 op_shift(&oa, FALSE, eap->amount);
9487 break;
9488 }
9489#ifdef FEAT_VIRTUALEDIT
9490 virtual_op = MAYBE;
9491#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00009492 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493}
9494
9495/*
9496 * ":put".
9497 */
9498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009499ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501 /* ":0put" works like ":1put!". */
9502 if (eap->line2 == 0)
9503 {
9504 eap->line2 = 1;
9505 eap->forceit = TRUE;
9506 }
9507 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009508 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
9509 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510}
9511
9512/*
9513 * Handle ":copy" and ":move".
9514 */
9515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009516ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 long n;
9519
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02009520 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 if (eap->arg == NULL) /* error detected */
9522 {
9523 eap->nextcmd = NULL;
9524 return;
9525 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009526 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527
9528 /*
9529 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
9530 */
9531 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
9532 {
9533 EMSG(_(e_invaddr));
9534 return;
9535 }
9536
9537 if (eap->cmdidx == CMD_move)
9538 {
9539 if (do_move(eap->line1, eap->line2, n) == FAIL)
9540 return;
9541 }
9542 else
9543 ex_copy(eap->line1, eap->line2, n);
9544 u_clearline();
9545 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009546 ex_may_print(eap);
9547}
9548
9549/*
9550 * Print the current line if flags were given to the Ex command.
9551 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009552 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009553ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009554{
9555 if (eap->flags != 0)
9556 {
9557 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9558 (eap->flags & EXFLAG_LIST));
9559 ex_no_reprint = TRUE;
9560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561}
9562
9563/*
9564 * ":smagic" and ":snomagic".
9565 */
9566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009567ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 int magic_save = p_magic;
9570
9571 p_magic = (eap->cmdidx == CMD_smagic);
9572 do_sub(eap);
9573 p_magic = magic_save;
9574}
9575
9576/*
9577 * ":join".
9578 */
9579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009580ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582 curwin->w_cursor.lnum = eap->line1;
9583 if (eap->line1 == eap->line2)
9584 {
9585 if (eap->addr_count >= 2) /* :2,2join does nothing */
9586 return;
9587 if (eap->line2 == curbuf->b_ml.ml_line_count)
9588 {
9589 beep_flush();
9590 return;
9591 }
9592 ++eap->line2;
9593 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009594 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009596 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597}
9598
9599/*
9600 * ":[addr]@r" or ":[addr]*r": execute register
9601 */
9602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009603ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604{
9605 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009606 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607
9608 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02009609 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
9611#ifdef USE_ON_FLY_SCROLL
9612 dont_scroll = TRUE; /* disallow scrolling here */
9613#endif
9614
9615 /* get the register name. No name means to use the previous one */
9616 c = *eap->arg;
9617 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9618 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009619 /* Put the register in the typeahead buffer with the "silent" flag. */
9620 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9621 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 else
9626 {
9627 int save_efr = exec_from_reg;
9628
9629 exec_from_reg = TRUE;
9630
9631 /*
9632 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009633 * Continue until the stuff buffer is empty and all added characters
9634 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009636 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9638
9639 exec_from_reg = save_efr;
9640 }
9641}
9642
9643/*
9644 * ":!".
9645 */
9646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009647ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
9649 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9650}
9651
9652/*
9653 * ":undo".
9654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009656ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009658 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009659 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009660 else
9661 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662}
9663
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009664#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009666ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009667{
9668 char_u hash[UNDO_HASH_SIZE];
9669
9670 u_compute_hash(hash);
9671 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9672}
9673
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009675ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009676{
9677 char_u hash[UNDO_HASH_SIZE];
9678
9679 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009680 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009681}
9682#endif
9683
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684/*
9685 * ":redo".
9686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009688ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
9690 u_redo(1);
9691}
9692
9693/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009694 * ":earlier" and ":later".
9695 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009697ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009698{
9699 long count = 0;
9700 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009701 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009702 char_u *p = eap->arg;
9703
9704 if (*p == NUL)
9705 count = 1;
9706 else if (isdigit(*p))
9707 {
9708 count = getdigits(&p);
9709 switch (*p)
9710 {
9711 case 's': ++p; sec = TRUE; break;
9712 case 'm': ++p; sec = TRUE; count *= 60; break;
9713 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009714 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9715 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009716 }
9717 }
9718
9719 if (*p != NUL)
9720 EMSG2(_(e_invarg2), eap->arg);
9721 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009722 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9723 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009724}
9725
9726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 * ":redir": start/stop redirection.
9728 */
9729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009730ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731{
9732 char *mode;
9733 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009734 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735
Bram Moolenaarba768492016-07-08 15:32:54 +02009736#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02009737 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009738 {
Bram Moolenaar79815f12016-07-09 17:07:29 +02009739 EMSG(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009740 return;
9741 }
Bram Moolenaarba768492016-07-08 15:32:54 +02009742#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02009743
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 if (STRICMP(eap->arg, "END") == 0)
9745 close_redir();
9746 else
9747 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009748 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009750 ++arg;
9751 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009753 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 mode = "a";
9755 }
9756 else
9757 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009758 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759
9760 close_redir();
9761
9762 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009763 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 if (fname == NULL)
9765 return;
9766#ifdef FEAT_BROWSE
9767 if (cmdmod.browse)
9768 {
9769 char_u *browseFile;
9770
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009771 browseFile = do_browse(BROWSE_SAVE,
9772 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02009773 fname, NULL, NULL,
9774 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 if (browseFile == NULL)
9776 return; /* operation cancelled */
9777 vim_free(fname);
9778 fname = browseFile;
9779 eap->forceit = TRUE; /* since dialog already asked */
9780 }
9781#endif
9782
9783 redir_fd = open_exfile(fname, eap->forceit, mode);
9784 vim_free(fname);
9785 }
9786#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009787 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 {
9789 /* redirect to a register a-z (resp. A-Z for appending) */
9790 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009791 ++arg;
9792 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009794 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009795 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009797 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009799 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009800 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009801 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009802 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009804 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009805 if (*arg == '>')
9806 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009807 /* Make register empty when not using @A-@Z and the
9808 * command is valid. */
9809 if (*arg == NUL && !isupper(redir_reg))
9810 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009812 }
9813 if (*arg != NUL)
9814 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009815 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009816 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009817 }
9818 }
9819 else if (*arg == '=' && arg[1] == '>')
9820 {
9821 int append;
9822
9823 /* redirect to a variable */
9824 close_redir();
9825 arg += 2;
9826
9827 if (*arg == '>')
9828 {
9829 ++arg;
9830 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 }
9832 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009833 append = FALSE;
9834
9835 if (var_redir_start(skipwhite(arg), append) == OK)
9836 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 }
9838#endif
9839
9840 /* TODO: redirect to a buffer */
9841
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009843 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009845
9846 /* Make sure redirection is not off. Can happen for cmdline completion
9847 * that indirectly invokes a command to catch its output. */
9848 if (redir_fd != NULL
9849#ifdef FEAT_EVAL
9850 || redir_reg || redir_vname
9851#endif
9852 )
9853 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854}
9855
9856/*
9857 * ":redraw": force redraw
9858 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01009859 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009860ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861{
9862 int r = RedrawingDisabled;
9863 int p = p_lz;
9864
9865 RedrawingDisabled = 0;
9866 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01009867 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009869 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870#ifdef FEAT_TITLE
9871 if (need_maketitle)
9872 maketitle();
9873#endif
9874 RedrawingDisabled = r;
9875 p_lz = p;
9876
9877 /* Reset msg_didout, so that a message that's there is overwritten. */
9878 msg_didout = FALSE;
9879 msg_col = 0;
9880
Bram Moolenaar56667a52013-07-17 11:54:28 +02009881 /* No need to wait after an intentional redraw. */
9882 need_wait_return = FALSE;
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 out_flush();
9885}
9886
9887/*
9888 * ":redrawstatus": force redraw of status line(s)
9889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009891ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 int r = RedrawingDisabled;
9894 int p = p_lz;
9895
9896 RedrawingDisabled = 0;
9897 p_lz = FALSE;
9898 if (eap->forceit)
9899 status_redraw_all();
9900 else
9901 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009902 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 RedrawingDisabled = r;
9904 p_lz = p;
9905 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906}
9907
9908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009909close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911 if (redir_fd != NULL)
9912 {
9913 fclose(redir_fd);
9914 redir_fd = NULL;
9915 }
9916#ifdef FEAT_EVAL
9917 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009918 if (redir_vname)
9919 {
9920 var_redir_stop();
9921 redir_vname = 0;
9922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923#endif
9924}
9925
9926#if defined(FEAT_SESSION) && defined(USE_CRNL)
9927# define MKSESSION_NL
9928static int mksession_nl = FALSE; /* use NL only in put_eol() */
9929#endif
9930
9931/*
9932 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9933 */
9934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009935ex_mkrc(
9936 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937{
9938 FILE *fd;
9939 int failed = FALSE;
9940 char_u *fname;
9941#ifdef FEAT_BROWSE
9942 char_u *browseFile = NULL;
9943#endif
9944#ifdef FEAT_SESSION
9945 int view_session = FALSE;
9946 int using_vdir = FALSE; /* using 'viewdir'? */
9947 char_u *viewFile = NULL;
9948 unsigned *flagp;
9949#endif
9950
9951 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9952 {
9953#ifdef FEAT_SESSION
9954 view_session = TRUE;
9955#else
9956 ex_ni(eap);
9957 return;
9958#endif
9959 }
9960
9961#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009962 /* Use the short file name until ":lcd" is used. We also don't use the
9963 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009964 did_lcd = FALSE;
9965
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9967 if (eap->cmdidx == CMD_mkview
9968 && (*eap->arg == NUL
9969 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9970 {
9971 eap->forceit = TRUE;
9972 fname = get_view_file(*eap->arg);
9973 if (fname == NULL)
9974 return;
9975 viewFile = fname;
9976 using_vdir = TRUE;
9977 }
9978 else
9979#endif
9980 if (*eap->arg != NUL)
9981 fname = eap->arg;
9982 else if (eap->cmdidx == CMD_mkvimrc)
9983 fname = (char_u *)VIMRC_FILE;
9984#ifdef FEAT_SESSION
9985 else if (eap->cmdidx == CMD_mksession)
9986 fname = (char_u *)SESSION_FILE;
9987#endif
9988 else
9989 fname = (char_u *)EXRC_FILE;
9990
9991#ifdef FEAT_BROWSE
9992 if (cmdmod.browse)
9993 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009994 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995# ifdef FEAT_SESSION
9996 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9997 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9998# endif
9999 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +020010000 fname, (char_u *)"vim", NULL,
10001 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 if (browseFile == NULL)
10003 goto theend;
10004 fname = browseFile;
10005 eap->forceit = TRUE; /* since dialog already asked */
10006 }
10007#endif
10008
10009#if defined(FEAT_SESSION) && defined(vim_mkdir)
10010 /* When using 'viewdir' may have to create the directory. */
10011 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +000010012 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013#endif
10014
10015 fd = open_exfile(fname, eap->forceit, WRITEBIN);
10016 if (fd != NULL)
10017 {
10018#ifdef FEAT_SESSION
10019 if (eap->cmdidx == CMD_mkview)
10020 flagp = &vop_flags;
10021 else
10022 flagp = &ssop_flags;
10023#endif
10024
10025#ifdef MKSESSION_NL
10026 /* "unix" in 'sessionoptions': use NL line separator */
10027 if (view_session && (*flagp & SSOP_UNIX))
10028 mksession_nl = TRUE;
10029#endif
10030
10031 /* Write the version command for :mkvimrc */
10032 if (eap->cmdidx == CMD_mkvimrc)
10033 (void)put_line(fd, "version 6.0");
10034
10035#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010036 if (eap->cmdidx == CMD_mksession)
10037 {
10038 if (put_line(fd, "let SessionLoad = 1") == FAIL)
10039 failed = TRUE;
10040 }
10041
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 if (eap->cmdidx != CMD_mkview)
10043#endif
10044 {
10045 /* Write setting 'compatible' first, because it has side effects.
10046 * For that same reason only do it when needed. */
10047 if (p_cp)
10048 (void)put_line(fd, "if !&cp | set cp | endif");
10049 else
10050 (void)put_line(fd, "if &cp | set nocp | endif");
10051 }
10052
10053#ifdef FEAT_SESSION
10054 if (!view_session
10055 || (eap->cmdidx == CMD_mksession
10056 && (*flagp & SSOP_OPTIONS)))
10057#endif
10058 failed |= (makemap(fd, NULL) == FAIL
10059 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
10060
10061#ifdef FEAT_SESSION
10062 if (!failed && view_session)
10063 {
10064 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
10065 failed = TRUE;
10066 if (eap->cmdidx == CMD_mksession)
10067 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010068 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069
Bram Moolenaard9462e32011-04-11 21:35:11 +020010070 dirnow = alloc(MAXPATHL);
10071 if (dirnow == NULL)
10072 failed = TRUE;
10073 else
10074 {
10075 /*
10076 * Change to session file's dir.
10077 */
10078 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010080 *dirnow = NUL;
10081 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
10082 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +010010083 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010084 shorten_fnames(TRUE);
10085 }
10086 else if (*dirnow != NUL
10087 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
10088 {
10089 if (mch_chdir((char *)globaldir) == 0)
10090 shorten_fnames(TRUE);
10091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092
Bram Moolenaard9462e32011-04-11 21:35:11 +020010093 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094
Bram Moolenaard9462e32011-04-11 21:35:11 +020010095 /* restore original dir */
10096 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +020010098 {
10099 if (mch_chdir((char *)dirnow) != 0)
10100 EMSG(_(e_prev_dir));
10101 shorten_fnames(TRUE);
10102 }
10103 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 }
10105 }
10106 else
10107 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010108 failed |= (put_view(fd, curwin, !using_vdir, flagp,
10109 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 }
10111 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
10112 == FAIL)
10113 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010114 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
10115 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010116 if (eap->cmdidx == CMD_mksession)
10117 {
10118 if (put_line(fd, "unlet SessionLoad") == FAIL)
10119 failed = TRUE;
10120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 }
10122#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010123 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
10124 failed = TRUE;
10125
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 failed |= fclose(fd);
10127
10128 if (failed)
10129 EMSG(_(e_write));
10130#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
10131 else if (eap->cmdidx == CMD_mksession)
10132 {
10133 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +020010134 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135
Bram Moolenaard9462e32011-04-11 21:35:11 +020010136 tbuf = alloc(MAXPATHL);
10137 if (tbuf != NULL)
10138 {
10139 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
10140 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
10141 vim_free(tbuf);
10142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 }
10144#endif
10145#ifdef MKSESSION_NL
10146 mksession_nl = FALSE;
10147#endif
10148 }
10149
10150#ifdef FEAT_BROWSE
10151theend:
10152 vim_free(browseFile);
10153#endif
10154#ifdef FEAT_SESSION
10155 vim_free(viewFile);
10156#endif
10157}
10158
Bram Moolenaardf177f62005-02-22 08:39:57 +000010159#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
10160 || defined(PROTO)
10161 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +010010162vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +000010163{
10164 if (vim_mkdir(name, prot) != 0)
10165 {
10166 EMSG2(_("E739: Cannot create directory: %s"), name);
10167 return FAIL;
10168 }
10169 return OK;
10170}
10171#endif
10172
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173/*
10174 * Open a file for writing for an Ex command, with some checks.
10175 * Return file descriptor, or NULL on failure.
10176 */
10177 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010178open_exfile(
10179 char_u *fname,
10180 int forceit,
10181 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182{
10183 FILE *fd;
10184
10185#ifdef UNIX
10186 /* with Unix it is possible to open a directory */
10187 if (mch_isdir(fname))
10188 {
10189 EMSG2(_(e_isadir2), fname);
10190 return NULL;
10191 }
10192#endif
10193 if (!forceit && *mode != 'a' && vim_fexists(fname))
10194 {
10195 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
10196 return NULL;
10197 }
10198
10199 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
10200 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
10201
10202 return fd;
10203}
10204
10205/*
10206 * ":mark" and ":k".
10207 */
10208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010209ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210{
10211 pos_T pos;
10212
10213 if (*eap->arg == NUL) /* No argument? */
10214 EMSG(_(e_argreq));
10215 else if (eap->arg[1] != NUL) /* more than one character? */
10216 EMSG(_(e_trailing));
10217 else
10218 {
10219 pos = curwin->w_cursor; /* save curwin->w_cursor */
10220 curwin->w_cursor.lnum = eap->line2;
10221 beginline(BL_WHITE | BL_FIX);
10222 if (setmark(*eap->arg) == FAIL) /* set mark */
10223 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
10224 curwin->w_cursor = pos; /* restore curwin->w_cursor */
10225 }
10226}
10227
10228/*
10229 * Update w_topline, w_leftcol and the cursor position.
10230 */
10231 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010232update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233{
10234 check_cursor(); /* put cursor on valid line */
10235 update_topline();
10236 if (!curwin->w_p_wrap)
10237 validate_cursor();
10238 update_curswant();
10239}
10240
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010242 * Save the current State and go to Normal mode.
10243 * Return TRUE if the typeahead could be saved.
10244 */
10245 int
10246save_current_state(save_state_T *sst)
10247{
10248 sst->save_msg_scroll = msg_scroll;
10249 sst->save_restart_edit = restart_edit;
10250 sst->save_msg_didout = msg_didout;
10251 sst->save_State = State;
10252 sst->save_insertmode = p_im;
10253 sst->save_finish_op = finish_op;
10254 sst->save_opcount = opcount;
10255
10256 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
10257 restart_edit = 0; /* don't go to Insert mode */
10258 p_im = FALSE; /* don't use 'insertmode' */
10259
10260 /*
10261 * Save the current typeahead. This is required to allow using ":normal"
10262 * from an event handler and makes sure we don't hang when the argument
10263 * ends with half a command.
10264 */
10265 save_typeahead(&sst->tabuf);
10266 return sst->tabuf.typebuf_valid;
10267}
10268
10269 void
10270restore_current_state(save_state_T *sst)
10271{
10272 /* Restore the previous typeahead. */
10273 restore_typeahead(&sst->tabuf);
10274
10275 msg_scroll = sst->save_msg_scroll;
10276 restart_edit = sst->save_restart_edit;
10277 p_im = sst->save_insertmode;
10278 finish_op = sst->save_finish_op;
10279 opcount = sst->save_opcount;
10280 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
10281
10282 /* Restore the state (needed when called from a function executed for
10283 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
10284 State = sst->save_State;
10285#ifdef CURSOR_SHAPE
10286 ui_cursor_shape(); /* may show different cursor shape */
10287#endif
10288}
10289
10290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 * ":normal[!] {commands}": Execute normal mode commands.
10292 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +010010293 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010294ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295{
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010296 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297#ifdef FEAT_MBYTE
10298 char_u *arg = NULL;
10299 int l;
10300 char_u *p;
10301#endif
10302
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010303 if (ex_normal_lock > 0)
10304 {
10305 EMSG(_(e_secure));
10306 return;
10307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 if (ex_normal_busy >= p_mmd)
10309 {
10310 EMSG(_("E192: Recursive use of :normal too deep"));
10311 return;
10312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313
10314#ifdef FEAT_MBYTE
10315 /*
10316 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
10317 * this for the K_SPECIAL leading byte, otherwise special keys will not
10318 * work.
10319 */
10320 if (has_mbyte)
10321 {
10322 int len = 0;
10323
10324 /* Count the number of characters to be escaped. */
10325 for (p = eap->arg; *p != NUL; ++p)
10326 {
10327# ifdef FEAT_GUI
10328 if (*p == CSI) /* leadbyte CSI */
10329 len += 2;
10330# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010331 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
10333# ifdef FEAT_GUI
10334 || *p == CSI
10335# endif
10336 )
10337 len += 2;
10338 }
10339 if (len > 0)
10340 {
10341 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
10342 if (arg != NULL)
10343 {
10344 len = 0;
10345 for (p = eap->arg; *p != NUL; ++p)
10346 {
10347 arg[len++] = *p;
10348# ifdef FEAT_GUI
10349 if (*p == CSI)
10350 {
10351 arg[len++] = KS_EXTRA;
10352 arg[len++] = (int)KE_CSI;
10353 }
10354# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010355 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 {
10357 arg[len++] = *++p;
10358 if (*p == K_SPECIAL)
10359 {
10360 arg[len++] = KS_SPECIAL;
10361 arg[len++] = KE_FILLER;
10362 }
10363# ifdef FEAT_GUI
10364 else if (*p == CSI)
10365 {
10366 arg[len++] = KS_EXTRA;
10367 arg[len++] = (int)KE_CSI;
10368 }
10369# endif
10370 }
10371 arg[len] = NUL;
10372 }
10373 }
10374 }
10375 }
10376#endif
10377
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010378 ++ex_normal_busy;
10379 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 {
10381 /*
10382 * Repeat the :normal command for each line in the range. When no
10383 * range given, execute it just once, without positioning the cursor
10384 * first.
10385 */
10386 do
10387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 if (eap->addr_count != 0)
10389 {
10390 curwin->w_cursor.lnum = eap->line1++;
10391 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +020010392 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 }
10394
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010395 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396#ifdef FEAT_MBYTE
10397 arg != NULL ? arg :
10398#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010399 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 }
10401 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
10402 }
10403
10404 /* Might not return to the main loop when in an event handler. */
10405 update_topline_cursor();
10406
Bram Moolenaara21a6a92017-09-23 16:33:50 +020010407 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +010010409#ifdef FEAT_MOUSE
10410 setmouse();
10411#endif
10412#ifdef CURSOR_SHAPE
10413 ui_cursor_shape(); /* may show different cursor shape */
10414#endif
10415
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416#ifdef FEAT_MBYTE
10417 vim_free(arg);
10418#endif
10419}
10420
10421/*
Bram Moolenaar64969662005-12-14 21:59:55 +000010422 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 */
10424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010425ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426{
Bram Moolenaarfd371682005-01-14 21:42:54 +000010427 if (eap->forceit)
10428 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +020010429 /* cursor line can be zero on startup */
10430 if (!curwin->w_cursor.lnum)
10431 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +000010432 coladvance((colnr_T)MAXCOL);
10433 curwin->w_curswant = MAXCOL;
10434 curwin->w_set_curswant = FALSE;
10435 }
10436
Bram Moolenaara40c5002005-01-09 21:16:21 +000010437 /* Ignore the command when already in Insert mode. Inserting an
10438 * expression register that invokes a function can do this. */
10439 if (State & INSERT)
10440 return;
10441
Bram Moolenaar64969662005-12-14 21:59:55 +000010442 if (eap->cmdidx == CMD_startinsert)
10443 restart_edit = 'a';
10444 else if (eap->cmdidx == CMD_startreplace)
10445 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 else
Bram Moolenaar64969662005-12-14 21:59:55 +000010447 restart_edit = 'V';
10448
10449 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010451 if (eap->cmdidx == CMD_startinsert)
10452 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 curwin->w_curswant = 0; /* avoid MAXCOL */
10454 }
10455}
10456
10457/*
10458 * ":stopinsert"
10459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010461ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463 restart_edit = 0;
10464 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010465 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010468/*
10469 * Execute normal mode command "cmd".
10470 * "remap" can be REMAP_NONE or REMAP_YES.
10471 */
10472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010473exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010474{
Bram Moolenaar25281632016-01-21 23:32:32 +010010475 /* Stuff the argument into the typeahead buffer. */
10476 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010477 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +010010478}
Bram Moolenaar25281632016-01-21 23:32:32 +010010479
Bram Moolenaar25281632016-01-21 23:32:32 +010010480/*
10481 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010482 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +010010483 */
10484 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010485exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +010010486{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010487 oparg_T oa;
10488
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010489 clear_oparg(&oa);
10490 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +020010491 while ((!stuff_empty()
10492 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
10493 || (use_vpeekc && vpeekc() != NUL))
10494 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010495 {
10496 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010497#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +020010498 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +020010499 && oa.op_type == OP_NOP && oa.regname == NUL
10500 && !VIsual_active)
10501 {
10502 /* If terminal_loop() returns OK we got a key that is handled
10503 * in Normal model. With FAIL we first need to position the
10504 * cursor and the screen needs to be redrawn. */
10505 if (terminal_loop(TRUE) == OK)
10506 normal_cmd(&oa, TRUE);
10507 }
10508 else
10509#endif
10510 /* execute a Normal mode cmd */
10511 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010512 }
10513}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010514
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515#ifdef FEAT_FIND_ID
10516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010517ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518{
10519 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
10520 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
10521 (linenr_T)1, (linenr_T)MAXLNUM);
10522}
10523
Bram Moolenaar4033c552017-09-16 20:54:51 +020010524#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525/*
10526 * ":psearch"
10527 */
10528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010529ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530{
10531 g_do_tagpreview = p_pvh;
10532 ex_findpat(eap);
10533 g_do_tagpreview = 0;
10534}
10535#endif
10536
10537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010538ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
10540 int whole = TRUE;
10541 long n;
10542 char_u *p;
10543 int action;
10544
10545 switch (cmdnames[eap->cmdidx].cmd_name[2])
10546 {
10547 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
10548 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
10549 action = ACTION_GOTO;
10550 else
10551 action = ACTION_SHOW;
10552 break;
10553 case 'i': /* ":ilist" and ":dlist" */
10554 action = ACTION_SHOW_ALL;
10555 break;
10556 case 'u': /* ":ijump" and ":djump" */
10557 action = ACTION_GOTO;
10558 break;
10559 default: /* ":isplit" and ":dsplit" */
10560 action = ACTION_SPLIT;
10561 break;
10562 }
10563
10564 n = 1;
10565 if (vim_isdigit(*eap->arg)) /* get count */
10566 {
10567 n = getdigits(&eap->arg);
10568 eap->arg = skipwhite(eap->arg);
10569 }
10570 if (*eap->arg == '/') /* Match regexp, not just whole words */
10571 {
10572 whole = FALSE;
10573 ++eap->arg;
10574 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10575 if (*p)
10576 {
10577 *p++ = NUL;
10578 p = skipwhite(p);
10579
10580 /* Check for trailing illegal characters */
10581 if (!ends_excmd(*p))
10582 eap->errmsg = e_trailing;
10583 else
10584 eap->nextcmd = check_nextcmd(p);
10585 }
10586 }
10587 if (!eap->skip)
10588 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10589 whole, !eap->forceit,
10590 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10591 n, action, eap->line1, eap->line2);
10592}
10593#endif
10594
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595
Bram Moolenaar4033c552017-09-16 20:54:51 +020010596#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597/*
10598 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10599 */
10600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010601ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010603 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10605}
10606
10607/*
10608 * ":pedit"
10609 */
10610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010611ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612{
10613 win_T *curwin_save = curwin;
10614
10615 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010616 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010617 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 do_exedit(eap, NULL);
10619 keep_help_flag = FALSE;
10620 if (curwin != curwin_save && win_valid(curwin_save))
10621 {
10622 /* Return cursor to where we were */
10623 validate_cursor();
10624 redraw_later(VALID);
10625 win_enter(curwin_save, TRUE);
10626 }
10627 g_do_tagpreview = 0;
10628}
Bram Moolenaar4033c552017-09-16 20:54:51 +020010629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630
10631/*
10632 * ":stag", ":stselect" and ":stjump".
10633 */
10634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010635ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636{
10637 postponed_split = -1;
10638 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010639 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10641 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010642 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644
10645/*
10646 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10647 */
10648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010649ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650{
10651 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10652}
10653
10654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010655ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656{
10657 int cmd;
10658
10659 switch (name[1])
10660 {
10661 case 'j': cmd = DT_JUMP; /* ":tjump" */
10662 break;
10663 case 's': cmd = DT_SELECT; /* ":tselect" */
10664 break;
10665 case 'p': cmd = DT_PREV; /* ":tprevious" */
10666 break;
10667 case 'N': cmd = DT_PREV; /* ":tNext" */
10668 break;
10669 case 'n': cmd = DT_NEXT; /* ":tnext" */
10670 break;
10671 case 'o': cmd = DT_POP; /* ":pop" */
10672 break;
10673 case 'f': /* ":tfirst" */
10674 case 'r': cmd = DT_FIRST; /* ":trewind" */
10675 break;
10676 case 'l': cmd = DT_LAST; /* ":tlast" */
10677 break;
10678 default: /* ":tag" */
10679#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010680 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 {
Bram Moolenaard4db7712016-11-12 19:16:46 +010010682 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 return;
10684 }
10685#endif
10686 cmd = DT_TAG;
10687 break;
10688 }
10689
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010690 if (name[0] == 'l')
10691 {
10692#ifndef FEAT_QUICKFIX
10693 ex_ni(eap);
10694 return;
10695#else
10696 cmd = DT_LTAG;
10697#endif
10698 }
10699
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10701 eap->forceit, TRUE);
10702}
10703
10704/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010705 * Check "str" for starting with a special cmdline variable.
10706 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10707 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10708 */
10709 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010710find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010711{
10712 int len;
10713 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010714 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010715 "%",
10716#define SPEC_PERC 0
10717 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +020010718#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010719 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010720#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010721 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010722#define SPEC_CCWORD (SPEC_CWORD + 1)
10723 "<cexpr>", /* expr under cursor */
10724#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010725 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010726#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010727 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010728#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010729 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010730#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010731 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010732#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010733 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010734#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010735 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010736#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010737 "<sflnum>", /* script file line number */
10738#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010739#ifdef FEAT_CLIENTSERVER
10740 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010741# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010742#endif
10743 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010744
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010745 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010746 {
10747 len = (int)STRLEN(spec_str[i]);
10748 if (STRNCMP(src, spec_str[i], len) == 0)
10749 {
10750 *usedlen = len;
10751 return i;
10752 }
10753 }
10754 return -1;
10755}
10756
10757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 * Evaluate cmdline variables.
10759 *
10760 * change '%' to curbuf->b_ffname
10761 * '#' to curwin->w_altfile
10762 * '<cword>' to word under the cursor
10763 * '<cWORD>' to WORD under the cursor
10764 * '<cfile>' to path name under the cursor
10765 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010766 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 * '<afile>' to file name for autocommand
10768 * '<abuf>' to buffer number for autocommand
10769 * '<amatch>' to matching name for autocommand
10770 *
10771 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10772 * "" for error without a message) and NULL is returned.
10773 * Returns an allocated string if a valid match was found.
10774 * Returns NULL if no match was found. "usedlen" then still contains the
10775 * number of characters to skip.
10776 */
10777 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010778eval_vars(
10779 char_u *src, /* pointer into commandline */
10780 char_u *srcstart, /* beginning of valid memory for src */
10781 int *usedlen, /* characters after src that are used */
10782 linenr_T *lnump, /* line number for :e command, or NULL */
10783 char_u **errormsg, /* pointer to error message */
10784 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +000010785 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786{
10787 int i;
10788 char_u *s;
10789 char_u *result;
10790 char_u *resultbuf = NULL;
10791 int resultlen;
10792 buf_T *buf;
10793 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10794 int spec_idx;
10795#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +020010796 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 int skip_mod = FALSE;
10798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800
10801 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010802 if (escaped != NULL)
10803 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804
10805 /*
10806 * Check if there is something to do.
10807 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010808 spec_idx = find_cmdline_var(src, usedlen);
10809 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 {
10811 *usedlen = 1;
10812 return NULL;
10813 }
10814
10815 /*
10816 * Skip when preceded with a backslash "\%" and "\#".
10817 * Note: In "\\%" the % is also not recognized!
10818 */
10819 if (src > srcstart && src[-1] == '\\')
10820 {
10821 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010822 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 return NULL;
10824 }
10825
10826 /*
10827 * word or WORD under cursor
10828 */
Bram Moolenaar65f08472017-09-10 18:16:20 +020010829 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
10830 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 {
Bram Moolenaar65f08472017-09-10 18:16:20 +020010832 resultlen = find_ident_under_cursor(&result,
10833 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
10834 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
10835 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 if (resultlen == 0)
10837 {
10838 *errormsg = (char_u *)"";
10839 return NULL;
10840 }
10841 }
10842
10843 /*
10844 * '#': Alternate file name
10845 * '%': Current file name
10846 * File name under the cursor
10847 * File name for autocommand
10848 * and following modifiers
10849 */
10850 else
10851 {
10852 switch (spec_idx)
10853 {
10854 case SPEC_PERC: /* '%': current file */
10855 if (curbuf->b_fname == NULL)
10856 {
10857 result = (char_u *)"";
10858 valid = 0; /* Must have ":p:h" to be valid */
10859 }
10860 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010863#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010864 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010865#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 break;
10868
10869 case SPEC_HASH: /* '#' or "#99": alternate file */
10870 if (src[1] == '#') /* "##": the argument list */
10871 {
10872 result = arg_all();
10873 resultbuf = result;
10874 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010875 if (escaped != NULL)
10876 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877#ifdef FEAT_MODIFY_FNAME
10878 skip_mod = TRUE;
10879#endif
10880 break;
10881 }
10882 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010883 if (*s == '<') /* "#<99" uses v:oldfiles */
10884 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010886 if (s == src + 2 && src[1] == '-')
10887 /* just a minus sign, don't skip over it */
10888 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 *usedlen = (int)(s - src); /* length of what we expand */
10890
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010891 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010893 if (*usedlen < 2)
10894 {
10895 /* Should we give an error message for #<text? */
10896 *usedlen = 1;
10897 return NULL;
10898 }
10899#ifdef FEAT_EVAL
10900 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10901 (long)i);
10902 if (result == NULL)
10903 {
10904 *errormsg = (char_u *)"";
10905 return NULL;
10906 }
10907#else
10908 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 }
10912 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010913 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +020010914 if (i == 0 && src[1] == '<' && *usedlen > 1)
10915 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010916 buf = buflist_findnr(i);
10917 if (buf == NULL)
10918 {
10919 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10920 return NULL;
10921 }
10922 if (lnump != NULL)
10923 *lnump = ECMD_LAST;
10924 if (buf->b_fname == NULL)
10925 {
10926 result = (char_u *)"";
10927 valid = 0; /* Must have ":p:h" to be valid */
10928 }
10929 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010930 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010931 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010932#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010933 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +020010934#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010935 }
Bram Moolenaard812df62008-11-09 12:46:09 +000010936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 break;
10938
10939#ifdef FEAT_SEARCHPATH
10940 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010941 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 if (result == NULL)
10943 {
10944 *errormsg = (char_u *)"";
10945 return NULL;
10946 }
10947 resultbuf = result; /* remember allocated string */
10948 break;
10949#endif
10950
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 case SPEC_AFILE: /* file name for autocommand */
10952 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010953 if (result != NULL && !autocmd_fname_full)
10954 {
10955 /* Still need to turn the fname into a full path. It is
10956 * postponed to avoid a delay when <afile> is not used. */
10957 autocmd_fname_full = TRUE;
10958 result = FullName_save(autocmd_fname, FALSE);
10959 vim_free(autocmd_fname);
10960 autocmd_fname = result;
10961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 if (result == NULL)
10963 {
10964 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10965 return NULL;
10966 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010967 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 break;
10969
10970 case SPEC_ABUF: /* buffer number for autocommand */
10971 if (autocmd_bufnr <= 0)
10972 {
10973 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10974 return NULL;
10975 }
10976 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10977 result = strbuf;
10978 break;
10979
10980 case SPEC_AMATCH: /* match name for autocommand */
10981 result = autocmd_match;
10982 if (result == NULL)
10983 {
10984 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10985 return NULL;
10986 }
10987 break;
10988
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 case SPEC_SFILE: /* file name for ":so" command */
10990 result = sourcing_name;
10991 if (result == NULL)
10992 {
10993 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10994 return NULL;
10995 }
10996 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010997
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010998 case SPEC_SLNUM: /* line in file for ":so" command */
10999 if (sourcing_name == NULL || sourcing_lnum == 0)
11000 {
11001 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
11002 return NULL;
11003 }
11004 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
11005 result = strbuf;
11006 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011007
11008#ifdef FEAT_EVAL
11009 case SPEC_SFLNUM: /* line in script file */
11010 if (current_sctx.sc_lnum + sourcing_lnum == 0)
11011 {
11012 *errormsg = (char_u *)_("E961: no line number to use for \"<sflnum>\"");
11013 return NULL;
11014 }
11015 sprintf((char *)strbuf, "%ld",
11016 (long)(current_sctx.sc_lnum + sourcing_lnum));
11017 result = strbuf;
11018 break;
11019#endif
11020
11021#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011023 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
11024 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 result = strbuf;
11026 break;
11027#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020011028
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020011029 default:
11030 result = (char_u *)""; /* avoid gcc warning */
11031 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
11033
11034 resultlen = (int)STRLEN(result); /* length of new string */
11035 if (src[*usedlen] == '<') /* remove the file name extension */
11036 {
11037 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 resultlen = (int)(s - result);
11040 }
11041#ifdef FEAT_MODIFY_FNAME
11042 else if (!skip_mod)
11043 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020011044 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 &resultlen);
11046 if (result == NULL)
11047 {
11048 *errormsg = (char_u *)"";
11049 return NULL;
11050 }
11051 }
11052#endif
11053 }
11054
11055 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
11056 {
11057 if (valid != VALID_HEAD + VALID_PATH)
11058 /* xgettext:no-c-format */
11059 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
11060 else
11061 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
11062 result = NULL;
11063 }
11064 else
11065 result = vim_strnsave(result, resultlen);
11066 vim_free(resultbuf);
11067 return result;
11068}
11069
11070/*
11071 * Concatenate all files in the argument list, separated by spaces, and return
11072 * it in one allocated string.
11073 * Spaces and backslashes in the file names are escaped with a backslash.
11074 * Returns NULL when out of memory.
11075 */
11076 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011077arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 int len;
11080 int idx;
11081 char_u *retval = NULL;
11082 char_u *p;
11083
11084 /*
11085 * Do this loop two times:
11086 * first time: compute the total length
11087 * second time: concatenate the names
11088 */
11089 for (;;)
11090 {
11091 len = 0;
11092 for (idx = 0; idx < ARGCOUNT; ++idx)
11093 {
11094 p = alist_name(&ARGLIST[idx]);
11095 if (p != NULL)
11096 {
11097 if (len > 0)
11098 {
11099 /* insert a space in between names */
11100 if (retval != NULL)
11101 retval[len] = ' ';
11102 ++len;
11103 }
11104 for ( ; *p != NUL; ++p)
11105 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +020011106 if (*p == ' '
11107#ifndef BACKSLASH_IN_FILENAME
11108 || *p == '\\'
11109#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +020011110 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 {
11112 /* insert a backslash */
11113 if (retval != NULL)
11114 retval[len] = '\\';
11115 ++len;
11116 }
11117 if (retval != NULL)
11118 retval[len] = *p;
11119 ++len;
11120 }
11121 }
11122 }
11123
11124 /* second time: break here */
11125 if (retval != NULL)
11126 {
11127 retval[len] = NUL;
11128 break;
11129 }
11130
11131 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000011132 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 if (retval == NULL)
11134 break;
11135 }
11136
11137 return retval;
11138}
11139
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140/*
11141 * Expand the <sfile> string in "arg".
11142 *
11143 * Returns an allocated string, or NULL for any error.
11144 */
11145 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011146expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147{
11148 char_u *errormsg;
11149 int len;
11150 char_u *result;
11151 char_u *newres;
11152 char_u *repl;
11153 int srclen;
11154 char_u *p;
11155
11156 result = vim_strsave(arg);
11157 if (result == NULL)
11158 return NULL;
11159
11160 for (p = result; *p; )
11161 {
11162 if (STRNCMP(p, "<sfile>", 7) != 0)
11163 ++p;
11164 else
11165 {
11166 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000011167 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 if (errormsg != NULL)
11169 {
11170 if (*errormsg)
11171 emsg(errormsg);
11172 vim_free(result);
11173 return NULL;
11174 }
11175 if (repl == NULL) /* no match (cannot happen) */
11176 {
11177 p += srclen;
11178 continue;
11179 }
11180 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
11181 newres = alloc(len);
11182 if (newres == NULL)
11183 {
11184 vim_free(repl);
11185 vim_free(result);
11186 return NULL;
11187 }
11188 mch_memmove(newres, result, (size_t)(p - result));
11189 STRCPY(newres + (p - result), repl);
11190 len = (int)STRLEN(newres);
11191 STRCAT(newres, p + srclen);
11192 vim_free(repl);
11193 vim_free(result);
11194 result = newres;
11195 p = newres + len; /* continue after the match */
11196 }
11197 }
11198
11199 return result;
11200}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201
11202#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010011203static int ses_winsizes(FILE *fd, int restore_size,
11204 win_T *tab_firstwin);
11205static int ses_win_rec(FILE *fd, frame_T *fr);
11206static frame_T *ses_skipframe(frame_T *fr);
11207static int ses_do_frame(frame_T *fr);
11208static int ses_do_win(win_T *wp);
11209static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
11210static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011211static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212
11213/*
11214 * Write openfile commands for the current buffers to an .exrc file.
11215 * Return FAIL on error, OK otherwise.
11216 */
11217 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011218makeopens(
11219 FILE *fd,
11220 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
11222 buf_T *buf;
11223 int only_save_windows = TRUE;
11224 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 int restore_size = TRUE;
11226 win_T *wp;
11227 char_u *sname;
11228 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011229 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011230 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011231 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011232 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011233 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000011234 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235
11236 if (ssop_flags & SSOP_BUFFERS)
11237 only_save_windows = FALSE; /* Save ALL buffers */
11238
11239 /*
11240 * Begin by setting the this_session variable, and then other
11241 * sessionable variables.
11242 */
11243#ifdef FEAT_EVAL
11244 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
11245 return FAIL;
11246 if (ssop_flags & SSOP_GLOBALS)
11247 if (store_session_globals(fd) == FAIL)
11248 return FAIL;
11249#endif
11250
11251 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011252 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 */
11254 if (put_line(fd, "silent only") == FAIL)
11255 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011256 if ((ssop_flags & SSOP_TABPAGES)
11257 && put_line(fd, "silent tabonly") == FAIL)
11258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259
11260 /*
11261 * Now a :cd command to the session directory or the current directory
11262 */
11263 if (ssop_flags & SSOP_SESDIR)
11264 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011265 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
11266 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 return FAIL;
11268 }
11269 else if (ssop_flags & SSOP_CURDIR)
11270 {
11271 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
11272 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011273 || fputs("cd ", fd) < 0
11274 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
11275 || put_eol(fd) == FAIL)
11276 {
11277 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 vim_free(sname);
11281 }
11282
11283 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011284 * If there is an empty, unnamed buffer we will wipe it out later.
11285 * Remember the buffer number.
11286 */
11287 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
11288 return FAIL;
11289 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
11290 return FAIL;
11291 if (put_line(fd, "endif") == FAIL)
11292 return FAIL;
11293
11294 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 * Now save the current files, current buffer first.
11296 */
11297 if (put_line(fd, "set shortmess=aoO") == FAIL)
11298 return FAIL;
11299
11300 /* Now put the other buffers into the buffer list */
Bram Moolenaar29323592016-07-24 22:04:11 +020011301 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 {
11303 if (!(only_save_windows && buf->b_nwindows == 0)
11304 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011305#ifdef FEAT_TERMINAL
11306 /* skip terminal buffers: finished ones are not useful, others
11307 * will be resurrected and result in a new buffer */
11308 && !bt_terminal(buf)
11309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 && buf->b_fname != NULL
11311 && buf->b_p_bl)
11312 {
11313 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
11314 : buf->b_wininfo->wi_fpos.lnum) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011315 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 return FAIL;
11317 }
11318 }
11319
11320 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011321 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
11323 return FAIL;
11324
11325 if (ssop_flags & SSOP_RESIZE)
11326 {
11327 /* Note: after the restore we still check it worked!*/
11328 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
11329 || put_eol(fd) == FAIL)
11330 return FAIL;
11331 }
11332
11333#ifdef FEAT_GUI
11334 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
11335 {
11336 int x, y;
11337
11338 if (gui_mch_get_winpos(&x, &y) == OK)
11339 {
11340 /* Note: after the restore we still check it worked!*/
11341 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
11342 return FAIL;
11343 }
11344 }
11345#endif
11346
11347 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011348 * When there are two or more tabpages and 'showtabline' is 1 the tabline
11349 * will be displayed when creating the next tab. That resizes the windows
11350 * in the first tab, which may cause problems. Set 'showtabline' to 2
11351 * temporarily to avoid that.
11352 */
11353 if (p_stal == 1 && first_tabpage->tp_next != NULL)
11354 {
11355 if (put_line(fd, "set stal=2") == FAIL)
11356 return FAIL;
11357 restore_stal = TRUE;
11358 }
11359
11360 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011361 * May repeat putting Windows for each tab, when "tabpages" is in
11362 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011363 * Don't use goto_tabpage(), it may change directory and trigger
11364 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011366 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011367 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011368 if ((ssop_flags & SSOP_TABPAGES))
11369 {
11370 int num_tabs;
11371
11372 /*
11373 * Similar to ses_win_rec() below, populate the tab pages first so
11374 * later local options won't be copied to the new tabs.
11375 */
11376 for (tabnr = 1; ; ++tabnr)
11377 {
11378 tabpage_T *tp = find_tabpage(tabnr);
11379
11380 if (tp == NULL) /* done all tab pages */
11381 break;
11382
11383 if (tabnr > 1 && put_line(fd, "tabnew") == FAIL)
11384 return FAIL;
11385 }
11386
11387 num_tabs = tabnr - 1;
11388 if (num_tabs > 1 && (fprintf(fd, "tabnext -%d", num_tabs - 1) < 0
11389 || put_eol(fd) == FAIL))
11390 return FAIL;
11391 }
Bram Moolenaar18144c82006-04-12 21:52:12 +000011392 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011394 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +020011395 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011396
Bram Moolenaar18144c82006-04-12 21:52:12 +000011397 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011399 tabpage_T *tp = find_tabpage(tabnr);
11400
11401 if (tp == NULL)
11402 break; /* done all tab pages */
11403 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011404 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011405 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011406 tab_topframe = topframe;
11407 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011408 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011409 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011410 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011411 tab_topframe = tp->tp_topframe;
11412 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011413 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011414 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416
Bram Moolenaar18144c82006-04-12 21:52:12 +000011417 /*
11418 * Before creating the window layout, try loading one file. If this
11419 * is aborted we don't end up with a number of useless windows.
11420 * This may have side effects! (e.g., compressed or network file).
11421 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011422 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011423 {
11424 if (ses_do_win(wp)
11425 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +020011426 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011427#ifdef FEAT_QUICKFIX
11428 && !bt_nofile(wp->w_buffer)
11429#endif
11430 )
11431 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011432 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
11433 return FAIL;
11434 need_tabnext = FALSE;
11435
11436 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011437 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
11438 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011439 return FAIL;
11440 if (!wp->w_arg_idx_invalid)
11441 edited_win = wp;
11442 break;
11443 }
11444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011446 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011447 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011448 return FAIL;
11449
Bram Moolenaar18144c82006-04-12 21:52:12 +000011450 /*
11451 * Save current window layout.
11452 */
11453 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000011455 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011457 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
11458 return FAIL;
11459 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
11460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461
Bram Moolenaar18144c82006-04-12 21:52:12 +000011462 /*
11463 * Check if window sizes can be restored (no windows omitted).
11464 * Remember the window number of the current window after restoring.
11465 */
11466 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011467 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011468 {
11469 if (ses_do_win(wp))
11470 ++nr;
11471 else
11472 restore_size = FALSE;
11473 if (curwin == wp)
11474 cnr = nr;
11475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476
Bram Moolenaar18144c82006-04-12 21:52:12 +000011477 /* Go to the first window. */
11478 if (put_line(fd, "wincmd t") == FAIL)
11479 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011480
Bram Moolenaar18144c82006-04-12 21:52:12 +000011481 /*
11482 * If more than one window, see if sizes can be restored.
11483 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
11484 * resized when moving between windows.
11485 * Do this before restoring the view, so that the topline and the
11486 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011487 * winminheight and winminwidth need to be set to avoid an error if the
11488 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +000011489 */
Bram Moolenaar19834012018-06-12 17:03:39 +020011490 if (put_line(fd, "set winminheight=0") == FAIL
11491 || put_line(fd, "set winheight=1") == FAIL
11492 || put_line(fd, "set winminwidth=0") == FAIL
11493 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011494 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011495 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011496 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497
Bram Moolenaar18144c82006-04-12 21:52:12 +000011498 /*
11499 * Restore the view of the window (options, file, cursor, etc.).
11500 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011501 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011502 {
11503 if (!ses_do_win(wp))
11504 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011505 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
11506 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011507 return FAIL;
11508 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
11509 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011510 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011511 }
11512
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011513 /* The argument index in the first tab page is zero, need to set it in
11514 * each window. For further tab pages it's the window where we do
11515 * "tabedit". */
11516 cur_arg_idx = next_arg_idx;
11517
Bram Moolenaar18144c82006-04-12 21:52:12 +000011518 /*
11519 * Restore cursor to the current window if it's not the first one.
11520 */
11521 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
11522 || put_eol(fd) == FAIL))
11523 return FAIL;
11524
11525 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000011526 * Restore window sizes again after jumping around in windows, because
11527 * the current window has a minimum size while others may not.
11528 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011529 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000011530 return FAIL;
11531
Bram Moolenaar18144c82006-04-12 21:52:12 +000011532 /* Don't continue in another tab page when doing only the current one
11533 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011534 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000011535 break;
11536 }
11537
11538 if (ssop_flags & SSOP_TABPAGES)
11539 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000011540 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
11541 || put_eol(fd) == FAIL)
11542 return FAIL;
11543 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020011544 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
11545 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000011546
Bram Moolenaar9c102382006-05-03 21:26:49 +000011547 /*
11548 * Wipe out an empty unnamed buffer we started in.
11549 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020011550 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011551 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011552 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000011553 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000011554 return FAIL;
11555 if (put_line(fd, "endif") == FAIL)
11556 return FAIL;
11557 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
11558 return FAIL;
11559
11560 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
11561 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
11562 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
11563 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010011564 /* Re-apply 'winminheight' and 'winminwidth'. */
11565 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
11566 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
11567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568
11569 /*
11570 * Lastly, execute the x.vim file if it exists.
11571 */
11572 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
11573 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000011574 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 || put_line(fd, "endif") == FAIL)
11576 return FAIL;
11577
11578 return OK;
11579}
11580
11581 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011582ses_winsizes(
11583 FILE *fd,
11584 int restore_size,
11585 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586{
11587 int n = 0;
11588 win_T *wp;
11589
11590 if (restore_size && (ssop_flags & SSOP_WINSIZE))
11591 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000011592 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 {
11594 if (!ses_do_win(wp))
11595 continue;
11596 ++n;
11597
11598 /* restore height when not full height */
11599 if (wp->w_height + wp->w_status_height < topframe->fr_height
11600 && (fprintf(fd,
11601 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
11602 n, (long)wp->w_height, Rows / 2, Rows) < 0
11603 || put_eol(fd) == FAIL))
11604 return FAIL;
11605
11606 /* restore width when not full width */
11607 if (wp->w_width < Columns && (fprintf(fd,
11608 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
11609 n, (long)wp->w_width, Columns / 2, Columns) < 0
11610 || put_eol(fd) == FAIL))
11611 return FAIL;
11612 }
11613 }
11614 else
11615 {
11616 /* Just equalise window sizes */
11617 if (put_line(fd, "wincmd =") == FAIL)
11618 return FAIL;
11619 }
11620 return OK;
11621}
11622
11623/*
11624 * Write commands to "fd" to recursively create windows for frame "fr",
11625 * horizontally and vertically split.
11626 * After the commands the last window in the frame is the current window.
11627 * Returns FAIL when writing the commands to "fd" fails.
11628 */
11629 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011630ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631{
11632 frame_T *frc;
11633 int count = 0;
11634
11635 if (fr->fr_layout != FR_LEAF)
11636 {
11637 /* Find first frame that's not skipped and then create a window for
11638 * each following one (first frame is already there). */
11639 frc = ses_skipframe(fr->fr_child);
11640 if (frc != NULL)
11641 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11642 {
11643 /* Make window as big as possible so that we have lots of room
11644 * to split. */
11645 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11646 || put_line(fd, fr->fr_layout == FR_COL
11647 ? "split" : "vsplit") == FAIL)
11648 return FAIL;
11649 ++count;
11650 }
11651
11652 /* Go back to the first window. */
11653 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11654 ? "%dwincmd k" : "%dwincmd h", count) < 0
11655 || put_eol(fd) == FAIL))
11656 return FAIL;
11657
11658 /* Recursively create frames/windows in each window of this column or
11659 * row. */
11660 frc = ses_skipframe(fr->fr_child);
11661 while (frc != NULL)
11662 {
11663 ses_win_rec(fd, frc);
11664 frc = ses_skipframe(frc->fr_next);
11665 /* Go to next window. */
11666 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11667 return FAIL;
11668 }
11669 }
11670 return OK;
11671}
11672
11673/*
11674 * Skip frames that don't contain windows we want to save in the Session.
11675 * Returns NULL when there none.
11676 */
11677 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011678ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679{
11680 frame_T *frc;
11681
11682 for (frc = fr; frc != NULL; frc = frc->fr_next)
11683 if (ses_do_frame(frc))
11684 break;
11685 return frc;
11686}
11687
11688/*
11689 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11690 * the Session.
11691 */
11692 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011693ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694{
11695 frame_T *frc;
11696
11697 if (fr->fr_layout == FR_LEAF)
11698 return ses_do_win(fr->fr_win);
11699 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11700 if (ses_do_frame(frc))
11701 return TRUE;
11702 return FALSE;
11703}
11704
11705/*
11706 * Return non-zero if window "wp" is to be stored in the Session.
11707 */
11708 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011709ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011711#ifdef FEAT_TERMINAL
11712 if (bt_terminal(wp->w_buffer))
11713 return !term_is_finished(wp->w_buffer)
11714 && (ssop_flags & SSOP_TERMINAL)
11715 && term_should_restore(wp->w_buffer);
11716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 if (wp->w_buffer->b_fname == NULL
11718#ifdef FEAT_QUICKFIX
11719 /* When 'buftype' is "nofile" can't restore the window contents. */
11720 || bt_nofile(wp->w_buffer)
11721#endif
11722 )
11723 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020011724 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725 return (ssop_flags & SSOP_HELP);
11726 return TRUE;
11727}
11728
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011729 static int
11730put_view_curpos(FILE *fd, win_T *wp, char *spaces)
11731{
11732 int r;
11733
11734 if (wp->w_curswant == MAXCOL)
11735 r = fprintf(fd, "%snormal! $", spaces);
11736 else
11737 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
11738 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
11739}
11740
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741/*
11742 * Write commands to "fd" to restore the view of a window.
11743 * Caller must make sure 'scrolloff' is zero.
11744 */
11745 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011746put_view(
11747 FILE *fd,
11748 win_T *wp,
11749 int add_edit, /* add ":edit" command to view */
11750 unsigned *flagp, /* vop_flags or ssop_flags */
11751 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011752 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753{
11754 win_T *save_curwin;
11755 int f;
11756 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011757 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758
11759 /* Always restore cursor position for ":mksession". For ":mkview" only
11760 * when 'viewoptions' contains "cursor". */
11761 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11762
11763 /*
11764 * Local argument list.
11765 */
11766 if (wp->w_alist == &global_alist)
11767 {
11768 if (put_line(fd, "argglobal") == FAIL)
11769 return FAIL;
11770 }
11771 else
11772 {
11773 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11774 flagp == &vop_flags
11775 || !(*flagp & SSOP_CURDIR)
11776 || wp->w_localdir != NULL, flagp) == FAIL)
11777 return FAIL;
11778 }
11779
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011780 /* Only when part of a session: restore the argument index. Some
11781 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011782 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011783 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011785 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 || put_eol(fd) == FAIL)
11787 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011788 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 }
11790
11791 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011792 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011794# ifdef FEAT_TERMINAL
11795 if (bt_terminal(wp->w_buffer))
11796 {
11797 if (term_write_session(fd, wp) == FAIL)
11798 return FAIL;
11799 }
11800 else
11801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 /*
11803 * Load the file.
11804 */
11805 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011806# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 )
11810 {
11811 /*
11812 * Editing a file in this buffer: use ":edit file".
11813 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011814 *
11815 * Note, if a buffer for that file already exists, use :badd to
11816 * edit that buffer, to not lose folding information (:edit resets
11817 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 */
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011819 if (fputs("if bufexists('", fd) < 0
11820 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11821 || fputs("') | buffer ", fd) < 0
11822 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11823 || fputs(" | else | edit ", fd) < 0
11824 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
11825 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011826 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 return FAIL;
11828 }
11829 else
11830 {
11831 /* No file in this buffer, just make it empty. */
11832 if (put_line(fd, "enew") == FAIL)
11833 return FAIL;
11834#ifdef FEAT_QUICKFIX
11835 if (wp->w_buffer->b_ffname != NULL)
11836 {
11837 /* The buffer does have a name, but it's not a file name. */
11838 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020011839 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 return FAIL;
11841 }
11842#endif
11843 do_cursor = FALSE;
11844 }
11845 }
11846
11847 /*
11848 * Local mappings and abbreviations.
11849 */
11850 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11851 && makemap(fd, wp->w_buffer) == FAIL)
11852 return FAIL;
11853
11854 /*
11855 * Local options. Need to go to the window temporarily.
11856 * Store only local values when using ":mkview" and when ":mksession" is
11857 * used and 'sessionoptions' doesn't include "options".
11858 * Some folding options are always stored when "folds" is included,
11859 * otherwise the folds would not be restored correctly.
11860 */
11861 save_curwin = curwin;
11862 curwin = wp;
11863 curbuf = curwin->w_buffer;
11864 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11865 f = makeset(fd, OPT_LOCAL,
11866 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11867#ifdef FEAT_FOLDING
11868 else if (*flagp & SSOP_FOLDS)
11869 f = makefoldset(fd);
11870#endif
11871 else
11872 f = OK;
11873 curwin = save_curwin;
11874 curbuf = curwin->w_buffer;
11875 if (f == FAIL)
11876 return FAIL;
11877
11878#ifdef FEAT_FOLDING
11879 /*
11880 * Save Folds when 'buftype' is empty and for help files.
11881 */
11882 if ((*flagp & SSOP_FOLDS)
11883 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020011884 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 {
11886 if (put_folds(fd, wp) == FAIL)
11887 return FAIL;
11888 }
11889#endif
11890
11891 /*
11892 * Set the cursor after creating folds, since that moves the cursor.
11893 */
11894 if (do_cursor)
11895 {
11896
11897 /* Restore the cursor line in the file and relatively in the
11898 * window. Don't use "G", it changes the jumplist. */
11899 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11900 (long)wp->w_cursor.lnum,
11901 (long)(wp->w_cursor.lnum - wp->w_topline),
11902 (long)wp->w_height / 2, (long)wp->w_height) < 0
11903 || put_eol(fd) == FAIL
11904 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11905 || put_line(fd, "exe s:l") == FAIL
11906 || put_line(fd, "normal! zt") == FAIL
11907 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11908 || put_eol(fd) == FAIL)
11909 return FAIL;
11910 /* Restore the cursor column and left offset when not wrapping. */
11911 if (wp->w_cursor.col == 0)
11912 {
11913 if (put_line(fd, "normal! 0") == FAIL)
11914 return FAIL;
11915 }
11916 else
11917 {
11918 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11919 {
11920 if (fprintf(fd,
11921 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011922 (long)wp->w_virtcol + 1,
11923 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 (long)wp->w_width / 2, (long)wp->w_width) < 0
11925 || put_eol(fd) == FAIL
11926 || put_line(fd, "if s:c > 0") == FAIL
11927 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011928 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11929 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 || put_eol(fd) == FAIL
11931 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011932 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 || put_line(fd, "endif") == FAIL)
11934 return FAIL;
11935 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020011936 else if (put_view_curpos(fd, wp, "") == FAIL)
11937 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 }
11939 }
11940
11941 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010011942 * Local directory, if the current flag is not view options or the "curdir"
11943 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010011945 if (wp->w_localdir != NULL
11946 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 {
11948 if (fputs("lcd ", fd) < 0
11949 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11950 || put_eol(fd) == FAIL)
11951 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011952 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 }
11954
11955 return OK;
11956}
11957
11958/*
11959 * Write an argument list to the session file.
11960 * Returns FAIL if writing fails.
11961 */
11962 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011963ses_arglist(
11964 FILE *fd,
11965 char *cmd,
11966 garray_T *gap,
11967 int fullname, /* TRUE: use full path name */
11968 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
11970 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011971 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 char_u *s;
11973
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011974 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11975 return FAIL;
11976 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 return FAIL;
11978 for (i = 0; i < gap->ga_len; ++i)
11979 {
11980 /* NULL file names are skipped (only happens when out of memory). */
11981 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11982 if (s != NULL)
11983 {
11984 if (fullname)
11985 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011986 buf = alloc(MAXPATHL);
11987 if (buf != NULL)
11988 {
11989 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11990 s = buf;
11991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010011993 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011994 || ses_put_fname(fd, s, flagp) == FAIL
11995 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011996 {
11997 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011999 }
12000 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 }
12002 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010012003 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004}
12005
12006/*
12007 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012008 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 * Returns FAIL if writing fails.
12010 */
12011 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012012ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013{
12014 char_u *name;
12015
12016 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012017 * the session file will be sourced.
12018 * Don't do this for ":mkview", we don't know the current directory.
12019 * Don't do this after ":lcd", we don't keep track of what the current
12020 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 if (buf->b_sfname != NULL
12022 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012023 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000012024#ifdef FEAT_AUTOCHDIR
12025 && !p_acd
12026#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000012027 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 name = buf->b_sfname;
12029 else
12030 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020012031 if (ses_put_fname(fd, name, flagp) == FAIL
12032 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 return FAIL;
12034 return OK;
12035}
12036
12037/*
12038 * Write a file name to the session file.
12039 * Takes care of the "slash" option in 'sessionoptions' and escapes special
12040 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012041 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 */
12043 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012044ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045{
12046 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012047 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049
12050 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012051 if (sname == NULL)
12052 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012054 if (*flagp & SSOP_SLASH)
12055 {
12056 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010012057 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012058 if (*p == '\\')
12059 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012061
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020012062 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012063 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020012065 if (p == NULL)
12066 return FAIL;
12067
12068 /* write the result */
12069 if (fputs((char *)p, fd) < 0)
12070 retval = FAIL;
12071
12072 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 return retval;
12074}
12075
12076/*
12077 * ":loadview [nr]"
12078 */
12079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012080ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081{
12082 char_u *fname;
12083
12084 fname = get_view_file(*eap->arg);
12085 if (fname != NULL)
12086 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012087 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 vim_free(fname);
12089 }
12090}
12091
12092/*
12093 * Get the name of the view file for the current buffer.
12094 */
12095 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012096get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097{
12098 int len = 0;
12099 char_u *p, *s;
12100 char_u *retval;
12101 char_u *sname;
12102
12103 if (curbuf->b_ffname == NULL)
12104 {
12105 EMSG(_(e_noname));
12106 return NULL;
12107 }
12108 sname = home_replace_save(NULL, curbuf->b_ffname);
12109 if (sname == NULL)
12110 return NULL;
12111
12112 /*
12113 * We want a file name without separators, because we're not going to make
12114 * a directory.
12115 * "normal" path separator -> "=+"
12116 * "=" -> "=="
12117 * ":" path separator -> "=-"
12118 */
12119 for (p = sname; *p; ++p)
12120 if (*p == '=' || vim_ispathsep(*p))
12121 ++len;
12122 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
12123 if (retval != NULL)
12124 {
12125 STRCPY(retval, p_vdir);
12126 add_pathsep(retval);
12127 s = retval + STRLEN(retval);
12128 for (p = sname; *p; ++p)
12129 {
12130 if (*p == '=')
12131 {
12132 *s++ = '=';
12133 *s++ = '=';
12134 }
12135 else if (vim_ispathsep(*p))
12136 {
12137 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020012138#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 if (*p == ':')
12140 *s++ = '-';
12141 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000012143 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 }
12145 else
12146 *s++ = *p;
12147 }
12148 *s++ = '=';
12149 *s++ = c;
12150 STRCPY(s, ".vim");
12151 }
12152
12153 vim_free(sname);
12154 return retval;
12155}
12156
12157#endif /* FEAT_SESSION */
12158
12159/*
12160 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
12161 * Return FAIL for a write error.
12162 */
12163 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012164put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165{
12166 if (
12167#ifdef USE_CRNL
12168 (
12169# ifdef MKSESSION_NL
12170 !mksession_nl &&
12171# endif
12172 (putc('\r', fd) < 0)) ||
12173#endif
12174 (putc('\n', fd) < 0))
12175 return FAIL;
12176 return OK;
12177}
12178
12179/*
12180 * Write a line to "fd".
12181 * Return FAIL for a write error.
12182 */
12183 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012184put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185{
12186 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
12187 return FAIL;
12188 return OK;
12189}
12190
12191#ifdef FEAT_VIMINFO
12192/*
12193 * ":rviminfo" and ":wviminfo".
12194 */
12195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012196ex_viminfo(
12197 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199 char_u *save_viminfo;
12200
12201 save_viminfo = p_viminfo;
12202 if (*p_viminfo == NUL)
12203 p_viminfo = (char_u *)"'100";
12204 if (eap->cmdidx == CMD_rviminfo)
12205 {
Bram Moolenaard812df62008-11-09 12:46:09 +000012206 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
12207 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 EMSG(_("E195: Cannot open viminfo file for reading"));
12209 }
12210 else
12211 write_viminfo(eap->arg, eap->forceit);
12212 p_viminfo = save_viminfo;
12213}
12214#endif
12215
12216#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012217/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020012218 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000012219 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000012220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012222dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 if (fname == NULL)
12225 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020012226 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227}
12228#endif
12229
12230/*
12231 * ":behave {mswin,xterm}"
12232 */
12233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012234ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235{
12236 if (STRCMP(eap->arg, "mswin") == 0)
12237 {
12238 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
12239 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
12240 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
12241 set_option_value((char_u *)"keymodel", 0L,
12242 (char_u *)"startsel,stopsel", 0);
12243 }
12244 else if (STRCMP(eap->arg, "xterm") == 0)
12245 {
12246 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
12247 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
12248 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
12249 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
12250 }
12251 else
12252 EMSG2(_(e_invarg2), eap->arg);
12253}
12254
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012255#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12256/*
12257 * Function given to ExpandGeneric() to obtain the possible arguments of the
12258 * ":behave {mswin,xterm}" command.
12259 */
12260 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012261get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012262{
12263 if (idx == 0)
12264 return (char_u *)"mswin";
12265 if (idx == 1)
12266 return (char_u *)"xterm";
12267 return NULL;
12268}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020012269
12270/*
12271 * Function given to ExpandGeneric() to obtain the possible arguments of the
12272 * ":messages {clear}" command.
12273 */
12274 char_u *
12275get_messages_arg(expand_T *xp UNUSED, int idx)
12276{
12277 if (idx == 0)
12278 return (char_u *)"clear";
12279 return NULL;
12280}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010012281#endif
12282
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020012283 char_u *
12284get_mapclear_arg(expand_T *xp UNUSED, int idx)
12285{
12286 if (idx == 0)
12287 return (char_u *)"<buffer>";
12288 return NULL;
12289}
12290
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291static int filetype_detect = FALSE;
12292static int filetype_plugin = FALSE;
12293static int filetype_indent = FALSE;
12294
12295/*
12296 * ":filetype [plugin] [indent] {on,off,detect}"
12297 * on: Load the filetype.vim file to install autocommands for file types.
12298 * off: Load the ftoff.vim file to remove all autocommands for file types.
12299 * plugin on: load filetype.vim and ftplugin.vim
12300 * plugin off: load ftplugof.vim
12301 * indent on: load filetype.vim and indent.vim
12302 * indent off: load indoff.vim
12303 */
12304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012305ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306{
12307 char_u *arg = eap->arg;
12308 int plugin = FALSE;
12309 int indent = FALSE;
12310
12311 if (*eap->arg == NUL)
12312 {
12313 /* Print current status. */
12314 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
12315 filetype_detect ? "ON" : "OFF",
12316 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
12317 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
12318 return;
12319 }
12320
12321 /* Accept "plugin" and "indent" in any order. */
12322 for (;;)
12323 {
12324 if (STRNCMP(arg, "plugin", 6) == 0)
12325 {
12326 plugin = TRUE;
12327 arg = skipwhite(arg + 6);
12328 continue;
12329 }
12330 if (STRNCMP(arg, "indent", 6) == 0)
12331 {
12332 indent = TRUE;
12333 arg = skipwhite(arg + 6);
12334 continue;
12335 }
12336 break;
12337 }
12338 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
12339 {
12340 if (*arg == 'o' || !filetype_detect)
12341 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012342 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 filetype_detect = TRUE;
12344 if (plugin)
12345 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012346 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 filetype_plugin = TRUE;
12348 }
12349 if (indent)
12350 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012351 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 filetype_indent = TRUE;
12353 }
12354 }
12355 if (*arg == 'd')
12356 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020012357 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000012358 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 }
12360 }
12361 else if (STRCMP(arg, "off") == 0)
12362 {
12363 if (plugin || indent)
12364 {
12365 if (plugin)
12366 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012367 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 filetype_plugin = FALSE;
12369 }
12370 if (indent)
12371 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012372 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 filetype_indent = FALSE;
12374 }
12375 }
12376 else
12377 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010012378 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 filetype_detect = FALSE;
12380 }
12381 }
12382 else
12383 EMSG2(_(e_invarg2), arg);
12384}
12385
12386/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020012387 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 */
12389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012390ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391{
12392 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020012393 {
12394 char_u *arg = eap->arg;
12395
12396 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12397 arg += 9;
12398
12399 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12400 if (arg != eap->arg)
12401 did_filetype = FALSE;
12402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403}
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012406ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407{
12408#ifdef FEAT_DIGRAPHS
12409 if (*eap->arg != NUL)
12410 putdigraph(eap->arg);
12411 else
12412 listdigraphs();
12413#else
12414 EMSG(_("E196: No digraphs in this version"));
12415#endif
12416}
12417
12418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012419ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420{
12421 int flags = 0;
12422
12423 if (eap->cmdidx == CMD_setlocal)
12424 flags = OPT_LOCAL;
12425 else if (eap->cmdidx == CMD_setglobal)
12426 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010012427#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 if (cmdmod.browse && flags == 0)
12429 ex_options(eap);
12430 else
12431#endif
12432 (void)do_set(eap->arg, flags);
12433}
12434
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012435#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
12436 void
12437set_no_hlsearch(int flag)
12438{
12439 no_hlsearch = flag;
12440# ifdef FEAT_EVAL
12441 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
12442# endif
12443}
12444
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445/*
12446 * ":nohlsearch"
12447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012449ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020012451 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000012452 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453}
12454
12455/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012456 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 * Sets nextcmd to the start of the next command, if any. Also called when
12458 * skipping commands to find the next command.
12459 */
12460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012461ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462{
12463 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000012464 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 char_u *end;
12466 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012467 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012468
12469 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012470 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000012471 else
12472 {
12473 EMSG(e_invcmd);
12474 return;
12475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476
12477 /* First clear any old pattern. */
12478 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012479 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480
12481 if (ends_excmd(*eap->arg))
12482 end = eap->arg;
12483 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010012484 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 end = eap->arg + 4;
12486 else
12487 {
12488 p = skiptowhite(eap->arg);
12489 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012490 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 p = skipwhite(p);
12492 if (*p == NUL)
12493 {
12494 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012495 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496 EMSG2(_(e_invarg2), eap->arg);
12497 return;
12498 }
12499 end = skip_regexp(p + 1, *p, TRUE, NULL);
12500 if (!eap->skip)
12501 {
12502 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
12503 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012504 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505 eap->errmsg = e_trailing;
12506 return;
12507 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012508 if (*end != *p)
12509 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010012510 vim_free(g);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000012511 EMSG2(_(e_invarg2), p);
12512 return;
12513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514
12515 c = *end;
12516 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020012517 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012518 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012519 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520 }
12521 }
12522 eap->nextcmd = find_nextcmd(end);
12523}
12524#endif
12525
12526#ifdef FEAT_CRYPT
12527/*
12528 * ":X": Get crypt key
12529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012531ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010012533 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020012534 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535}
12536#endif
12537
12538#ifdef FEAT_FOLDING
12539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012540ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541{
12542 if (foldManualAllowed(TRUE))
12543 foldCreate(eap->line1, eap->line2);
12544}
12545
12546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012547ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548{
12549 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
12550 eap->forceit, FALSE);
12551}
12552
12553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010012554ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555{
12556 linenr_T lnum;
12557
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012558#ifdef FEAT_CLIPBOARD
12559 start_global_changes();
12560#endif
12561
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 /* First set the marks for all lines closed/open. */
12563 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
12564 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
12565 ml_setmarked(lnum);
12566
12567 /* Execute the command on the marked lines. */
12568 global_exe(eap->arg);
12569 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020012570#ifdef FEAT_CLIPBOARD
12571 end_global_changes();
12572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573}
12574#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012575
Bram Moolenaar39665952018-08-15 20:59:48 +020012576#ifdef FEAT_QUICKFIX
12577/*
12578 * Returns TRUE if the supplied Ex cmdidx is for a location list command
12579 * instead of a quickfix command.
12580 */
12581 int
12582is_loclist_cmd(int cmdidx)
12583{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020012584 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020012585 return FALSE;
12586 return cmdnames[cmdidx].cmd_name[0] == 'l';
12587}
12588#endif
12589
Bram Moolenaarf5291f32017-09-14 22:55:37 +020012590# if defined(FEAT_TIMERS) || defined(PROTO)
12591 int
12592get_pressedreturn(void)
12593{
12594 return ex_pressedreturn;
12595}
12596
12597 void
12598set_pressedreturn(int val)
12599{
12600 ex_pressedreturn = val;
12601}
12602#endif