blob: 58ea7c86388fb9be22095f3914ba348644b4b976 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +010023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020028static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010029static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifndef FEAT_MENU
32# define ex_emenu ex_ni
33# define ex_menu ex_ni
34# define ex_menutranslate ex_ni
35#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void ex_autocmd(exarg_T *eap);
37static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static void ex_bunload(exarg_T *eap);
39static void ex_buffer(exarg_T *eap);
40static void ex_bmodified(exarg_T *eap);
41static void ex_bnext(exarg_T *eap);
42static void ex_bprevious(exarg_T *eap);
43static void ex_brewind(exarg_T *eap);
44static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifndef FEAT_QUICKFIX
48# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000049# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# define ex_cc ex_ni
51# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020052# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define ex_cfile ex_ni
54# define qf_list ex_ni
55# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020056# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000058# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020060#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define ex_cclose ex_ni
62# define ex_copen ex_ni
63# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020064# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000066#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
67# define ex_cexpr ex_ni
68#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020072#if !defined(FEAT_PERL) \
73 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
74 || !defined(FEAT_TCL) \
75 || !defined(FEAT_RUBY) \
76 || !defined(FEAT_LUA) \
77 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000078# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010081static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010082static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000083#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000085#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
87static void ex_highlight(exarg_T *eap);
88static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void ex_cquit(exarg_T *eap);
90static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static void ex_close(exarg_T *eap);
92static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
93static void ex_only(exarg_T *eap);
94static void ex_resize(exarg_T *eap);
95static void ex_stag(exarg_T *eap);
96static void ex_tabclose(exarg_T *eap);
97static void ex_tabonly(exarg_T *eap);
98static void ex_tabnext(exarg_T *eap);
99static void ex_tabmove(exarg_T *eap);
100static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200101#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100102static void ex_pclose(exarg_T *eap);
103static void ex_ptag(exarg_T *eap);
104static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#else
106# define ex_pclose ex_ni
107# define ex_ptag ex_ni
108# define ex_pedit ex_ni
109#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_hide(exarg_T *eap);
111static void ex_stop(exarg_T *eap);
112static void ex_exit(exarg_T *eap);
113static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#else
117# define ex_goto ex_ni
118#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void ex_shell(exarg_T *eap);
120static void ex_preserve(exarg_T *eap);
121static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_mode(exarg_T *eap);
123static void ex_wrongmodifier(exarg_T *eap);
124static void ex_find(exarg_T *eap);
125static void ex_open(exarg_T *eap);
126static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifndef FEAT_GUI
128# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100131#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#else
134# define ex_tearoff ex_ni
135#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100136#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
137 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100138static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#else
140# define ex_popup ex_ni
141#endif
142#ifndef FEAT_GUI_MSWIN
143# define ex_simalt ex_ni
144#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000145#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146# define gui_mch_find_dialog ex_ni
147# define gui_mch_replace_dialog ex_ni
148#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000149#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# define ex_helpfind ex_ni
151#endif
152#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100153# define ex_cscope ex_ni
154# define ex_scscope ex_ni
155# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157#ifndef FEAT_SYN_HL
158# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200159# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000160#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200161#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200162# define ex_syntime ex_ni
163#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000164#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000165# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000166# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000167# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000168# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000169# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000170#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200171#ifndef FEAT_PERSISTENT_UNDO
172# define ex_rundo ex_ni
173# define ex_wundo ex_ni
174#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#ifndef FEAT_LUA
176# define ex_lua ex_script_ni
177# define ex_luado ex_ni
178# define ex_luafile ex_ni
179#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000180#ifndef FEAT_MZSCHEME
181# define ex_mzscheme ex_script_ni
182# define ex_mzfile ex_ni
183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#ifndef FEAT_PERL
185# define ex_perl ex_script_ni
186# define ex_perldo ex_ni
187#endif
188#ifndef FEAT_PYTHON
189# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200190# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191# define ex_pyfile ex_ni
192#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200193#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200194# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200195# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196# define ex_py3file ex_ni
197#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100198#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
199# define ex_pyx ex_script_ni
200# define ex_pyxdo ex_ni
201# define ex_pyxfile ex_ni
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifndef FEAT_TCL
204# define ex_tcl ex_script_ni
205# define ex_tcldo ex_ni
206# define ex_tclfile ex_ni
207#endif
208#ifndef FEAT_RUBY
209# define ex_ruby ex_script_ni
210# define ex_rubydo ex_ni
211# define ex_rubyfile ex_ni
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifndef FEAT_KEYMAP
214# define ex_loadkeymap ex_ni
215#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void ex_swapname(exarg_T *eap);
217static void ex_syncbind(exarg_T *eap);
218static void ex_read(exarg_T *eap);
219static void ex_pwd(exarg_T *eap);
220static void ex_equal(exarg_T *eap);
221static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000224#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#else
227# define ex_winpos ex_ni
228#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_operators(exarg_T *eap);
230static void ex_put(exarg_T *eap);
231static void ex_copymove(exarg_T *eap);
232static void ex_submagic(exarg_T *eap);
233static void ex_join(exarg_T *eap);
234static void ex_at(exarg_T *eap);
235static void ex_bang(exarg_T *eap);
236static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200237#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_wundo(exarg_T *eap);
239static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_redo(exarg_T *eap);
242static void ex_later(exarg_T *eap);
243static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100245static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_startinsert(exarg_T *eap);
249static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_checkpath(exarg_T *eap);
252static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#else
254# define ex_findpat ex_ni
255# define ex_checkpath ex_ni
256#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200257#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#else
260# define ex_psearch ex_ni
261#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_tag(exarg_T *eap);
263static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200265# define ex_break ex_ni
266# define ex_breakadd ex_ni
267# define ex_breakdel ex_ni
268# define ex_breaklist ex_ni
269# define ex_call ex_ni
270# define ex_catch ex_ni
271# define ex_compiler ex_ni
272# define ex_const ex_ni
273# define ex_continue ex_ni
274# define ex_debug ex_ni
275# define ex_debuggreedy ex_ni
276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100373#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 char_u *line; // command line
392 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100402 garray_T *lines_gap; // growarray with line info
403 int current_line; // last read line from growarray
404 int repeating; // TRUE when looping a second time
405 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100439 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // When using ":global /pat/ visual" and then "Q" we return to continue
486 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 ++RedrawingDisabled; // don't redisplay the window
492 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // go up one line, to overwrite the ":<CR>" line, so the
533 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200563 * Print the executed command for when 'verbose' is set.
564 * When "lnum" is 0 only print the command.
565 */
566 static void
567msg_verbose_cmd(linenr_T lnum, char_u *cmd)
568{
569 ++no_wait_return;
570 verbose_enter_scroll();
571
572 if (lnum == 0)
573 smsg(_("Executing: %s"), cmd);
574 else
575 smsg(_("line %ld: %s"), (long)lnum, cmd);
576 if (msg_silent == 0)
577 msg_puts("\n"); // don't overwrite this
578
579 verbose_leave_scroll();
580 --no_wait_return;
581}
582
583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200615 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 char_u *next_cmdline; // next cmd to execute
620 char_u *cmdline_copy = NULL; // copy of cmd line
621 int used_getline = FALSE; // used "fgetline" to obtain command
622 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 int count = 0; // line number count
625 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int retval = OK;
627#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100628 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 garray_T lines_ga; // keep lines for ":while"/":for"
630 int current_line = 0; // active line in lines_ga
631 char_u *fname = NULL; // function or script name
632 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
633 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
634 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 int initial_trylevel;
636 struct msglist **saved_msg_list = NULL;
637 struct msglist *private_msg_list;
638
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200640 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000642 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000644 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100646# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647# define cmd_cookie cookie
648#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100649 static int call_depth = 0; // recursiveness
Bram Moolenaare31ee862020-01-07 20:59:34 +0100650 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
652#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100653 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
654 // location for storing error messages to be converted to an exception.
655 // This ensures that the do_errthrow() call in do_one_cmd() does not
656 // combine the messages stored by an earlier invocation of do_one_cmd()
657 // with the command name of the later one. This would happen when
658 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 saved_msg_list = msg_list;
660 msg_list = &private_msg_list;
661 private_msg_list = NULL;
662#endif
663
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100664 // It's possible to create an endless loop with ":execute", catch that
665 // here. The value of 200 allows nested function calls, ":source", etc.
666 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100667 if (call_depth >= 200
668#ifdef FEAT_EVAL
669 && call_depth >= p_mfd
670#endif
671 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100673 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100675 // When converting to an exception, we do not include the command name
676 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100677 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 msg_list = saved_msg_list;
679#endif
680 return FAIL;
681 }
682 ++call_depth;
683
684#ifdef FEAT_EVAL
685 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000686 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 cstack.cs_trylevel = 0;
688 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000689 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
691
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100692 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100694 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000696 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 ++ex_nesting_level;
698
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // Get the function or script name and the address where the next breakpoint
700 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 fname = func_name(real_cookie);
704 breakpoint = func_breakpoint(real_cookie);
705 dbg_tick = func_dbg_tick(real_cookie);
706 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100707 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100709 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 breakpoint = source_breakpoint(real_cookie);
711 dbg_tick = source_dbg_tick(real_cookie);
712 }
713
714 /*
715 * Initialize "force_abort" and "suppress_errthrow" at the top level.
716 */
717 if (!recursive)
718 {
719 force_abort = FALSE;
720 suppress_errthrow = FALSE;
721 }
722
723 /*
724 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000725 * exception handling (used when debugging). Otherwise clear it to avoid
726 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000728 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000729 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200731 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733 initial_trylevel = trylevel;
734
735 /*
736 * "did_throw" will be set to TRUE when an exception is being thrown.
737 */
738 did_throw = FALSE;
739#endif
740 /*
741 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000742 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * If force_abort is set, we cancel everything.
744 */
745 did_emsg = FALSE;
746
747 /*
748 * KeyTyped is only set when calling vgetc(). Reset it here when not
749 * calling vgetc() (sourced command lines).
750 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751 if (!(flags & DOCMD_KEYTYPED)
752 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 KeyTyped = FALSE;
754
755 /*
756 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000757 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 * - for multiple commands on one line, separated with '|'
759 * - when repeating until there are no more lines (for ":source")
760 */
761 next_cmdline = cmdline;
762 do
763 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000764#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766#endif
767
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100768 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (next_cmdline == NULL
770#ifdef FEAT_EVAL
771 && !force_abort
772 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
775 )
776 did_emsg = FALSE;
777
778 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000779 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100780 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 * 3. If a line is given: Make a copy, so we can mess with it.
782 */
783
784#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100785 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000786 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100788 // Each '|' separated command is stored separately in lines_ga, to
789 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100790 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 // Check if a function has returned or, unless it has an unclosed
793 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000797 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798 func_line_end(real_cookie);
799# endif
800 if (func_has_ended(real_cookie))
801 {
802 retval = FAIL;
803 break;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000807 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100808 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809 script_line_end();
810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100812 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
815 retval = FAIL;
816 break;
817 }
818
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100819 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (breakpoint != NULL && dbg_tick != NULL
821 && *dbg_tick != debug_tick)
822 {
823 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100825 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 *dbg_tick = debug_tick;
827 }
828
829 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100830 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100834 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100837 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100840 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 *dbg_tick = debug_tick;
842 }
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 {
846 if (getline_is_func)
847 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100848 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 script_line_start();
850 }
851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000854 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // Inside a while/for loop we need to store the lines and use them
857 // again. Pass a different "fgetline" function to do_one_cmd()
858 // below, so that it stores lines in or reads them from
859 // "lines_ga". Makes it possible to define a function inside a
860 // while/for loop.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000861 cmd_getline = get_loop_line;
862 cmd_cookie = (void *)&cmd_loop_cookie;
863 cmd_loop_cookie.lines_gap = &lines_ga;
864 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000866 cmd_loop_cookie.cookie = cookie;
867 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869 else
870 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 cmd_cookie = cookie;
873 }
874#endif
875
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100876 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (next_cmdline == NULL)
878 {
879 /*
880 * Need to set msg_didout for the first line after an ":if",
881 * otherwise the ":if" will be overwritten.
882 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_EVAL
887 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
888#else
889 0
890#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200891 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 // Don't call wait_return for aborted command line. The NULL
894 // returned for the end of a sourced file or executed function
895 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (KeyTyped && !(flags & DOCMD_REPEAT))
897 need_wait_return = FALSE;
898 retval = FAIL;
899 break;
900 }
901 used_getline = TRUE;
902
903 /*
904 * Keep the first typed line. Clear it when more lines are typed.
905 */
906 if (flags & DOCMD_KEEPLINE)
907 {
908 vim_free(repeat_cmdline);
909 if (count == 0)
910 repeat_cmdline = vim_strsave(next_cmdline);
911 else
912 repeat_cmdline = NULL;
913 }
914 }
915
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100916 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 else if (cmdline_copy == NULL)
918 {
919 next_cmdline = vim_strsave(next_cmdline);
920 if (next_cmdline == NULL)
921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 retval = FAIL;
924 break;
925 }
926 }
927 cmdline_copy = next_cmdline;
928
929#ifdef FEAT_EVAL
930 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000931 * Save the current line when inside a ":while" or ":for", and when
932 * the command looks like a ":while" or ":for", because we may need it
933 * later. When there is a '|' and another command, it is stored
934 * separately, because we need to be able to jump back to it from an
935 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 if (current_line == lines_ga.ga_len
938 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000940 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
942 retval = FAIL;
943 break;
944 }
945 }
946 did_endif = FALSE;
947#endif
948
949 if (count++ == 0)
950 {
951 /*
952 * All output from the commands is put below each other, without
953 * waiting for a return. Don't do this when executing commands
954 * from a script or when being called recursive (e.g. for ":e
955 * +command file").
956 */
957 if (!(flags & DOCMD_NOWAIT) && !recursive)
958 {
959 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100960 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100962 msg_scroll = TRUE; // put messages below each other
963 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 ++RedrawingDisabled;
965 did_inc = TRUE;
966 }
967 }
968
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100969 if (p_verbose >= 15 && SOURCING_NAME != NULL)
970 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 /*
973 * 2. Execute one '|' separated command.
974 * do_one_cmd() will return NULL if there is no trailing '|'.
975 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
976 */
977 ++recursive;
978 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
979#ifdef FEAT_EVAL
980 &cstack,
981#endif
982 cmd_getline, cmd_cookie);
983 --recursive;
984
985#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000986 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100987 // Use "current_line" from "cmd_loop_cookie", it may have been
988 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#endif
991
992 if (next_cmdline == NULL)
993 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100994 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 /*
997 * If the command was typed, remember it for the ':' register.
998 * Do this AFTER executing the command to make :@: work.
999 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001000 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 && new_last_cmdline != NULL)
1002 {
1003 vim_free(last_cmdline);
1004 last_cmdline = new_last_cmdline;
1005 new_last_cmdline = NULL;
1006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008 else
1009 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 // need to copy the command after the '|' to cmdline_copy, for the
1011 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001012 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 next_cmdline = cmdline_copy;
1014 }
1015
1016
1017#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001018 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001020 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 && !func_has_abort(real_cookie))
1022 did_emsg = FALSE;
1023
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001024 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 {
1026 ++current_line;
1027
1028 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001029 * An ":endwhile", ":endfor" and ":continue" is handled here.
1030 * If we were executing commands, jump back to the ":while" or
1031 * ":for".
1032 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001038 // Jump back to the matching ":while" or ":for". Be careful
1039 // not to use a cs_line[] from an entry that isn't a ":while"
1040 // or ":for": It would make "current_line" invalid and can
1041 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (!did_emsg && !got_int && !did_throw
1043 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 && (cstack.cs_flags[cstack.cs_idx]
1045 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 && cstack.cs_line[cstack.cs_idx] >= 0
1047 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1048 {
1049 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001050 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001052 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001054 // Check for the next breakpoint at or after the ":while"
1055 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (breakpoint != NULL)
1057 {
1058 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001059 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 fname,
1061 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1062 *dbg_tick = debug_tick;
1063 }
1064 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001069 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1070 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
1072 }
1073
1074 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1081 }
1082 }
1083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001085 if (breakpoint != NULL && has_watchexpr())
1086 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001087 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001088 *dbg_tick = debug_tick;
1089 }
1090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 /*
1092 * When not inside any ":while" loop, clear remembered lines.
1093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
1096 if (lines_ga.ga_len > 0)
1097 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001098 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1100 free_cmdlines(&lines_ga);
1101 }
1102 current_line = 0;
1103 }
1104
1105 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1107 * being restored at the ":endtry". Reset them here and set the
1108 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1109 * This includes the case where a missing ":endif", ":endwhile" or
1110 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 cstack.cs_lflags &= ~CSL_HAD_FINA;
1115 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1116 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 did_throw ? (void *)current_exception : NULL);
1118 did_emsg = got_int = did_throw = FALSE;
1119 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1120 }
1121
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001122 // Update global "trylevel" for recursive calls to do_cmdline() from
1123 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 trylevel = initial_trylevel + cstack.cs_trylevel;
1125
1126 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001127 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 * files) is aborted because of an error, an interrupt, or an uncaught
1129 * exception, cancel everything. If it is left normally, reset
1130 * force_abort to get the non-EH compatible abortion behavior for
1131 * the rest of the script.
1132 */
1133 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1134 force_abort = FALSE;
1135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001136 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001138#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
1140 }
1141 /*
1142 * Continue executing command lines when:
1143 * - no CTRL-C typed, no aborting error, no exception thrown or try
1144 * conditionals need to be checked for executing finally clauses or
1145 * catching an interrupt exception
1146 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 * looping for ":source" command or function call.
1149 */
1150 while (!((got_int
1151#ifdef FEAT_EVAL
1152 || (did_emsg && force_abort) || did_throw
1153#endif
1154 )
1155#ifdef FEAT_EVAL
1156 && cstack.cs_trylevel == 0
1157#endif
1158 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001159 && !(did_emsg
1160#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Keep going when inside try/catch, so that the error can be
1162 // deal with, except when it is a syntax error, it may cause
1163 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001164 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1165#endif
1166 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001167 && (getline_equal(fgetline, cookie, getexmodeline)
1168 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 && (next_cmdline != NULL
1170#ifdef FEAT_EVAL
1171 || cstack.cs_idx >= 0
1172#endif
1173 || (flags & DOCMD_REPEAT)));
1174
1175 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001176 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#ifdef FEAT_EVAL
1178 free_cmdlines(&lines_ga);
1179 ga_clear(&lines_ga);
1180
1181 if (cstack.cs_idx >= 0)
1182 {
1183 /*
1184 * If a sourced file or executed function ran to its end, report the
1185 * unclosed conditional.
1186 */
1187 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001188 && ((getline_equal(fgetline, cookie, getsourceline)
1189 && !source_finished(fgetline, cookie))
1190 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 && !func_has_ended(real_cookie))))
1192 {
1193 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202
1203 /*
1204 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1205 * ":endtry" in a sourced file or executed function. If the try
1206 * conditional is in its finally clause, ignore anything pending.
1207 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001208 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
1210 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 {
1212 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1213
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001214 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001215 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1217 &cstack.cs_looplevel);
1218 }
1219 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 trylevel = initial_trylevel;
1221 }
1222
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001223 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1224 // lack was reported above and the error message is to be converted to an
1225 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001226 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 ? (char_u *)"endfunction" : (char_u *)NULL);
1228
1229 if (trylevel == 0)
1230 {
1231 /*
1232 * When an exception is being thrown out of the outermost try
1233 * conditional, discard the uncaught exception, disable the conversion
1234 * of interrupts or errors to exceptions, and ensure that no more
1235 * commands are executed.
1236 */
1237 if (did_throw)
1238 {
1239 void *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 struct msglist *messages = NULL, *next;
1241
1242 /*
1243 * If the uncaught exception is a user exception, report it as an
1244 * error. If it is an error exception, display the saved error
1245 * message now. For an interrupt exception, do nothing; the
1246 * interrupt message is given elsewhere.
1247 */
1248 switch (current_exception->type)
1249 {
1250 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001251 vim_snprintf((char *)IObuff, IOSIZE,
1252 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 current_exception->value);
1254 p = vim_strsave(IObuff);
1255 break;
1256 case ET_ERROR:
1257 messages = current_exception->messages;
1258 current_exception->messages = NULL;
1259 break;
1260 case ET_INTERRUPT:
1261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001264 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1265 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001266 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 current_exception->throw_name = NULL;
1268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001269 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 suppress_errthrow = TRUE;
1271 force_abort = TRUE;
1272
1273 if (messages != NULL)
1274 {
1275 do
1276 {
1277 next = messages->next;
1278 emsg(messages->msg);
1279 vim_free(messages->msg);
1280 vim_free(messages);
1281 messages = next;
1282 }
1283 while (messages != NULL);
1284 }
1285 else if (p != NULL)
1286 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001287 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 vim_free(p);
1289 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001290 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001291 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001292 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294
1295 /*
1296 * On an interrupt or an aborting error not converted to an exception,
1297 * disable the conversion of errors to exceptions. (Interrupts are not
1298 * converted any more, here.) This enables also the interrupt message
1299 * when force_abort is set and did_emsg unset in case of an interrupt
1300 * from a finally clause after an error.
1301 */
1302 else if (got_int || (did_emsg && force_abort))
1303 suppress_errthrow = TRUE;
1304 }
1305
1306 /*
1307 * The current cstack will be freed when do_cmdline() returns. An uncaught
1308 * exception will have to be rethrown in the previous cstack. If a function
1309 * has just returned or a script file was just finished and the previous
1310 * cstack belongs to the same function or, respectively, script file, it
1311 * will have to be checked for finally clauses to be executed due to the
1312 * ":return" or ":finish". This is done in do_one_cmd().
1313 */
1314 if (did_throw)
1315 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001318 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && ex_nesting_level > func_level(real_cookie) + 1))
1320 {
1321 if (!did_throw)
1322 check_cstack = TRUE;
1323 }
1324 else
1325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001326 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 --ex_nesting_level;
1329 /*
1330 * Go to debug mode when returning from a function in which we are
1331 * single-stepping.
1332 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if ((getline_equal(fgetline, cookie, getsourceline)
1334 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001336 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 ? (char_u *)_("End of sourced file")
1338 : (char_u *)_("End of function"));
1339 }
1340
1341 /*
1342 * Restore the exception environment (done after returning from the
1343 * debugger).
1344 */
1345 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001346 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 msg_list = saved_msg_list;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001349#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 /*
1352 * If there was too much output to fit on the command line, ask the user to
1353 * hit return before redrawing the screen. With the ":global" command we do
1354 * this only once after the command is finished.
1355 */
1356 if (did_inc)
1357 {
1358 --RedrawingDisabled;
1359 --no_wait_return;
1360 msg_scroll = FALSE;
1361
1362 /*
1363 * When just finished an ":if"-":else" which was typed, no need to
1364 * wait for hit-return. Also for an error situation.
1365 */
1366 if (retval == FAIL
1367#ifdef FEAT_EVAL
1368 || (did_endif && KeyTyped && !did_emsg)
1369#endif
1370 )
1371 {
1372 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001373 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375 else if (need_wait_return)
1376 {
1377 /*
1378 * The msg_start() above clears msg_didout. The wait_return we do
1379 * here should not overwrite the command that may be shown before
1380 * doing that.
1381 */
1382 msg_didout |= msg_didout_before_start;
1383 wait_return(FALSE);
1384 }
1385 }
1386
Bram Moolenaar2a942252012-11-28 23:03:07 +01001387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001388 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001389#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /*
1391 * Reset if_level, in case a sourced script file contains more ":if" than
1392 * ":endif" (could be ":if x | foo | endif").
1393 */
1394 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001395#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 --call_depth;
1398 return retval;
1399}
1400
1401#ifdef FEAT_EVAL
1402/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001403 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 */
1405 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001406get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001408 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 wcmd_T *wp;
1410 char_u *line;
1411
1412 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1413 {
1414 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001415 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001417 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001419 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001421 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001422 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 ++cp->current_line;
1424
1425 return line;
1426 }
1427
1428 KeyTyped = FALSE;
1429 ++cp->current_line;
1430 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001431 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 return vim_strsave(wp->line);
1433}
1434
1435/*
1436 * Store a line in "gap" so that a ":while" loop can execute it again.
1437 */
1438 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001439store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 if (ga_grow(gap, 1) == FAIL)
1442 return FAIL;
1443 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001444 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return OK;
1447}
1448
1449/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001450 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 */
1452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001453free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 while (gap->ga_len > 0)
1456 {
1457 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1458 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460}
1461#endif
1462
1463/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001464 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1465 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001468getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001469 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001470 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001471 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001474 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001475 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001477 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1478 // function that's originally used to obtain the lines. This may be
1479 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001481 cp = (struct loop_cookie *)cookie;
1482 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 gp = cp->getline;
1485 cp = cp->cookie;
1486 }
1487 return gp == func;
1488#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001489 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491}
1492
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001494 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 * getline function. Otherwise return "cookie".
1496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001498getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001499 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001500 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
Bram Moolenaar13505972019-01-24 15:04:48 +01001502#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001503 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001506 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1507 // cookie that's originally used to obtain the lines. This may be nested
1508 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001509 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 cp = (struct loop_cookie *)cookie;
1511 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 {
1513 gp = cp->getline;
1514 cp = cp->cookie;
1515 }
1516 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001517#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001522
1523/*
1524 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1525 * ":bwipeout", etc.
1526 * Returns the buffer number.
1527 */
1528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001529compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001530{
1531 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001532 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001533 int count = offset;
1534
1535 buf = firstbuf;
1536 while (buf->b_next != NULL && buf->b_fnum < lnum)
1537 buf = buf->b_next;
1538 while (count != 0)
1539 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001540 count += (offset < 0) ? 1 : -1;
1541 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1542 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001544 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001545 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001546 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001547 while (buf->b_ml.ml_mfp == NULL)
1548 {
1549 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1550 if (nextbuf == NULL)
1551 break;
1552 buf = nextbuf;
1553 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001554 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001555 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001556 if (addr_type == ADDR_LOADED_BUFFERS)
1557 while (buf->b_ml.ml_mfp == NULL)
1558 {
1559 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1560 if (nextbuf == NULL)
1561 break;
1562 buf = nextbuf;
1563 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001564 return buf->b_fnum;
1565}
1566
Bram Moolenaarb7316892019-05-01 18:08:42 +02001567/*
1568 * Return the window number of "win".
1569 * When "win" is NULL return the number of windows.
1570 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001571 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001572current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001573{
1574 win_T *wp;
1575 int nr = 0;
1576
Bram Moolenaar29323592016-07-24 22:04:11 +02001577 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001578 {
1579 ++nr;
1580 if (wp == win)
1581 break;
1582 }
1583 return nr;
1584}
1585
1586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001587current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001588{
1589 tabpage_T *tp;
1590 int nr = 0;
1591
Bram Moolenaar29323592016-07-24 22:04:11 +02001592 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001593 {
1594 ++nr;
1595 if (tp == tab)
1596 break;
1597 }
1598 return nr;
1599}
1600
1601# define CURRENT_WIN_NR current_win_nr(curwin)
1602# define LAST_WIN_NR current_win_nr(NULL)
1603# define CURRENT_TAB_NR current_tab_nr(curtab)
1604# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606/*
1607 * Execute one Ex command.
1608 *
1609 * If 'sourcing' is TRUE, the command will be included in the error message.
1610 *
1611 * 1. skip comment lines and leading space
1612 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001613 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001615 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616 * 6. parse arguments
1617 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 *
1621 * This function may be called recursively!
1622 */
1623#if (_MSC_VER == 1200)
1624/*
Bram Moolenaared203462004-06-16 11:19:22 +00001625 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001627 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001630do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001631 char_u **cmdlinep,
1632 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001634 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001636 char_u *(*fgetline)(int, void *, int, int),
1637 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001639 char_u *p;
1640 linenr_T lnum;
1641 long n;
1642 char *errormsg = NULL; // error message
1643 char_u *after_modifier = NULL;
1644 exarg_T ea; // Ex command arguments
1645 int save_msg_scroll = msg_scroll;
1646 cmdmod_T save_cmdmod;
1647 int save_reg_executing = reg_executing;
1648 int ni; // set when Not Implemented
1649 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001650#ifdef FEAT_EVAL
1651 int starts_with_colon;
1652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 vim_memset(&ea, 0, sizeof(ea));
1655 ea.line1 = 1;
1656 ea.line2 = 1;
1657#ifdef FEAT_EVAL
1658 ++ex_nesting_level;
1659#endif
1660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001661 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (quitmore
1663#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001664 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001665 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001666#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001667 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 --quitmore;
1670
1671 /*
1672 * Reset browse, confirm, etc.. They are restored when returning, for
1673 * recursive calls.
1674 */
1675 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001677 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001678 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1679 goto doend;
1680
Bram Moolenaar4facea32019-10-12 20:17:40 +02001681 if (p_verbose >= 16)
1682 msg_verbose_cmd(0, *cmdlinep);
1683
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001684/*
1685 * 1. Skip comment lines and leading white space and colons.
1686 * 2. Handle command modifiers.
1687 */
1688 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001690 ea.cmdlinep = cmdlinep;
1691 ea.getline = fgetline;
1692 ea.cookie = cookie;
1693#ifdef FEAT_EVAL
1694 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001695 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001697 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001698 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001700 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
1702#ifdef FEAT_EVAL
1703 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1704 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1705#else
1706 ea.skip = (if_level > 0);
1707#endif
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 3. Skip over the range to find the command. Let "p" point to after it.
1711 *
1712 * We need the command to know what kind of range it uses.
1713 */
1714 cmd = ea.cmd;
1715 ea.cmd = skip_range(ea.cmd, NULL);
1716 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1717 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718
1719#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001720 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1722 else
1723#endif
1724 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001725
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001726#ifdef FEAT_EVAL
1727# ifdef FEAT_PROFILE
1728 // Count this line for profiling if skip is TRUE.
1729 if (do_profiling == PROF_YES
1730 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1731 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1732 {
1733 int skip = did_emsg || got_int || did_throw;
1734
1735 if (ea.cmdidx == CMD_catch)
1736 skip = !skip && !(cstack->cs_idx >= 0
1737 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1738 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1739 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1740 skip = skip || !(cstack->cs_idx >= 0
1741 && !(cstack->cs_flags[cstack->cs_idx]
1742 & (CSF_ACTIVE | CSF_TRUE)));
1743 else if (ea.cmdidx == CMD_finally)
1744 skip = FALSE;
1745 else if (ea.cmdidx != CMD_endif
1746 && ea.cmdidx != CMD_endfor
1747 && ea.cmdidx != CMD_endtry
1748 && ea.cmdidx != CMD_endwhile)
1749 skip = ea.skip;
1750
1751 if (!skip)
1752 {
1753 if (getline_equal(fgetline, cookie, get_func_line))
1754 func_line_exec(getline_cookie(fgetline, cookie));
1755 else if (getline_equal(fgetline, cookie, getsourceline))
1756 script_line_exec();
1757 }
1758 }
1759# endif
1760
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // May go to debug mode. If this happens and the ">quit" debug command is
1762 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001763 dbg_check_breakpoint(&ea);
1764 if (!ea.skip && got_int)
1765 {
1766 ea.skip = TRUE;
1767 (void)do_intthrow(cstack);
1768 }
1769#endif
1770
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001771/*
1772 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 *
1774 * where 'addr' is:
1775 *
1776 * % (entire file)
1777 * $ [+-NUM]
1778 * 'x [+-NUM] (where x denotes a currently defined mark)
1779 * . [+-NUM]
1780 * [+-NUM]..
1781 * NUM
1782 *
1783 * The ea.cmd pointer is updated to point to the first character following the
1784 * range spec. If an initial address is found, but no second, the upper bound
1785 * is equal to the lower.
1786 */
1787
Bram Moolenaar25190db2019-05-04 15:05:28 +02001788 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001789 if (!IS_USER_CMDIDX(ea.cmdidx))
1790 {
1791 if (ea.cmdidx != CMD_SIZE)
1792 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1793 else
1794 ea.addr_type = ADDR_LINES;
1795
Bram Moolenaar25190db2019-05-04 15:05:28 +02001796 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001797 if (ea.cmdidx == CMD_wincmd && p != NULL)
1798 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001799#ifdef FEAT_QUICKFIX
1800 // :.cc in quickfix window uses line number
1801 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1802 ea.addr_type = ADDR_OTHER;
1803#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001804 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001805
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001806 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001807 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001808 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001811 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 */
1813
1814 /*
1815 * Skip ':' and any white space
1816 */
1817 ea.cmd = skipwhite(ea.cmd);
1818 while (*ea.cmd == ':')
1819 ea.cmd = skipwhite(ea.cmd + 1);
1820
1821 /*
1822 * If we got a line, but no command, then go to the line.
1823 * If we find a '|' or '\n' we set ea.nextcmd.
1824 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001825 if (*ea.cmd == NUL || *ea.cmd == '"'
1826 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 {
1828 /*
1829 * strange vi behaviour:
1830 * ":3" jumps to line 3
1831 * ":3|..." prints line 3
1832 * ":|" prints current line
1833 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001834 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001836 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
1838 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001839 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if ((errormsg = invalid_range(&ea)) == NULL)
1841 {
1842 correct_range(&ea);
1843 ex_print(&ea);
1844 }
1845 }
1846 else if (ea.addr_count != 0)
1847 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001848 if (ea.line2 > curbuf->b_ml.ml_line_count)
1849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001850 // With '-' in 'cpoptions' a line number past the file is an
1851 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001852 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1853 ea.line2 = -1;
1854 else
1855 ea.line2 = curbuf->b_ml.ml_line_count;
1856 }
1857
1858 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001859 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 else
1861 {
1862 if (ea.line2 == 0)
1863 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 else
1865 curwin->w_cursor.lnum = ea.line2;
1866 beginline(BL_SOL | BL_FIX);
1867 }
1868 }
1869 goto doend;
1870 }
1871
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001872 // If this looks like an undefined user command and there are CmdUndefined
1873 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001874 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1875 && ASCII_ISUPPER(*ea.cmd)
1876 && has_cmdundefined())
1877 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001878 int ret;
1879
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001880 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001881 while (ASCII_ISALNUM(*p))
1882 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001883 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001884 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1885 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 // If the autocommands did something and didn't cause an error, try
1887 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001888 p = (ret
1889#ifdef FEAT_EVAL
1890 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001891#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001893 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (p == NULL)
1896 {
1897 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001898 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 goto doend;
1900 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001901 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001902 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1903 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
1905 errormsg = uc_fun_cmd();
1906 goto doend;
1907 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (ea.cmdidx == CMD_SIZE)
1910 {
1911 if (!ea.skip)
1912 {
1913 STRCPY(IObuff, _("E492: Not an editor command"));
1914 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001915 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001916 // If the modifier was parsed OK the error must be in the
1917 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001918 if (after_modifier != NULL)
1919 append_command(after_modifier);
1920 else
1921 append_command(*cmdlinep);
1922 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001923 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001924 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 goto doend;
1927 }
1928
Bram Moolenaar958636c2014-10-21 20:01:58 +02001929 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1930 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001931#ifdef HAVE_EX_SCRIPT_NI
1932 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1933#endif
1934 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
1936#ifndef FEAT_EVAL
1937 /*
1938 * When the expression evaluation is disabled, recognize the ":if" and
1939 * ":endif" commands and ignore everything in between it.
1940 */
1941 if (ea.cmdidx == CMD_if)
1942 ++if_level;
1943 if (if_level)
1944 {
1945 if (ea.cmdidx == CMD_endif)
1946 --if_level;
1947 goto doend;
1948 }
1949
1950#endif
1951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001952 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001953 if (*p == '!' && ea.cmdidx != CMD_substitute
1954 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 ++p;
1957 ea.forceit = TRUE;
1958 }
1959 else
1960 ea.forceit = FALSE;
1961
1962/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001963 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001965 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001966 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968 if (!ea.skip)
1969 {
1970#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001971 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001973 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001974 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 goto doend;
1976 }
1977#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001978 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001979 {
1980 errormsg = _("E981: Command not allowed in rvim");
1981 goto doend;
1982 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001983 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001986 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 goto doend;
1988 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001989
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001990 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001991 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001993 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001994 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 goto doend;
1996 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001998 // Disallow editing another buffer when "curbuf_lock" is set.
1999 // Do allow ":checktime" (it is postponed).
2000 // Do allow ":edit" (check for an argument later).
2001 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002002 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002003 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002004 && ea.cmdidx != CMD_edit
2005 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002006 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002007 && curbuf_locked())
2008 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002010 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002013 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 goto doend;
2015 }
2016 }
2017
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002018 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002020 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 goto doend;
2022 }
2023
2024 /*
2025 * Don't complain about the range if it is not used
2026 * (could happen if line_count is accidentally set to 0).
2027 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002028 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
2030 /*
2031 * If the range is backwards, ask for confirmation and, if given, swap
2032 * ea.line1 & ea.line2 so it's forwards again.
2033 * When global command is busy, don't ask, will fail below.
2034 */
2035 if (!global_busy && ea.line1 > ea.line2)
2036 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002037 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002039 if (sourcing || exmode_active)
2040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002041 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002042 goto doend;
2043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (ask_yesno((char_u *)
2045 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002046 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048 lnum = ea.line1;
2049 ea.line1 = ea.line2;
2050 ea.line2 = lnum;
2051 }
2052 if ((errormsg = invalid_range(&ea)) != NULL)
2053 goto doend;
2054 }
2055
Bram Moolenaarb7316892019-05-01 18:08:42 +02002056 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2057 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 ea.line2 = 1;
2059
2060 correct_range(&ea);
2061
2062#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002063 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002064 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002066 // Put the first line at the start of a closed fold, put the last line
2067 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 (void)hasFolding(ea.line1, &ea.line1, NULL);
2069 (void)hasFolding(ea.line2, NULL, &ea.line2);
2070 }
2071#endif
2072
2073#ifdef FEAT_QUICKFIX
2074 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002075 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 * option here, so things like % get expanded.
2077 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002078 p = replace_makeprg(&ea, p, cmdlinep);
2079 if (p == NULL)
2080 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081#endif
2082
2083 /*
2084 * Skip to start of argument.
2085 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2086 */
2087 if (ea.cmdidx == CMD_bang)
2088 ea.arg = p;
2089 else
2090 ea.arg = skipwhite(p);
2091
Bram Moolenaar379fb762018-08-30 15:58:28 +02002092 // ":file" cannot be run with an argument when "curbuf_lock" is set
2093 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2094 goto doend;
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 /*
2097 * Check for "++opt=val" argument.
2098 * Must be first, allow ":w ++enc=utf8 !cmd"
2099 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002100 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2102 if (getargopt(&ea) == FAIL && !ni)
2103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
2107
2108 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2109 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002110 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002112 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002114 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 goto doend;
2116 }
2117 ea.arg = skipwhite(ea.arg + 1);
2118 ea.append = TRUE;
2119 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002120 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
2122 ++ea.arg;
2123 ea.usefilter = TRUE;
2124 }
2125 }
2126
2127 if (ea.cmdidx == CMD_read)
2128 {
2129 if (ea.forceit)
2130 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002131 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 ea.forceit = FALSE;
2133 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 ++ea.arg;
2137 ea.usefilter = TRUE;
2138 }
2139 }
2140
2141 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2142 {
2143 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002144 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 ++ea.arg;
2147 ++ea.amount;
2148 }
2149 ea.arg = skipwhite(ea.arg);
2150 }
2151
2152 /*
2153 * Check for "+command" argument, before checking for next command.
2154 * Don't do this for ":read !cmd" and ":write !cmd".
2155 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002156 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2158
2159 /*
2160 * Check for '|' to separate commands and '"' to start comments.
2161 * Don't do this for ":read !cmd" and ":write !cmd".
2162 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002163 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 separate_nextcmd(&ea);
2165
2166 /*
2167 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002168 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2169 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002171 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002172 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002173 || ea.cmdidx == CMD_global
2174 || ea.cmdidx == CMD_vglobal
2175 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 for (p = ea.arg; *p; ++p)
2178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002179 // Remove one backslash before a newline, so that it's possible to
2180 // pass a newline to the shell and also a newline that is preceded
2181 // with a backslash. This makes it impossible to end a shell
2182 // command in a backslash, but that doesn't appear useful.
2183 // Halving the number of backslashes is incompatible with previous
2184 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002186 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 else if (*p == '\n')
2188 {
2189 ea.nextcmd = p + 1;
2190 *p = NUL;
2191 break;
2192 }
2193 }
2194 }
2195
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002196 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002198 buf_T *buf;
2199
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002201 switch (ea.addr_type)
2202 {
2203 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002204 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002205 ea.line2 = curbuf->b_ml.ml_line_count;
2206 break;
2207 case ADDR_LOADED_BUFFERS:
2208 buf = firstbuf;
2209 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2210 buf = buf->b_next;
2211 ea.line1 = buf->b_fnum;
2212 buf = lastbuf;
2213 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2214 buf = buf->b_prev;
2215 ea.line2 = buf->b_fnum;
2216 break;
2217 case ADDR_BUFFERS:
2218 ea.line1 = firstbuf->b_fnum;
2219 ea.line2 = lastbuf->b_fnum;
2220 break;
2221 case ADDR_WINDOWS:
2222 ea.line2 = LAST_WIN_NR;
2223 break;
2224 case ADDR_TABS:
2225 ea.line2 = LAST_TAB_NR;
2226 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002227 case ADDR_TABS_RELATIVE:
2228 ea.line2 = 1;
2229 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002230 case ADDR_ARGUMENTS:
2231 if (ARGCOUNT == 0)
2232 ea.line1 = ea.line2 = 0;
2233 else
2234 ea.line2 = ARGCOUNT;
2235 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002236 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002237#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002238 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002239 if (ea.line2 == 0)
2240 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002241#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002242 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002243 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002244 case ADDR_UNSIGNED:
2245 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002246 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002247 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
2250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002251 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002252 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002254 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002255 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002256 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002258#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002260 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002262 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002263 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002266 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2267 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002268 {
2269 ea.regname = *ea.arg++;
2270#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002272 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2273 {
2274 set_expr_line(vim_strsave(ea.arg));
2275 ea.arg += STRLEN(ea.arg);
2276 }
2277#endif
2278 ea.arg = skipwhite(ea.arg);
2279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281
2282 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002283 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 * count, it's a buffer name.
2285 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002286 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2287 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002288 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 n = getdigits(&ea.arg);
2291 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002292 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002294 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 goto doend;
2296 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002297 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
2299 ea.line2 = n;
2300 if (ea.addr_count == 0)
2301 ea.addr_count = 1;
2302 }
2303 else
2304 {
2305 ea.line1 = ea.line2;
2306 ea.line2 += n - 1;
2307 ++ea.addr_count;
2308 /*
2309 * Be vi compatible: no error message for out of range.
2310 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002311 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 ea.line2 = curbuf->b_ml.ml_line_count;
2313 }
2314 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002315
2316 /*
2317 * Check for flags: 'l', 'p' and '#'.
2318 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002319 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002320 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002321 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2322 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002324 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002325 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 goto doend;
2327 }
2328
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002329 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002331 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 goto doend;
2333 }
2334
2335#ifdef FEAT_EVAL
2336 /*
2337 * Skip the command when it's not going to be executed.
2338 * The commands like :if, :endif, etc. always need to be executed.
2339 * Also make an exception for commands that handle a trailing command
2340 * themselves.
2341 */
2342 if (ea.skip)
2343 {
2344 switch (ea.cmdidx)
2345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002346 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 case CMD_while:
2348 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002349 case CMD_for:
2350 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 case CMD_if:
2352 case CMD_elseif:
2353 case CMD_else:
2354 case CMD_endif:
2355 case CMD_try:
2356 case CMD_catch:
2357 case CMD_finally:
2358 case CMD_endtry:
2359 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002360 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 break;
2362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 // Commands that handle '|' themselves. Check: A command should
2364 // either have the EX_TRLBAR flag, appear in this list or appear in
2365 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 case CMD_aboveleft:
2367 case CMD_and:
2368 case CMD_belowright:
2369 case CMD_botright:
2370 case CMD_browse:
2371 case CMD_call:
2372 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002373 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 case CMD_delfunction:
2375 case CMD_djump:
2376 case CMD_dlist:
2377 case CMD_dsearch:
2378 case CMD_dsplit:
2379 case CMD_echo:
2380 case CMD_echoerr:
2381 case CMD_echomsg:
2382 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002383 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002385 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 case CMD_help:
2387 case CMD_hide:
2388 case CMD_ijump:
2389 case CMD_ilist:
2390 case CMD_isearch:
2391 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002392 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 case CMD_keepjumps:
2394 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002395 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 case CMD_leftabove:
2397 case CMD_let:
2398 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002399 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002400 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002402 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002403 case CMD_noautocmd:
2404 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 case CMD_perl:
2406 case CMD_psearch:
2407 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002408 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002409 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_return:
2411 case CMD_rightbelow:
2412 case CMD_ruby:
2413 case CMD_silent:
2414 case CMD_smagic:
2415 case CMD_snomagic:
2416 case CMD_substitute:
2417 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002418 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_tcl:
2420 case CMD_throw:
2421 case CMD_tilde:
2422 case CMD_topleft:
2423 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002424 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_verbose:
2426 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002427 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 break;
2429
2430 default: goto doend;
2431 }
2432 }
2433#endif
2434
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002435 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
2437 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2438 goto doend;
2439 }
2440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 /*
2442 * Accept buffer name. Cannot be used at the same time with a buffer
2443 * number. Don't do this for a user command.
2444 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002445 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002446 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 /*
2449 * :bdelete, :bwipeout and :bunload take several arguments, separated
2450 * by spaces: find next space (skipping over escaped characters).
2451 * The others take one argument: ignore trailing spaces.
2452 */
2453 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2454 || ea.cmdidx == CMD_bunload)
2455 p = skiptowhite_esc(ea.arg);
2456 else
2457 {
2458 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002459 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 --p;
2461 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002462 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002463 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002464 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 goto doend;
2466 ea.addr_count = 1;
2467 ea.arg = skipwhite(p);
2468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002470 // The :try command saves the emsg_silent flag, reset it here when
2471 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002472 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002473 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002474 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002475 if (emsg_silent < 0)
2476 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002477 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002478 }
2479
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002481 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
Bram Moolenaar958636c2014-10-21 20:01:58 +02002484 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 /*
2487 * Execute a user-defined command.
2488 */
2489 do_ucmd(&ea);
2490 }
2491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
2493 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002494 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 */
2496 ea.errmsg = NULL;
2497 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2498 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002499 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 }
2501
2502#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002504 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2505 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002506 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 /*
2509 * If the command just executed called do_cmdline(), any throw or ":return"
2510 * or ":finish" encountered there must also check the cstack of the still
2511 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2512 * exception, or reanimate a returned function or finished script file and
2513 * return or finish it again.
2514 */
2515 if (need_rethrow)
2516 do_throw(cstack);
2517 else if (check_cstack)
2518 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002519 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002521 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 && current_func_returned())
2523 do_return(&ea, TRUE, FALSE, NULL);
2524 }
2525 need_rethrow = check_cstack = FALSE;
2526#endif
2527
2528doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002529 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002532 curwin->w_cursor.col = 0;
2533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2536 {
2537 if (sourcing)
2538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002539 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
2541 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002542 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002544 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546 emsg(errormsg);
2547 }
2548#ifdef FEAT_EVAL
2549 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002550 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2551 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#endif
2553
Bram Moolenaareffed932018-08-14 13:38:17 +02002554 if (ea.verbose_save >= 0)
2555 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002556
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002557 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002559 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaareffed932018-08-14 13:38:17 +02002561 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002563 // messages could be enabled for a serious error, need to check if the
2564 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002565 if (!did_emsg || msg_silent > ea.save_msg_silent)
2566 msg_silent = ea.save_msg_silent;
2567 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (emsg_silent < 0)
2569 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002570 // Restore msg_scroll, it's set by file I/O commands, even when no
2571 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002574 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2575 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002576 if (redirecting())
2577 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002580#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002581 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002582 --sandbox;
2583#endif
2584
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002585 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 ea.nextcmd = NULL;
2587
2588#ifdef FEAT_EVAL
2589 --ex_nesting_level;
2590#endif
2591
2592 return ea.nextcmd;
2593}
2594#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002595 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596#endif
2597
2598/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002599 * Parse and skip over command modifiers:
2600 * - update eap->cmd
2601 * - store flags in "cmdmod".
2602 * - Set ex_pressedreturn for an empty command line.
2603 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002604 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002605 * - set p_verbose for ":verbose"
2606 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002607 * When "skip_only" is TRUE the global variables are not changed, except for
2608 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002609 * Return FAIL when the command is not to be executed.
2610 * May set "errormsg" to an error message.
2611 */
2612 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002614{
2615 char_u *p;
2616
2617 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2618 eap->verbose_save = -1;
2619 eap->save_msg_silent = -1;
2620
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002621 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 for (;;)
2623 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002624 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2625 ++eap->cmd;
2626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002627 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002628 if (*eap->cmd == NUL && exmode_active
2629 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2630 || getline_equal(eap->getline, eap->cookie, getexline))
2631 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2632 {
2633 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002634 if (!skip_only)
2635 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 }
2637
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002638 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 if (*eap->cmd == '"')
2640 return FAIL;
2641 if (*eap->cmd == NUL)
2642 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002643 if (!skip_only)
2644 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002645 return FAIL;
2646 }
2647
Bram Moolenaareffed932018-08-14 13:38:17 +02002648 p = skip_range(eap->cmd, NULL);
2649 switch (*p)
2650 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002651 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002652 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2653 break;
2654 cmdmod.split |= WSP_ABOVE;
2655 continue;
2656
2657 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2658 {
2659 cmdmod.split |= WSP_BELOW;
2660 continue;
2661 }
2662 if (checkforcmd(&eap->cmd, "browse", 3))
2663 {
2664#ifdef FEAT_BROWSE_CMD
2665 cmdmod.browse = TRUE;
2666#endif
2667 continue;
2668 }
2669 if (!checkforcmd(&eap->cmd, "botright", 2))
2670 break;
2671 cmdmod.split |= WSP_BOT;
2672 continue;
2673
2674 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2675 break;
2676#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2677 cmdmod.confirm = TRUE;
2678#endif
2679 continue;
2680
2681 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2682 {
2683 cmdmod.keepmarks = TRUE;
2684 continue;
2685 }
2686 if (checkforcmd(&eap->cmd, "keepalt", 5))
2687 {
2688 cmdmod.keepalt = TRUE;
2689 continue;
2690 }
2691 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2692 {
2693 cmdmod.keeppatterns = TRUE;
2694 continue;
2695 }
2696 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2697 break;
2698 cmdmod.keepjumps = TRUE;
2699 continue;
2700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002701 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002702 {
2703 char_u *reg_pat;
2704
2705 if (!checkforcmd(&p, "filter", 4)
2706 || *p == NUL || ends_excmd(*p))
2707 break;
2708 if (*p == '!')
2709 {
2710 cmdmod.filter_force = TRUE;
2711 p = skipwhite(p + 1);
2712 if (*p == NUL || ends_excmd(*p))
2713 break;
2714 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002715 if (skip_only)
2716 p = skip_vimgrep_pat(p, NULL, NULL);
2717 else
2718 // NOTE: This puts a NUL after the pattern.
2719 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 if (p == NULL || *p == NUL)
2721 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002722 if (!skip_only)
2723 {
2724 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002725 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002726 if (cmdmod.filter_regmatch.regprog == NULL)
2727 break;
2728 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002729 eap->cmd = p;
2730 continue;
2731 }
2732
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002733 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002734 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2735 || *p == NUL || ends_excmd(*p))
2736 break;
2737 eap->cmd = p;
2738 cmdmod.hide = TRUE;
2739 continue;
2740
2741 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2742 {
2743 cmdmod.lockmarks = TRUE;
2744 continue;
2745 }
2746
2747 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2748 break;
2749 cmdmod.split |= WSP_ABOVE;
2750 continue;
2751
2752 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2753 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002754 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002755 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002756 // Set 'eventignore' to "all". Restore the
2757 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002758 cmdmod.save_ei = vim_strsave(p_ei);
2759 set_string_option_direct((char_u *)"ei", -1,
2760 (char_u *)"all", OPT_FREE, SID_NONE);
2761 }
2762 continue;
2763 }
2764 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2765 break;
2766 cmdmod.noswapfile = TRUE;
2767 continue;
2768
2769 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2770 break;
2771 cmdmod.split |= WSP_BELOW;
2772 continue;
2773
2774 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2775 {
2776#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002777 if (!skip_only)
2778 {
2779 if (!eap->did_sandbox)
2780 ++sandbox;
2781 eap->did_sandbox = TRUE;
2782 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002783#endif
2784 continue;
2785 }
2786 if (!checkforcmd(&eap->cmd, "silent", 3))
2787 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002788 if (!skip_only)
2789 {
2790 if (eap->save_msg_silent == -1)
2791 eap->save_msg_silent = msg_silent;
2792 ++msg_silent;
2793 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2795 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002796 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002798 if (!skip_only)
2799 {
2800 ++emsg_silent;
2801 ++eap->did_esilent;
2802 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 }
2804 continue;
2805
2806 case 't': if (checkforcmd(&p, "tab", 3))
2807 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002808 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002809 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002810 long tabnr = get_address(eap, &eap->cmd,
2811 ADDR_TABS, eap->skip,
2812 skip_only, FALSE, 1);
2813 if (tabnr == MAXLNUM)
2814 cmdmod.tab = tabpage_index(curtab) + 1;
2815 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002817 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002819 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002820 return FAIL;
2821 }
2822 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002823 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 }
2825 eap->cmd = p;
2826 continue;
2827 }
2828 if (!checkforcmd(&eap->cmd, "topleft", 2))
2829 break;
2830 cmdmod.split |= WSP_TOP;
2831 continue;
2832
2833 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2834 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002835 if (!skip_only)
2836 {
2837 if (eap->save_msg_silent == -1)
2838 eap->save_msg_silent = msg_silent;
2839 msg_silent = 0;
2840 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 continue;
2842
2843 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2844 {
2845 cmdmod.split |= WSP_VERT;
2846 continue;
2847 }
2848 if (!checkforcmd(&p, "verbose", 4))
2849 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002850 if (!skip_only)
2851 {
2852 if (eap->verbose_save < 0)
2853 eap->verbose_save = p_verbose;
2854 if (vim_isdigit(*eap->cmd))
2855 p_verbose = atoi((char *)eap->cmd);
2856 else
2857 p_verbose = 1;
2858 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 eap->cmd = p;
2860 continue;
2861 }
2862 break;
2863 }
2864
2865 return OK;
2866}
2867
2868/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002869 * Free contents of "cmdmod".
2870 */
2871 static void
2872free_cmdmod(void)
2873{
2874 if (cmdmod.save_ei != NULL)
2875 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002876 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002877 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2878 OPT_FREE, SID_NONE);
2879 free_string_option(cmdmod.save_ei);
2880 }
2881
2882 if (cmdmod.filter_regmatch.regprog != NULL)
2883 vim_regfree(cmdmod.filter_regmatch.regprog);
2884}
2885
2886/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002887 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002888 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002889 * Return FAIL and set "errormsg" or return OK.
2890 */
2891 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002892parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002893{
2894 int address_count = 1;
2895 linenr_T lnum;
2896
2897 // Repeat for all ',' or ';' separated addresses.
2898 for (;;)
2899 {
2900 eap->line1 = eap->line2;
2901 switch (eap->addr_type)
2902 {
2903 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002904 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002905 // default is current line number
2906 eap->line2 = curwin->w_cursor.lnum;
2907 break;
2908 case ADDR_WINDOWS:
2909 eap->line2 = CURRENT_WIN_NR;
2910 break;
2911 case ADDR_ARGUMENTS:
2912 eap->line2 = curwin->w_arg_idx + 1;
2913 if (eap->line2 > ARGCOUNT)
2914 eap->line2 = ARGCOUNT;
2915 break;
2916 case ADDR_LOADED_BUFFERS:
2917 case ADDR_BUFFERS:
2918 eap->line2 = curbuf->b_fnum;
2919 break;
2920 case ADDR_TABS:
2921 eap->line2 = CURRENT_TAB_NR;
2922 break;
2923 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002924 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002925 eap->line2 = 1;
2926 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002927 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002928#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002929 eap->line2 = qf_get_cur_idx(eap);
2930#endif
2931 break;
2932 case ADDR_QUICKFIX_VALID:
2933#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002934 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002935#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002936 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002937 case ADDR_NONE:
2938 // Will give an error later if a range is found.
2939 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002940 }
2941 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002942 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002943 eap->addr_count == 0, address_count++);
2944 if (eap->cmd == NULL) // error detected
2945 return FAIL;
2946 if (lnum == MAXLNUM)
2947 {
2948 if (*eap->cmd == '%') // '%' - all lines
2949 {
2950 ++eap->cmd;
2951 switch (eap->addr_type)
2952 {
2953 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002954 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002955 eap->line1 = 1;
2956 eap->line2 = curbuf->b_ml.ml_line_count;
2957 break;
2958 case ADDR_LOADED_BUFFERS:
2959 {
2960 buf_T *buf = firstbuf;
2961
2962 while (buf->b_next != NULL
2963 && buf->b_ml.ml_mfp == NULL)
2964 buf = buf->b_next;
2965 eap->line1 = buf->b_fnum;
2966 buf = lastbuf;
2967 while (buf->b_prev != NULL
2968 && buf->b_ml.ml_mfp == NULL)
2969 buf = buf->b_prev;
2970 eap->line2 = buf->b_fnum;
2971 break;
2972 }
2973 case ADDR_BUFFERS:
2974 eap->line1 = firstbuf->b_fnum;
2975 eap->line2 = lastbuf->b_fnum;
2976 break;
2977 case ADDR_WINDOWS:
2978 case ADDR_TABS:
2979 if (IS_USER_CMDIDX(eap->cmdidx))
2980 {
2981 eap->line1 = 1;
2982 eap->line2 = eap->addr_type == ADDR_WINDOWS
2983 ? LAST_WIN_NR : LAST_TAB_NR;
2984 }
2985 else
2986 {
2987 // there is no Vim command which uses '%' and
2988 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002989 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002990 return FAIL;
2991 }
2992 break;
2993 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002994 case ADDR_UNSIGNED:
2995 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002996 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002997 return FAIL;
2998 case ADDR_ARGUMENTS:
2999 if (ARGCOUNT == 0)
3000 eap->line1 = eap->line2 = 0;
3001 else
3002 {
3003 eap->line1 = 1;
3004 eap->line2 = ARGCOUNT;
3005 }
3006 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003007 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003008#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003010 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003011 if (eap->line2 == 0)
3012 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003013#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003014 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003015 case ADDR_NONE:
3016 // Will give an error later if a range is found.
3017 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003018 }
3019 ++eap->addr_count;
3020 }
3021 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3022 {
3023 pos_T *fp;
3024
3025 // '*' - visual area
3026 if (eap->addr_type != ADDR_LINES)
3027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003028 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003029 return FAIL;
3030 }
3031
3032 ++eap->cmd;
3033 if (!eap->skip)
3034 {
3035 fp = getmark('<', FALSE);
3036 if (check_mark(fp) == FAIL)
3037 return FAIL;
3038 eap->line1 = fp->lnum;
3039 fp = getmark('>', FALSE);
3040 if (check_mark(fp) == FAIL)
3041 return FAIL;
3042 eap->line2 = fp->lnum;
3043 ++eap->addr_count;
3044 }
3045 }
3046 }
3047 else
3048 eap->line2 = lnum;
3049 eap->addr_count++;
3050
3051 if (*eap->cmd == ';')
3052 {
3053 if (!eap->skip)
3054 {
3055 curwin->w_cursor.lnum = eap->line2;
3056 // don't leave the cursor on an illegal line or column
3057 check_cursor();
3058 }
3059 }
3060 else if (*eap->cmd != ',')
3061 break;
3062 ++eap->cmd;
3063 }
3064
3065 // One address given: set start and end lines.
3066 if (eap->addr_count == 1)
3067 {
3068 eap->line1 = eap->line2;
3069 // ... but only implicit: really no address given
3070 if (lnum == MAXLNUM)
3071 eap->addr_count = 0;
3072 }
3073 return OK;
3074}
3075
3076/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003077 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 * If there is a match advance "pp" to the argument and return TRUE.
3079 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003080 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003081checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003082 char_u **pp, // start of command
3083 char *cmd, // name of command
3084 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 int i;
3087
3088 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003089 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 break;
3091 if (i >= len && !isalpha((*pp)[i]))
3092 {
3093 *pp = skipwhite(*pp + i);
3094 return TRUE;
3095 }
3096 return FALSE;
3097}
3098
3099/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003100 * Append "cmd" to the error message in IObuff.
3101 * Takes care of limiting the length and handling 0xa0, which would be
3102 * invisible otherwise.
3103 */
3104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003105append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003106{
3107 char_u *s = cmd;
3108 char_u *d;
3109
3110 STRCAT(IObuff, ": ");
3111 d = IObuff + STRLEN(IObuff);
3112 while (*s != NUL && d - IObuff < IOSIZE - 7)
3113 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003114 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003115 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003116 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003117 STRCPY(d, "<a0>");
3118 d += 4;
3119 }
3120 else
3121 MB_COPY_CHAR(s, d);
3122 }
3123 *d = NUL;
3124}
3125
3126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003128 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003129 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003130 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 *
3132 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3133 * assignment without "let". Sets eap->cmdidx to the command while returning
3134 * "eap->cmd".
3135 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 * Returns NULL for an ambiguous user command.
3137 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 char_u *
3139find_ex_command(
3140 exarg_T *eap,
3141 int *full UNUSED,
3142 int (*lookup)(char_u *, size_t, cctx_T *) UNUSED,
3143 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
3145 int len;
3146 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003147 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149#ifdef FEAT_EVAL
3150 /*
3151 * Recognize a Vim9 script function/method call and assignment:
3152 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3153 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003154 p = eap->cmd;
3155 if (lookup != NULL && (*p == '('
3156 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 {
3158 int oplen;
3159 int heredoc;
3160
3161 // "funcname(" is always a function call.
3162 // "varname[]" is an expression.
3163 // "g:varname" is an expression.
3164 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003165 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 if (*p == '('
3167 || *p == '['
3168 || p[1] == ':'
3169 || (*p == '-' && p[1] == '>'))
3170 {
3171 eap->cmdidx = CMD_eval;
3172 return eap->cmd;
3173 }
3174
3175 oplen = assignment_len(skipwhite(p), &heredoc);
3176 if (oplen > 0)
3177 {
3178 // Recognize an assignment if we recognize the variable name:
3179 // "g:var = expr"
3180 // "var = expr" where "var" is a local var name.
3181 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3182 || lookup(eap->cmd, p - eap->cmd, cctx) >= 0)
3183 {
3184 eap->cmdidx = CMD_let;
3185 return eap->cmd;
3186 }
3187 }
3188 }
3189#endif
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 /*
3192 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003193 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 * - the 'k' command can directly be followed by any character.
3195 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003196 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003198 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
3200 p = eap->cmd;
3201 if (*p == 'k')
3202 {
3203 eap->cmdidx = CMD_k;
3204 ++p;
3205 }
3206 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003207 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3208 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 || p[1] == 'g'
3210 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3211 || p[1] == 'I'
3212 || (p[1] == 'r' && p[2] != 'e')))
3213 {
3214 eap->cmdidx = CMD_substitute;
3215 ++p;
3216 }
3217 else
3218 {
3219 while (ASCII_ISALPHA(*p))
3220 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003221 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003222 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003224 while (ASCII_ISALNUM(*p))
3225 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 }
3227 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3228 {
3229 // include "9" for "vim9script"
3230 ++p;
3231 while (ASCII_ISALPHA(*p))
3232 ++p;
3233 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003235 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3237 ++p;
3238 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003239 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003241 // Check for ":dl", ":dell", etc. to ":deletel": that's
3242 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003243 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003244 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003245 break;
3246 if (i == len - 1)
3247 {
3248 --len;
3249 if (p[-1] == 'l')
3250 eap->flags |= EXFLAG_LIST;
3251 else
3252 eap->flags |= EXFLAG_PRINT;
3253 }
3254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003256 if (ASCII_ISLOWER(eap->cmd[0]))
3257 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003258 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003259 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003260
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003261 if (command_count != (int)CMD_SIZE)
3262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003263 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003264 getout(1);
3265 }
3266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003267 // Use a precomputed index for fast look-up in cmdnames[]
3268 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003269 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3270 if (ASCII_ISLOWER(c2))
3271 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003274 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3277 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3278 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3279 (size_t)len) == 0)
3280 {
3281#ifdef FEAT_EVAL
3282 if (full != NULL
3283 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3284 *full = TRUE;
3285#endif
3286 break;
3287 }
3288
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003289 // Look for a user defined command as a last resort. Let ":Print" be
3290 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003291 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3292 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003294 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 while (ASCII_ISALNUM(*p))
3296 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003297 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003299 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 eap->cmdidx = CMD_SIZE;
3301 }
3302
3303 return p;
3304}
3305
3306#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003307static struct cmdmod
3308{
3309 char *name;
3310 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003311 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003312} cmdmods[] = {
3313 {"aboveleft", 3, FALSE},
3314 {"belowright", 3, FALSE},
3315 {"botright", 2, FALSE},
3316 {"browse", 3, FALSE},
3317 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003318 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003319 {"hide", 3, FALSE},
3320 {"keepalt", 5, FALSE},
3321 {"keepjumps", 5, FALSE},
3322 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003323 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003324 {"leftabove", 5, FALSE},
3325 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003326 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003327 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003328 {"rightbelow", 6, FALSE},
3329 {"sandbox", 3, FALSE},
3330 {"silent", 3, FALSE},
3331 {"tab", 3, TRUE},
3332 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003333 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003334 {"verbose", 4, TRUE},
3335 {"vertical", 4, FALSE},
3336};
3337
3338/*
3339 * Return length of a command modifier (including optional count).
3340 * Return zero when it's not a modifier.
3341 */
3342 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003343modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003344{
3345 int i, j;
3346 char_u *p = cmd;
3347
3348 if (VIM_ISDIGIT(*cmd))
3349 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003350 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003351 {
3352 for (j = 0; p[j] != NUL; ++j)
3353 if (p[j] != cmdmods[i].name[j])
3354 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003355 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003356 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003357 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003358 }
3359 return 0;
3360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
3363 * Return > 0 if an Ex command "name" exists.
3364 * Return 2 if there is an exact match.
3365 * Return 3 if there is an ambiguous match.
3366 */
3367 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003368cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 exarg_T ea;
3371 int full = FALSE;
3372 int i;
3373 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003374 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003376 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003377 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
3379 for (j = 0; name[j] != NUL; ++j)
3380 if (name[j] != cmdmods[i].name[j])
3381 break;
3382 if (name[j] == NUL && j >= cmdmods[i].minlen)
3383 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3384 }
3385
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003386 // Check built-in commands and user defined commands.
3387 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003388 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003391 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003393 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3394 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003395 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003396 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3398}
3399#endif
3400
Bram Moolenaard0190392019-08-23 21:17:35 +02003401 cmdidx_T
3402excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
Bram Moolenaard0190392019-08-23 21:17:35 +02003404 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaard0190392019-08-23 21:17:35 +02003406 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3407 idx = (cmdidx_T)((int)idx + 1))
3408 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 break;
3410
Bram Moolenaard0190392019-08-23 21:17:35 +02003411 return idx;
3412}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaard0190392019-08-23 21:17:35 +02003414 long
3415excmd_get_argt(cmdidx_T idx)
3416{
3417 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418}
3419
3420/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003421 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 *
3423 * Backslashed delimiters after / or ? will be skipped, and commands will
3424 * not be expanded between /'s and ?'s or after "'".
3425 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003426 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 * Returns the "cmd" pointer advanced to beyond the range.
3428 */
3429 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003430skip_range(
3431 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003432 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003434 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003436 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003438 if (*cmd == '\\')
3439 {
3440 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3441 ++cmd;
3442 else
3443 break;
3444 }
3445 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
3447 if (*++cmd == NUL && ctx != NULL)
3448 *ctx = EXPAND_NOTHING;
3449 }
3450 else if (*cmd == '/' || *cmd == '?')
3451 {
3452 delim = *cmd++;
3453 while (*cmd != NUL && *cmd != delim)
3454 if (*cmd++ == '\\' && *cmd != NUL)
3455 ++cmd;
3456 if (*cmd == NUL && ctx != NULL)
3457 *ctx = EXPAND_NOTHING;
3458 }
3459 if (*cmd != NUL)
3460 ++cmd;
3461 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003462
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003463 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003464 while (*cmd == ':')
3465 cmd = skipwhite(cmd + 1);
3466
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 return cmd;
3468}
3469
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003470 static void
3471addr_error(cmd_addr_T addr_type)
3472{
3473 if (addr_type == ADDR_NONE)
3474 emsg(_(e_norange));
3475 else
3476 emsg(_(e_invrange));
3477}
3478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003480 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 *
3482 * Set ptr to the next character after the part that was interpreted.
3483 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003484 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 *
3486 * Return MAXLNUM when no Ex address was found.
3487 */
3488 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003489get_address(
3490 exarg_T *eap UNUSED,
3491 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003492 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003493 int skip, // only skip the address, don't use it
3494 int silent, // no errors or side effects
3495 int to_other_file, // flag: may jump to other file
3496 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 int c;
3499 int i;
3500 long n;
3501 char_u *cmd;
3502 pos_T pos;
3503 pos_T *fp;
3504 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003505 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 cmd = skipwhite(*ptr);
3508 lnum = MAXLNUM;
3509 do
3510 {
3511 switch (*cmd)
3512 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003513 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003514 ++cmd;
3515 switch (addr_type)
3516 {
3517 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003518 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 lnum = curwin->w_cursor.lnum;
3520 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003521 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003522 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003523 break;
3524 case ADDR_ARGUMENTS:
3525 lnum = curwin->w_arg_idx + 1;
3526 break;
3527 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003528 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003529 lnum = curbuf->b_fnum;
3530 break;
3531 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003532 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003533 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003534 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003535 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003536 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003537 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003538 cmd = NULL;
3539 goto error;
3540 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003541 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003542#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003543 lnum = qf_get_cur_idx(eap);
3544#endif
3545 break;
3546 case ADDR_QUICKFIX_VALID:
3547#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003548 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003549#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003550 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003551 }
3552 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003554 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003555 ++cmd;
3556 switch (addr_type)
3557 {
3558 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003559 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 lnum = curbuf->b_ml.ml_line_count;
3561 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003562 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003563 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003564 break;
3565 case ADDR_ARGUMENTS:
3566 lnum = ARGCOUNT;
3567 break;
3568 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003569 buf = lastbuf;
3570 while (buf->b_ml.ml_mfp == NULL)
3571 {
3572 if (buf->b_prev == NULL)
3573 break;
3574 buf = buf->b_prev;
3575 }
3576 lnum = buf->b_fnum;
3577 break;
3578 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003579 lnum = lastbuf->b_fnum;
3580 break;
3581 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003582 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003583 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003584 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003585 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003586 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003587 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003588 cmd = NULL;
3589 goto error;
3590 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003591 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003592#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003593 lnum = qf_get_size(eap);
3594 if (lnum == 0)
3595 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003596#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003597 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003598 case ADDR_QUICKFIX_VALID:
3599#ifdef FEAT_QUICKFIX
3600 lnum = qf_get_valid_size(eap);
3601 if (lnum == 0)
3602 lnum = 1;
3603#endif
3604 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003605 }
3606 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003608 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003609 if (*++cmd == NUL)
3610 {
3611 cmd = NULL;
3612 goto error;
3613 }
3614 if (addr_type != ADDR_LINES)
3615 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003616 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003617 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003618 goto error;
3619 }
3620 if (skip)
3621 ++cmd;
3622 else
3623 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003624 // Only accept a mark in another file when it is
3625 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003626 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3627 ++cmd;
3628 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003629 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003630 lnum = curwin->w_cursor.lnum;
3631 else
3632 {
3633 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 cmd = NULL;
3636 goto error;
3637 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003638 lnum = fp->lnum;
3639 }
3640 }
3641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003644 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003645 c = *cmd++;
3646 if (addr_type != ADDR_LINES)
3647 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003648 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003649 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003650 goto error;
3651 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003652 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003653 {
3654 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3655 if (*cmd == c)
3656 ++cmd;
3657 }
3658 else
3659 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003660 int flags;
3661
3662 pos = curwin->w_cursor; // save curwin->w_cursor
3663
3664 // When '/' or '?' follows another address, start from
3665 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003666 if (lnum != MAXLNUM)
3667 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003668
3669 // Start a forward search at the end of the line (unless
3670 // before the first line).
3671 // Start a backward search at the start of the line.
3672 // This makes sure we never match in the current
3673 // line, and can match anywhere in the
3674 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003675 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003676 curwin->w_cursor.col = MAXCOL;
3677 else
3678 curwin->w_cursor.col = 0;
3679 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003680 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003681 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003682 {
3683 curwin->w_cursor = pos;
3684 cmd = NULL;
3685 goto error;
3686 }
3687 lnum = curwin->w_cursor.lnum;
3688 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003689 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003690 cmd += searchcmdlen;
3691 }
3692 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003694 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003695 ++cmd;
3696 if (addr_type != ADDR_LINES)
3697 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003698 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003699 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003700 goto error;
3701 }
3702 if (*cmd == '&')
3703 i = RE_SUBST;
3704 else if (*cmd == '?' || *cmd == '/')
3705 i = RE_SEARCH;
3706 else
3707 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003708 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003709 cmd = NULL;
3710 goto error;
3711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003713 if (!skip)
3714 {
3715 /*
3716 * When search follows another address, start from
3717 * there.
3718 */
3719 if (lnum != MAXLNUM)
3720 pos.lnum = lnum;
3721 else
3722 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003724 /*
3725 * Start the search just like for the above
3726 * do_search().
3727 */
3728 if (*cmd != '?')
3729 pos.col = MAXCOL;
3730 else
3731 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003732 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003733 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003734 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003735 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003736 lnum = pos.lnum;
3737 else
3738 {
3739 cmd = NULL;
3740 goto error;
3741 }
3742 }
3743 ++cmd;
3744 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
3746 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003747 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003748 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750
3751 for (;;)
3752 {
3753 cmd = skipwhite(cmd);
3754 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3755 break;
3756
3757 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003758 {
3759 switch (addr_type)
3760 {
3761 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003762 case ADDR_OTHER:
3763 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003764 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003765 break;
3766 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003767 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003768 break;
3769 case ADDR_ARGUMENTS:
3770 lnum = curwin->w_arg_idx + 1;
3771 break;
3772 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003773 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003774 lnum = curbuf->b_fnum;
3775 break;
3776 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003777 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003778 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003779 case ADDR_TABS_RELATIVE:
3780 lnum = 1;
3781 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003782 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003783#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003784 lnum = qf_get_cur_idx(eap);
3785#endif
3786 break;
3787 case ADDR_QUICKFIX_VALID:
3788#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003789 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003790#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003791 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003792 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003793 case ADDR_UNSIGNED:
3794 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003795 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003796 }
3797 }
3798
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003800 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 else
3802 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003803 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 n = 1;
3805 else
3806 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003807
3808 if (addr_type == ADDR_TABS_RELATIVE)
3809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003810 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003811 cmd = NULL;
3812 goto error;
3813 }
3814 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003815 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003816 lnum = compute_buffer_local_count(
3817 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003819 {
3820#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003821 // Relative line addressing, need to adjust for folded lines
3822 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003823 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3824 && address_count >= 2)
3825 (void)hasFolding(lnum, NULL, &lnum);
3826#endif
3827 if (i == '-')
3828 lnum -= n;
3829 else
3830 lnum += n;
3831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833 } while (*cmd == '/' || *cmd == '?');
3834
3835error:
3836 *ptr = cmd;
3837 return lnum;
3838}
3839
3840/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003841 * Get flags from an Ex command argument.
3842 */
3843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003844get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003845{
3846 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3847 {
3848 if (*eap->arg == 'l')
3849 eap->flags |= EXFLAG_LIST;
3850 else if (*eap->arg == 'p')
3851 eap->flags |= EXFLAG_PRINT;
3852 else
3853 eap->flags |= EXFLAG_NR;
3854 eap->arg = skipwhite(eap->arg + 1);
3855 }
3856}
3857
3858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 * Function called for command which is Not Implemented. NI!
3860 */
3861 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003862ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863{
3864 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003865 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866}
3867
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003868#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869/*
3870 * Function called for script command which is Not Implemented. NI!
3871 * Skips over ":perl <<EOF" constructs.
3872 */
3873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003874ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 if (!eap->skip)
3877 ex_ni(eap);
3878 else
3879 vim_free(script_get(eap, eap->arg));
3880}
3881#endif
3882
3883/*
3884 * Check range in Ex command for validity.
3885 * Return NULL when valid, error message when invalid.
3886 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003887 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003888invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003890 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003891
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if ( eap->line1 < 0
3893 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003894 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003895 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003896
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003897 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003898 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003899 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003900 {
3901 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003902 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003903#ifdef FEAT_DIFF
3904 + (eap->cmdidx == CMD_diffget)
3905#endif
3906 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003907 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003908 break;
3909 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003910 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003911 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003912 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003913 break;
3914 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003915 // Only a boundary check, not whether the buffers actually
3916 // exist.
3917 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003918 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003919 break;
3920 case ADDR_LOADED_BUFFERS:
3921 buf = firstbuf;
3922 while (buf->b_ml.ml_mfp == NULL)
3923 {
3924 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003925 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003926 buf = buf->b_next;
3927 }
3928 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003929 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003930 buf = lastbuf;
3931 while (buf->b_ml.ml_mfp == NULL)
3932 {
3933 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003934 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003935 buf = buf->b_prev;
3936 }
3937 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003938 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003939 break;
3940 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003941 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003942 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003943 break;
3944 case ADDR_TABS:
3945 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003946 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003947 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003948 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003949 case ADDR_OTHER:
3950 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003951 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003952 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003953#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003954 // No error for value that is too big, will use the last entry.
3955 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003956 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003957#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003958 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003959 case ADDR_QUICKFIX_VALID:
3960#ifdef FEAT_QUICKFIX
3961 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3962 || eap->line2 < 0)
3963 return _(e_invrange);
3964#endif
3965 break;
3966 case ADDR_UNSIGNED:
3967 if (eap->line2 < 0)
3968 return _(e_invrange);
3969 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003970 case ADDR_NONE:
3971 // Will give an error elsewhere.
3972 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003973 }
3974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return NULL;
3976}
3977
3978/*
3979 * Correct the range for zero line number, if required.
3980 */
3981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003982correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003984 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
3986 if (eap->line1 == 0)
3987 eap->line1 = 1;
3988 if (eap->line2 == 0)
3989 eap->line2 = 1;
3990 }
3991}
3992
Bram Moolenaar748bf032005-02-02 23:04:36 +00003993#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00003994/*
3995 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3996 * pattern. Otherwise return eap->arg.
3997 */
3998 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003999skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004000{
4001 char_u *p = eap->arg;
4002
Bram Moolenaara37420f2006-02-04 22:37:47 +00004003 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4004 || eap->cmdidx == CMD_vimgrepadd
4005 || eap->cmdidx == CMD_lvimgrepadd
4006 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004007 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004008 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004009 if (p == NULL)
4010 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004011 }
4012 return p;
4013}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004014
4015/*
4016 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4017 * in the command line, so that things like % get expanded.
4018 */
4019 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004020replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004021{
4022 char_u *new_cmdline;
4023 char_u *program;
4024 char_u *pos;
4025 char_u *ptr;
4026 int len;
4027 int i;
4028
4029 /*
4030 * Don't do it when ":vimgrep" is used for ":grep".
4031 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004032 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4033 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4034 || eap->cmdidx == CMD_grepadd
4035 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004036 && !grep_internal(eap->cmdidx))
4037 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004038 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4039 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004040 {
4041 if (*curbuf->b_p_gp == NUL)
4042 program = p_gp;
4043 else
4044 program = curbuf->b_p_gp;
4045 }
4046 else
4047 {
4048 if (*curbuf->b_p_mp == NUL)
4049 program = p_mp;
4050 else
4051 program = curbuf->b_p_mp;
4052 }
4053
4054 p = skipwhite(p);
4055
4056 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4057 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004058 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004059 i = 1;
4060 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4061 ++i;
4062 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004063 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004064 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004065 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004066 ptr = new_cmdline;
4067 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4068 {
4069 i = (int)(pos - program);
4070 STRNCPY(ptr, program, i);
4071 STRCPY(ptr += i, p);
4072 ptr += len;
4073 program = pos + 2;
4074 }
4075 STRCPY(ptr, program);
4076 }
4077 else
4078 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004079 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004080 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004081 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004082 STRCPY(new_cmdline, program);
4083 STRCAT(new_cmdline, " ");
4084 STRCAT(new_cmdline, p);
4085 }
4086 msg_make(p);
4087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004088 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004089 vim_free(*cmdlinep);
4090 *cmdlinep = new_cmdline;
4091 p = new_cmdline;
4092 }
4093 return p;
4094}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004095#endif
4096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097/*
4098 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004099 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * Return FAIL for failure, OK otherwise.
4101 */
4102 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004103expand_filename(
4104 exarg_T *eap,
4105 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004106 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004108 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 char_u *repl;
4110 int srclen;
4111 char_u *p;
4112 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004113 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
Bram Moolenaar748bf032005-02-02 23:04:36 +00004115#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004116 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004117 p = skip_grep_pat(eap);
4118#else
4119 p = eap->arg;
4120#endif
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 /*
4123 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4124 * the file name contains a wildcard it should not cause expanding.
4125 * (it will be expanded anyway if there is a wildcard before replacing).
4126 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004127 has_wildcards = mch_has_wildcard(p);
4128 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004130#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004131 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004132 if (p[0] == '`' && p[1] == '=')
4133 {
4134 p += 2;
4135 (void)skip_expr(&p);
4136 if (*p == '`')
4137 ++p;
4138 continue;
4139 }
4140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 /*
4142 * Quick check if this cannot be the start of a special string.
4143 * Also removes backslash before '%', '#' and '<'.
4144 */
4145 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4146 {
4147 ++p;
4148 continue;
4149 }
4150
4151 /*
4152 * Try to find a match at this position.
4153 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004154 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4155 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004156 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004158 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
4160 p += srclen;
4161 continue;
4162 }
4163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004164 // Wildcards won't be expanded below, the replacement is taken
4165 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004166 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4167 {
4168 char_u *l = repl;
4169
4170 repl = expand_env_save(repl);
4171 vim_free(l);
4172 }
4173
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004174 // Need to escape white space et al. with a backslash.
4175 // Don't do this for:
4176 // - replacement that already has been escaped: "##"
4177 // - shell commands (may have to use quotes instead).
4178 // - non-unix systems when there is a single argument (spaces don't
4179 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004181 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 && eap->cmdidx != CMD_grep
4184 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004185 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004186 && eap->cmdidx != CMD_lgrep
4187 && eap->cmdidx != CMD_lgrepadd
4188 && eap->cmdidx != CMD_lmake
4189 && eap->cmdidx != CMD_make
4190 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004192 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#endif
4194 )
4195 {
4196 char_u *l;
4197#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004198 // Don't escape a backslash here, because rem_backslash() doesn't
4199 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 static char_u *nobslash = (char_u *)" \t\"|";
4201# define ESCAPE_CHARS nobslash
4202#else
4203# define ESCAPE_CHARS escape_chars
4204#endif
4205
4206 for (l = repl; *l; ++l)
4207 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4208 {
4209 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4210 if (l != NULL)
4211 {
4212 vim_free(repl);
4213 repl = l;
4214 }
4215 break;
4216 }
4217 }
4218
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004219 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004220 if ((eap->usefilter || eap->cmdidx == CMD_bang
4221 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004222 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
4224 char_u *l;
4225
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004226 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (l != NULL)
4228 {
4229 vim_free(repl);
4230 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232 }
4233
4234 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4235 vim_free(repl);
4236 if (p == NULL)
4237 return FAIL;
4238 }
4239
4240 /*
4241 * One file argument: Expand wildcards.
4242 * Don't do this with ":r !command" or ":w !command".
4243 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004244 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 /*
4247 * May do this twice:
4248 * 1. Replace environment variables.
4249 * 2. Replace any other wildcards, remove backslashes.
4250 */
4251 for (n = 1; n <= 2; ++n)
4252 {
4253 if (n == 2)
4254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 /*
4256 * Halve the number of backslashes (this is Vi compatible).
4257 * For Unix and OS/2, when wildcards are expanded, this is
4258 * done by ExpandOne() below.
4259 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004260#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (!has_wildcards)
4262#endif
4263 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265
4266 if (has_wildcards)
4267 {
4268 if (n == 1)
4269 {
4270 /*
4271 * First loop: May expand environment variables. This
4272 * can be done much faster with expand_env() than with
4273 * something else (e.g., calling a shell).
4274 * After expanding environment variables, check again
4275 * if there are still wildcards present.
4276 */
4277 if (vim_strchr(eap->arg, '$') != NULL
4278 || vim_strchr(eap->arg, '~') != NULL)
4279 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004280 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004281 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 has_wildcards = mch_has_wildcard(NameBuff);
4283 p = NameBuff;
4284 }
4285 else
4286 p = NULL;
4287 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004288 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 {
4290 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004291 int options = WILD_LIST_NOTFOUND
4292 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
4294 ExpandInit(&xpc);
4295 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004296 if (p_wic)
4297 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004299 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (p == NULL)
4301 return FAIL;
4302 }
4303 if (p != NULL)
4304 {
4305 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4306 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004307 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 vim_free(p);
4309 }
4310 }
4311 }
4312 }
4313 return OK;
4314}
4315
4316/*
4317 * Replace part of the command line, keeping eap->cmd, eap->arg and
4318 * eap->nextcmd correct.
4319 * "src" points to the part that is to be replaced, of length "srclen".
4320 * "repl" is the replacement string.
4321 * Returns a pointer to the character after the replaced string.
4322 * Returns NULL for failure.
4323 */
4324 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004325repl_cmdline(
4326 exarg_T *eap,
4327 char_u *src,
4328 int srclen,
4329 char_u *repl,
4330 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331{
4332 int len;
4333 int i;
4334 char_u *new_cmdline;
4335
4336 /*
4337 * The new command line is build in new_cmdline[].
4338 * First allocate it.
4339 * Careful: a "+cmd" argument may have been NUL terminated.
4340 */
4341 len = (int)STRLEN(repl);
4342 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004343 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004344 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004345 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004346 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 /*
4349 * Copy the stuff before the expanded part.
4350 * Copy the expanded stuff.
4351 * Copy what came after the expanded part.
4352 * Copy the next commands, if there are any.
4353 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004354 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004356
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004358 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004360 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004362 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
4364 i = (int)STRLEN(new_cmdline) + 1;
4365 STRCPY(new_cmdline + i, eap->nextcmd);
4366 eap->nextcmd = new_cmdline + i;
4367 }
4368 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4369 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4370 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4371 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4372 vim_free(*cmdlinep);
4373 *cmdlinep = new_cmdline;
4374
4375 return src;
4376}
4377
4378/*
4379 * Check for '|' to separate commands and '"' to start comments.
4380 */
4381 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004382separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 char_u *p;
4385
Bram Moolenaar86b68352004-12-27 21:59:20 +00004386#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004387 p = skip_grep_pat(eap);
4388#else
4389 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004390#endif
4391
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004392 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
4394 if (*p == Ctrl_V)
4395 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004396 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004397 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004400 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004401 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 break;
4403 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004404
4405#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004406 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004407 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004408 {
4409 p += 2;
4410 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004411 }
4412#endif
4413
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004414 // Check for '"': start of comment or '|': next command
4415 // :@" and :*" do not start a comment!
4416 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004417 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4419 || p != eap->arg)
4420 && (eap->cmdidx != CMD_redir
4421 || p != eap->arg + 1 || p[-1] != '@'))
4422 || *p == '|' || *p == '\n')
4423 {
4424 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004425 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 * AND 'b' is present in 'cpoptions'.
4427 */
4428 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004429 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004431 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 --p;
4433 }
4434 else
4435 {
4436 eap->nextcmd = check_nextcmd(p);
4437 *p = NUL;
4438 break;
4439 }
4440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004442
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 del_trailing_spaces(eap->arg);
4445}
4446
4447/*
4448 * get + command from ex argument
4449 */
4450 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004451getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452{
4453 char_u *arg = *argp;
4454 char_u *command = NULL;
4455
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004456 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
4458 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004459 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 command = dollar_command;
4461 else
4462 {
4463 command = arg;
4464 arg = skip_cmd_arg(command, TRUE);
4465 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004466 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004469 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 *argp = arg;
4471 }
4472 return command;
4473}
4474
4475/*
4476 * Find end of "+command" argument. Skip over "\ " and "\\".
4477 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004478 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004479skip_cmd_arg(
4480 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004481 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
4483 while (*p && !vim_isspace(*p))
4484 {
4485 if (*p == '\\' && p[1] != NUL)
4486 {
4487 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004488 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 else
4490 ++p;
4491 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004492 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 return p;
4495}
4496
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004497 int
4498get_bad_opt(char_u *p, exarg_T *eap)
4499{
4500 if (STRICMP(p, "keep") == 0)
4501 eap->bad_char = BAD_KEEP;
4502 else if (STRICMP(p, "drop") == 0)
4503 eap->bad_char = BAD_DROP;
4504 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4505 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004506 else
4507 return FAIL;
4508 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004509}
4510
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511/*
4512 * Get "++opt=arg" argument.
4513 * Return FAIL or OK.
4514 */
4515 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004516getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
4518 char_u *arg = eap->arg + 2;
4519 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004520 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004523 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4525 {
4526 if (*arg == 'n')
4527 {
4528 arg += 2;
4529 eap->force_bin = FORCE_NOBIN;
4530 }
4531 else
4532 eap->force_bin = FORCE_BIN;
4533 if (!checkforcmd(&arg, "binary", 3))
4534 return FAIL;
4535 eap->arg = skipwhite(arg);
4536 return OK;
4537 }
4538
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004539 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004540 if (STRNCMP(arg, "edit", 4) == 0)
4541 {
4542 eap->read_edit = TRUE;
4543 eap->arg = skipwhite(arg + 4);
4544 return OK;
4545 }
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (STRNCMP(arg, "ff", 2) == 0)
4548 {
4549 arg += 2;
4550 pp = &eap->force_ff;
4551 }
4552 else if (STRNCMP(arg, "fileformat", 10) == 0)
4553 {
4554 arg += 10;
4555 pp = &eap->force_ff;
4556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else if (STRNCMP(arg, "enc", 3) == 0)
4558 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004559 if (STRNCMP(arg, "encoding", 8) == 0)
4560 arg += 8;
4561 else
4562 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 pp = &eap->force_enc;
4564 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004565 else if (STRNCMP(arg, "bad", 3) == 0)
4566 {
4567 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004568 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
4571 if (pp == NULL || *arg != '=')
4572 return FAIL;
4573
4574 ++arg;
4575 *pp = (int)(arg - eap->cmd);
4576 arg = skip_cmd_arg(arg, FALSE);
4577 eap->arg = skipwhite(arg);
4578 *arg = NUL;
4579
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (pp == &eap->force_ff)
4581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4583 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004584 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004586 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004588 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4590 *p = TOLOWER_ASC(*p);
4591 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004592 else
4593 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004594 // Check ++bad= argument. Must be a single-byte character, "keep" or
4595 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004596 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004597 return FAIL;
4598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
4600 return OK;
4601}
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004607 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 * directory for security reasons.
4609 */
4610 if (secure)
4611 {
4612 secure = 2;
4613 eap->errmsg = e_curdir;
4614 }
4615 else if (eap->cmdidx == CMD_autocmd)
4616 do_autocmd(eap->arg, eap->forceit);
4617 else
4618 do_augroup(eap->arg, eap->forceit);
4619}
4620
4621/*
4622 * ":doautocmd": Apply the automatic commands to the current buffer.
4623 */
4624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004625ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004627 char_u *arg = eap->arg;
4628 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004629 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004630
Bram Moolenaar1610d052016-06-09 22:53:01 +02004631 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004632 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004633 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004634 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637/*
4638 * :[N]bunload[!] [N] [bufname] unload buffer
4639 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4640 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4641 */
4642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004643ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004645 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004646 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 eap->errmsg = do_bufdel(
4648 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4649 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4650 : DOBUF_UNLOAD, eap->arg,
4651 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4652}
4653
4654/*
4655 * :[N]buffer [N] to buffer N
4656 * :[N]sbuffer [N] to buffer N
4657 */
4658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004659ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004661 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004662 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (*eap->arg)
4664 eap->errmsg = e_trailing;
4665 else
4666 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004667 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4669 else
4670 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004671 if (eap->do_ecmd_cmd != NULL)
4672 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674}
4675
4676/*
4677 * :[N]bmodified [N] to next mod. buffer
4678 * :[N]sbmodified [N] to next mod. buffer
4679 */
4680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004681ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682{
4683 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004684 if (eap->do_ecmd_cmd != NULL)
4685 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686}
4687
4688/*
4689 * :[N]bnext [N] to next buffer
4690 * :[N]sbnext [N] split and to next buffer
4691 */
4692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004693ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004695 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004696 return;
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004699 if (eap->do_ecmd_cmd != NULL)
4700 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701}
4702
4703/*
4704 * :[N]bNext [N] to previous buffer
4705 * :[N]bprevious [N] to previous buffer
4706 * :[N]sbNext [N] split and to previous buffer
4707 * :[N]sbprevious [N] split and to previous buffer
4708 */
4709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004710ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004712 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004713 return;
4714
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004716 if (eap->do_ecmd_cmd != NULL)
4717 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718}
4719
4720/*
4721 * :brewind to first buffer
4722 * :bfirst to first buffer
4723 * :sbrewind split and to first buffer
4724 * :sbfirst split and to first buffer
4725 */
4726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004727ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004729 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004730 return;
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004733 if (eap->do_ecmd_cmd != NULL)
4734 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735}
4736
4737/*
4738 * :blast to last buffer
4739 * :sblast split and to last buffer
4740 */
4741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004742ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004744 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004745 return;
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004748 if (eap->do_ecmd_cmd != NULL)
4749 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004753ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 return (c == NUL || c == '|' || c == '"' || c == '\n');
4756}
4757
4758#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4759 || defined(PROTO)
4760/*
4761 * Return the next command, after the first '|' or '\n'.
4762 * Return NULL if not found.
4763 */
4764 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004765find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
4767 while (*p != '|' && *p != '\n')
4768 {
4769 if (*p == NUL)
4770 return NULL;
4771 ++p;
4772 }
4773 return (p + 1);
4774}
4775#endif
4776
4777/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004778 * Check if *p is a separator between Ex commands, skipping over white space.
4779 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 */
4781 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004782check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004784 char_u *s = skipwhite(p);
4785
4786 if (*s == '|' || *s == '\n')
4787 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 else
4789 return NULL;
4790}
4791
4792/*
4793 * - if there are more files to edit
4794 * - and this is the last window
4795 * - and forceit not used
4796 * - and not repeated twice on a row
4797 * return FAIL and give error message if 'message' TRUE
4798 * return OK otherwise
4799 */
4800 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004801check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004802 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004803 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
4805 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4806
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004807 if (!forceit && only_one_window()
4808 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
4810 if (message)
4811 {
4812#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4813 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4814 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004815 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004817 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4818 NGETTEXT("%d more file to edit. Quit anyway?",
4819 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4821 return OK;
4822 return FAIL;
4823 }
4824#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004825 semsg(NGETTEXT("E173: %d more file to edit",
4826 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004827 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 return FAIL;
4830 }
4831 return OK;
4832}
4833
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834/*
4835 * Function given to ExpandGeneric() to obtain the list of command names.
4836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
4840 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 return cmdnames[idx].cmd_name;
4843}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004846ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847{
Bram Moolenaare6850792010-05-14 15:28:44 +02004848 if (*eap->arg == NUL)
4849 {
4850#ifdef FEAT_EVAL
4851 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4852 char_u *p = NULL;
4853
4854 if (expr != NULL)
4855 {
4856 ++emsg_off;
4857 p = eval_to_string(expr, NULL, FALSE);
4858 --emsg_off;
4859 vim_free(expr);
4860 }
4861 if (p != NULL)
4862 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004863 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004864 vim_free(p);
4865 }
4866 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004867 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004868#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004869 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004870#endif
4871 }
4872 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004873 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004874
4875#ifdef FEAT_VTP
4876 else if (has_vtp_working())
4877 {
4878 // background color change requires clear + redraw
4879 update_screen(CLEAR);
4880 redrawcmd();
4881 }
4882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883}
4884
4885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004886ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
4888 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004889 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 do_highlight(eap->arg, eap->forceit, FALSE);
4891}
4892
4893
4894/*
4895 * Call this function if we thought we were going to exit, but we won't
4896 * (because of an error). May need to restore the terminal mode.
4897 */
4898 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004899not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900{
4901 exiting = FALSE;
4902 settmode(TMODE_RAW);
4903}
4904
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004905 static int
4906before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4907{
4908 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4909
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004910 // Bail out when autocommands closed the window.
4911 // Refuse to quit when the buffer in the last window is being closed (can
4912 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004913 if (!win_valid(wp)
4914 || curbuf_locked()
4915 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4916 return TRUE;
4917
4918 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4919 {
4920 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004921 // Refuse to quit when locked or when the window was closed or the
4922 // buffer in the last window is being closed (can only happen in
4923 // autocommands).
4924 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004925 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4926 return TRUE;
4927 }
4928
4929 return FALSE;
4930}
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004933 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004934 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004935 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004937 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004938ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004940 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#ifdef FEAT_CMDWIN
4943 if (cmdwin_type != 0)
4944 {
4945 cmdwin_result = Ctrl_C;
4946 return;
4947 }
4948#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004949 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004950 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004951 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004952 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004953 return;
4954 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004955 if (eap->addr_count > 0)
4956 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004957 int wnr = eap->line2;
4958
4959 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4960 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004961 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004962 }
4963 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004964 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004966 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004967 if (curbuf_locked())
4968 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004969
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004970 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004971 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004972 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974#ifdef FEAT_NETBEANS_INTG
4975 netbeansForcedQuit = eap->forceit;
4976#endif
4977
4978 /*
4979 * If there are more files or windows we won't exit.
4980 */
4981 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4982 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004983 if ((!buf_hide(wp->w_buffer)
4984 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004985 | (eap->forceit ? CCGD_FORCEIT : 0)
4986 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004988 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 not_exiting();
4991 }
4992 else
4993 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004994 // quit last window
4995 // Note: only_one_window() returns true, even so a help window is
4996 // still open. In that case only quit, if no address has been
4997 // specified. Example:
4998 // :h|wincmd w|1q - don't quit
4999 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005000 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005002 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005003#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005005#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005006 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005007 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009}
5010
5011/*
5012 * ":cquit".
5013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005015ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005017 // this does not always pass on the exit code to the Manx compiler. why?
5018 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019}
5020
5021/*
5022 * ":qall": try to quit all windows
5023 */
5024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005025ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026{
5027# ifdef FEAT_CMDWIN
5028 if (cmdwin_type != 0)
5029 {
5030 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005031 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 else
5033 cmdwin_result = K_XF2;
5034 return;
5035 }
5036# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005038 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005039 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005040 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005041 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005042 return;
5043 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005044
5045 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005046 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005049 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 getout(0);
5051 not_exiting();
5052}
5053
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054/*
5055 * ":close": close current window, unless it is the last one
5056 */
5057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005058ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005060 win_T *win;
5061 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005062#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005064 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005066#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005067 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005068 {
5069 if (eap->addr_count == 0)
5070 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005071 else
5072 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005073 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005074 {
5075 winnr++;
5076 if (winnr == eap->line2)
5077 break;
5078 }
5079 if (win == NULL)
5080 win = lastwin;
5081 ex_win_close(eap->forceit, win, NULL);
5082 }
5083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084}
5085
Bram Moolenaar4033c552017-09-16 20:54:51 +02005086#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005087/*
5088 * ":pclose": Close any preview window.
5089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005091ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005092{
5093 win_T *win;
5094
Bram Moolenaar79648732019-07-18 21:43:07 +02005095 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005096 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005097 if (win->w_p_pvw)
5098 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005099 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005100 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005101 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005102# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005103 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005104 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005105# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005106}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005107#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005108
Bram Moolenaarf740b292006-02-16 22:11:02 +00005109/*
5110 * Close window "win" and take care of handling closing the last window for a
5111 * modified buffer.
5112 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005114ex_win_close(
5115 int forceit,
5116 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005117 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
5119 int need_hide;
5120 buf_T *buf = win->w_buffer;
5121
5122 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005123 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005125#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if ((p_confirm || cmdmod.confirm) && p_write)
5127 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005128 bufref_T bufref;
5129
5130 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005132 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 return;
5134 need_hide = FALSE;
5135 }
5136 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005139 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 return;
5141 }
5142 }
5143
Bram Moolenaar4033c552017-09-16 20:54:51 +02005144#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005146#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005147
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005148 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005149 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005150 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005151 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005152 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153}
5154
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005156 * Handle the argument for a tabpage related ex command.
5157 * Returns a tabpage number.
5158 * When an error is encountered then eap->errmsg is set.
5159 */
5160 static int
5161get_tabpage_arg(exarg_T *eap)
5162{
5163 int tab_number;
5164 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5165
5166 if (eap->arg && *eap->arg != NUL)
5167 {
5168 char_u *p = eap->arg;
5169 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005170 int relative = 0; // argument +N/-N means: go to N places to the
5171 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005172
5173 if (*p == '-')
5174 {
5175 relative = -1;
5176 p++;
5177 }
5178 else if (*p == '+')
5179 {
5180 relative = 1;
5181 p++;
5182 }
5183
5184 p_save = p;
5185 tab_number = getdigits(&p);
5186
5187 if (relative == 0)
5188 {
5189 if (STRCMP(p, "$") == 0)
5190 tab_number = LAST_TAB_NR;
5191 else if (p == p_save || *p_save == '-' || *p != NUL
5192 || tab_number > LAST_TAB_NR)
5193 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005194 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005195 eap->errmsg = e_invarg;
5196 goto theend;
5197 }
5198 }
5199 else
5200 {
5201 if (*p_save == NUL)
5202 tab_number = 1;
5203 else if (p == p_save || *p_save == '-' || *p != NUL
5204 || tab_number == 0)
5205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005206 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005207 eap->errmsg = e_invarg;
5208 goto theend;
5209 }
5210 tab_number = tab_number * relative + tabpage_index(curtab);
5211 if (!unaccept_arg0 && relative == -1)
5212 --tab_number;
5213 }
5214 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5215 eap->errmsg = e_invarg;
5216 }
5217 else if (eap->addr_count > 0)
5218 {
5219 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005220 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005221 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005222 tab_number = 0;
5223 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005224 else
5225 {
5226 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005227 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005228 {
5229 --tab_number;
5230 if (tab_number < unaccept_arg0)
5231 eap->errmsg = e_invarg;
5232 }
5233 }
5234 }
5235 else
5236 {
5237 switch (eap->cmdidx)
5238 {
5239 case CMD_tabnext:
5240 tab_number = tabpage_index(curtab) + 1;
5241 if (tab_number > LAST_TAB_NR)
5242 tab_number = 1;
5243 break;
5244 case CMD_tabmove:
5245 tab_number = LAST_TAB_NR;
5246 break;
5247 default:
5248 tab_number = tabpage_index(curtab);
5249 }
5250 }
5251
5252theend:
5253 return tab_number;
5254}
5255
5256/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005257 * ":tabclose": close current tab page, unless it is the last one.
5258 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 */
5260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005261ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005263 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005264 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005265
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005266# ifdef FEAT_CMDWIN
5267 if (cmdwin_type != 0)
5268 cmdwin_result = K_IGNORE;
5269 else
5270# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005271 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005272 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005275 tab_number = get_tabpage_arg(eap);
5276 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005277 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005278 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005279 if (tp == NULL)
5280 {
5281 beep_flush();
5282 return;
5283 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005284 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005285 {
5286 tabpage_close_other(tp, eap->forceit);
5287 return;
5288 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005289 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005290 tabpage_close(eap->forceit);
5291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005293}
5294
5295/*
5296 * ":tabonly": close all tab pages except the current one
5297 */
5298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005299ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005300{
5301 tabpage_T *tp;
5302 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005303 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005304
5305# ifdef FEAT_CMDWIN
5306 if (cmdwin_type != 0)
5307 cmdwin_result = K_IGNORE;
5308 else
5309# endif
5310 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005311 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005312 else
5313 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005314 tab_number = get_tabpage_arg(eap);
5315 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005316 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005317 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005318 // Repeat this up to a 1000 times, because autocommands may
5319 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005320 for (done = 0; done < 1000; ++done)
5321 {
5322 FOR_ALL_TABPAGES(tp)
5323 if (tp->tp_topframe != topframe)
5324 {
5325 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005326 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005327 if (valid_tabpage(tp))
5328 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005329 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005330 break;
5331 }
5332 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005333 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005334 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005335 }
5336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
5339/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005340 * Close the current tab page.
5341 */
5342 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005343tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005344{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005345 // First close all the windows but the current one. If that worked then
5346 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005347 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005348 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005349 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005350 ex_win_close(forceit, curwin, NULL);
5351# ifdef FEAT_GUI
5352 need_mouse_correct = TRUE;
5353# endif
5354}
5355
5356/*
5357 * Close tab page "tp", which is not the current tab page.
5358 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005359 * Also takes care of the tab pages line disappearing when closing the
5360 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005361 */
5362 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005363tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005364{
5365 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005366 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005367 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005369 // Limit to 1000 windows, autocommands may add a window while we close
5370 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005371 while (++done < 1000)
5372 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005373 wp = tp->tp_firstwin;
5374 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005375
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 // Autocommands may delete the tab page under our fingers and we may
5377 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005378 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005379 break;
5380 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005381
Bram Moolenaar29323592016-07-24 22:04:11 +02005382 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005383
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005384 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005385 if (h != tabline_height())
5386 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005387}
5388
5389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 * ":only".
5391 */
5392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005393ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005395 win_T *wp;
5396 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397# ifdef FEAT_GUI
5398 need_mouse_correct = TRUE;
5399# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005400 if (eap->addr_count > 0)
5401 {
5402 wnr = eap->line2;
5403 for (wp = firstwin; --wnr > 0; )
5404 {
5405 if (wp->w_next == NULL)
5406 break;
5407 else
5408 wp = wp->w_next;
5409 }
5410 win_goto(wp);
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 close_others(TRUE, eap->forceit);
5413}
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005416ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005418 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005419 if (!eap->skip)
5420 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005421#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005422 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005423#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005424 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005425 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005426 else
5427 {
5428 int winnr = 0;
5429 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005430
Bram Moolenaar2256c992016-11-15 21:17:07 +01005431 FOR_ALL_WINDOWS(win)
5432 {
5433 winnr++;
5434 if (winnr == eap->line2)
5435 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005436 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005437 if (win == NULL)
5438 win = lastwin;
5439 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
5442}
5443
5444/*
5445 * ":stop" and ":suspend": Suspend Vim.
5446 */
5447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005448ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
5450 /*
5451 * Disallow suspending for "rvim".
5452 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005453 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
5455 if (!eap->forceit)
5456 autowrite_all();
5457 windgoto((int)Rows - 1, 0);
5458 out_char('\n');
5459 out_flush();
5460 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005461 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005463 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005465 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466#ifdef FEAT_TITLE
5467 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005468 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469#endif
5470 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005471 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005473 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
5475}
5476
5477/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005478 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
5480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005481ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483#ifdef FEAT_CMDWIN
5484 if (cmdwin_type != 0)
5485 {
5486 cmdwin_result = Ctrl_C;
5487 return;
5488 }
5489#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005490 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005491 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005492 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005493 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005494 return;
5495 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005496
5497 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005498 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
5500 /*
5501 * if more files or windows we won't exit
5502 */
5503 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5504 exiting = TRUE;
5505 if ( ((eap->cmdidx == CMD_wq
5506 || curbufIsChanged())
5507 && do_write(eap) == FAIL)
5508 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005509 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
5511 not_exiting();
5512 }
5513 else
5514 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005515 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005517 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518# ifdef FEAT_GUI
5519 need_mouse_correct = TRUE;
5520# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005521 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005522 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
5524}
5525
5526/*
5527 * ":print", ":list", ":number".
5528 */
5529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005532 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005533 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005534 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005536 for ( ;!got_int; ui_breakcheck())
5537 {
5538 print_line(eap->line1,
5539 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5540 || (eap->flags & EXFLAG_NR)),
5541 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5542 if (++eap->line1 > eap->line2)
5543 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005544 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005545 }
5546 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005547 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005548 curwin->w_cursor.lnum = eap->line2;
5549 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553}
5554
5555#ifdef FEAT_BYTEOFF
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 goto_byte(eap->line2);
5560}
5561#endif
5562
5563/*
5564 * ":shell".
5565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005567ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 do_shell(NULL, 0);
5570}
5571
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005572#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005574static int drop_busy = FALSE;
5575static int drop_filec;
5576static char_u **drop_filev = NULL;
5577static int drop_split;
5578static void (*drop_callback)(void *);
5579static void *drop_cookie;
5580
5581 static void
5582handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 exarg_T ea;
5585 int save_msg_scroll = msg_scroll;
5586
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005587 // Setting the argument list may cause screen updates and being called
5588 // recursively. Avoid that by setting drop_busy.
5589 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005591 // Check whether the current buffer is changed. If so, we will need
5592 // to split the current window or data could be lost.
5593 // We don't need to check if the 'hidden' option is set, as in this
5594 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005595 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 {
5597 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005598 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 --emsg_off;
5600 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005601 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 if (win_split(0, 0) == FAIL)
5604 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005605 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005607 // When splitting the window, create a new alist. Otherwise the
5608 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 alist_unlink(curwin->w_alist);
5610 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
5612
5613 /*
5614 * Set up the new argument list.
5615 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005616 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618 /*
5619 * Move to the first file.
5620 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005621 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 vim_memset(&ea, 0, sizeof(ea));
5623 ea.cmd = (char_u *)"next";
5624 do_argfile(&ea, 0);
5625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005626 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5627 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 need_start_insertmode = FALSE;
5629
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005630 // Restore msg_scroll, otherwise a following command may cause scrolling
5631 // unexpectedly. The screen will be redrawn by the caller, thus
5632 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005634
5635 if (drop_callback != NULL)
5636 drop_callback(drop_cookie);
5637
5638 drop_filev = NULL;
5639 drop_busy = FALSE;
5640}
5641
5642/*
5643 * Handle a file drop. The code is here because a drop is *nearly* like an
5644 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005645 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005646 * code is very similar to :args and hence needs access to a lot of the static
5647 * functions in this file.
5648 *
5649 * The "filev" list must have been allocated using alloc(), as should each item
5650 * in the list. This function takes over responsibility for freeing the "filev"
5651 * list.
5652 */
5653 void
5654handle_drop(
5655 int filec, // the number of files dropped
5656 char_u **filev, // the list of files dropped
5657 int split, // force splitting the window
5658 void (*callback)(void *), // to be called after setting the argument
5659 // list
5660 void *cookie) // argument for "callback" (allocated)
5661{
5662 // Cannot handle recursive drops, finish the pending one.
5663 if (drop_busy)
5664 {
5665 FreeWild(filec, filev);
5666 vim_free(cookie);
5667 return;
5668 }
5669
5670 // When calling handle_drop() more than once in a row we only use the last
5671 // one.
5672 if (drop_filev != NULL)
5673 {
5674 FreeWild(drop_filec, drop_filev);
5675 vim_free(drop_cookie);
5676 }
5677
5678 drop_filec = filec;
5679 drop_filev = filev;
5680 drop_split = split;
5681 drop_callback = callback;
5682 drop_cookie = cookie;
5683
5684 // Postpone this when:
5685 // - editing the command line
5686 // - not possible to change the current buffer
5687 // - updating the screen
5688 // As it may change buffers and window structures that are in use and cause
5689 // freed memory to be used.
5690 if (text_locked() || curbuf_locked() || updating_screen)
5691 return;
5692
5693 handle_drop_internal();
5694}
5695
5696/*
5697 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5698 * reset: Handle a postponed drop.
5699 */
5700 void
5701handle_any_postponed_drop(void)
5702{
5703 if (!drop_busy && drop_filev != NULL
5704 && !text_locked() && !curbuf_locked() && !updating_screen)
5705 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706}
5707#endif
5708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 * ":preserve".
5711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005713ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005715 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 ml_preserve(curbuf, TRUE);
5717}
5718
5719/*
5720 * ":recover".
5721 */
5722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005723ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005725 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005727 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5728 | CCGD_MULTWIN
5729 | (eap->forceit ? CCGD_FORCEIT : 0)
5730 | CCGD_EXCMD)
5731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 && (*eap->arg == NUL
5733 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005734 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 recoverymode = FALSE;
5736}
5737
5738/*
5739 * Command modifier used in a wrong way.
5740 */
5741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005742ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
5744 eap->errmsg = e_invcmd;
5745}
5746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747/*
5748 * :sview [+command] file split window with new file, read-only
5749 * :split [[+command] file] split window with current or new file
5750 * :vsplit [[+command] file] split window vertically with current or new file
5751 * :new [[+command] file] split window with no or new file
5752 * :vnew [[+command] file] split vertically window with no or new file
5753 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005754 *
5755 * :tabedit open new Tab page with empty window
5756 * :tabedit [+command] file open new Tab page and edit "file"
5757 * :tabnew [[+command] file] just like :tabedit
5758 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 */
5760 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005761ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005763 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005764#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005766#endif
5767#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005769#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005770 int use_tab = eap->cmdidx == CMD_tabedit
5771 || eap->cmdidx == CMD_tabfind
5772 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005774 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005775 return;
5776
Bram Moolenaar4033c552017-09-16 20:54:51 +02005777#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
Bram Moolenaar4033c552017-09-16 20:54:51 +02005781#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005782 // A ":split" in the quickfix window works like ":new". Don't want two
5783 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005784 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 {
5786 if (eap->cmdidx == CMD_split)
5787 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 if (eap->cmdidx == CMD_vsplit)
5789 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaar4033c552017-09-16 20:54:51 +02005793#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005794 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
5796 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5797 FNAME_MESS, TRUE, curbuf->b_ffname);
5798 if (fname == NULL)
5799 goto theend;
5800 eap->arg = fname;
5801 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005802# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005803 else
5804# endif
5805#endif
5806#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 && eap->cmdidx != CMD_new)
5810 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005811 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005812# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005813 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005814# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005815 au_has_group((char_u *)"FileExplorer"))
5816 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005817 // No browsing supported but we do have the file explorer:
5818 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005819 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5820 eap->arg = (char_u *)".";
5821 }
5822 else
5823 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005824 fname = do_browse(0, (char_u *)(use_tab
5825 ? _("Edit File in new tab page")
5826 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005828 if (fname == NULL)
5829 goto theend;
5830 eap->arg = fname;
5831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005833 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005836 /*
5837 * Either open new tab page or split the window.
5838 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005839 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005840 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005841 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5842 : eap->addr_count == 0 ? 0
5843 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005844 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005845 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005846
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005847 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005848 if (curwin != old_curwin
5849 && win_valid(old_curwin)
5850 && old_curwin->w_buffer != curbuf
5851 && !cmdmod.keepalt)
5852 old_curwin->w_alt_fnum = curbuf->b_fnum;
5853 }
5854 }
5855 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5857 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005858 // Reset 'scrollbind' when editing another file, but keep it when
5859 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005861# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005865 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 else
5867 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 do_exedit(eap, old_curwin);
5869 }
5870
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005871# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005875# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876theend:
5877 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005880
5881/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005882 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005883 */
5884 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005885tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005886{
5887 exarg_T ea;
5888
5889 vim_memset(&ea, 0, sizeof(ea));
5890 ea.cmdidx = CMD_tabnew;
5891 ea.cmd = (char_u *)"tabn";
5892 ea.arg = (char_u *)"";
5893 ex_splitview(&ea);
5894}
5895
5896/*
5897 * :tabnext command
5898 */
5899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005900ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005901{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005902 int tab_number;
5903
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005904 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005905 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005906 switch (eap->cmdidx)
5907 {
5908 case CMD_tabfirst:
5909 case CMD_tabrewind:
5910 goto_tabpage(1);
5911 break;
5912 case CMD_tablast:
5913 goto_tabpage(9999);
5914 break;
5915 case CMD_tabprevious:
5916 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005917 if (eap->arg && *eap->arg != NUL)
5918 {
5919 char_u *p = eap->arg;
5920 char_u *p_save = p;
5921
5922 tab_number = getdigits(&p);
5923 if (p == p_save || *p_save == '-' || *p != NUL
5924 || tab_number == 0)
5925 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005926 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005927 eap->errmsg = e_invarg;
5928 return;
5929 }
5930 }
5931 else
5932 {
5933 if (eap->addr_count == 0)
5934 tab_number = 1;
5935 else
5936 {
5937 tab_number = eap->line2;
5938 if (tab_number < 1)
5939 {
5940 eap->errmsg = e_invrange;
5941 return;
5942 }
5943 }
5944 }
5945 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005946 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005947 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005948 tab_number = get_tabpage_arg(eap);
5949 if (eap->errmsg == NULL)
5950 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005951 break;
5952 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005953}
5954
5955/*
5956 * :tabmove command
5957 */
5958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005959ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005960{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005961 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005962
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005963 tab_number = get_tabpage_arg(eap);
5964 if (eap->errmsg == NULL)
5965 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005966}
5967
5968/*
5969 * :tabs command: List tabs and their contents.
5970 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005972ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005973{
5974 tabpage_T *tp;
5975 win_T *wp;
5976 int tabcount = 1;
5977
5978 msg_start();
5979 msg_scroll = TRUE;
5980 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5981 {
5982 msg_putchar('\n');
5983 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005984 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005985 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005986 ui_breakcheck();
5987
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005988 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005989 wp = firstwin;
5990 else
5991 wp = tp->tp_firstwin;
5992 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5993 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005994 msg_putchar('\n');
5995 msg_putchar(wp == curwin ? '>' : ' ');
5996 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005997 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5998 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006000 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006001 else
6002 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6003 IObuff, IOSIZE, TRUE);
6004 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006005 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006006 ui_breakcheck();
6007 }
6008 }
6009}
6010
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011/*
6012 * ":mode": Set screen mode.
6013 * If no argument given, just get the screen size and redraw.
6014 */
6015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006016ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017{
6018 if (*eap->arg == NUL)
6019 shell_resized();
6020 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006021 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022}
6023
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024/*
6025 * ":resize".
6026 * set, increment or decrement current window height
6027 */
6028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006029ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030{
6031 int n;
6032 win_T *wp = curwin;
6033
6034 if (eap->addr_count > 0)
6035 {
6036 n = eap->line2;
6037 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6038 ;
6039 }
6040
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006041# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 if (cmdmod.split & WSP_VERT)
6046 {
6047 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006048 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006049 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 n = 9999;
6051 win_setwidth_win((int)n, wp);
6052 }
6053 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 {
6055 if (*eap->arg == '-' || *eap->arg == '+')
6056 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006057 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 n = 9999;
6059 win_setheight_win((int)n, wp);
6060 }
6061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
6063/*
6064 * ":find [+command] <file>" command.
6065 */
6066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006067ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069#ifdef FEAT_SEARCHPATH
6070 char_u *fname;
6071 int count;
6072
6073 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6074 TRUE, curbuf->b_ffname);
6075 if (eap->addr_count > 0)
6076 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006077 // Repeat finding the file "count" times. This matters when it
6078 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 count = eap->line2;
6080 while (fname != NULL && --count > 0)
6081 {
6082 vim_free(fname);
6083 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6084 FALSE, curbuf->b_ffname);
6085 }
6086 }
6087
6088 if (fname != NULL)
6089 {
6090 eap->arg = fname;
6091#endif
6092 do_exedit(eap, NULL);
6093#ifdef FEAT_SEARCHPATH
6094 vim_free(fname);
6095 }
6096#endif
6097}
6098
6099/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006100 * ":open" simulation: for now just work like ":visual".
6101 */
6102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006103ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006104{
6105 regmatch_T regmatch;
6106 char_u *p;
6107
6108 curwin->w_cursor.lnum = eap->line2;
6109 beginline(BL_SOL | BL_FIX);
6110 if (*eap->arg == '/')
6111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006112 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006113 ++eap->arg;
6114 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6115 *p = NUL;
6116 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6117 if (regmatch.regprog != NULL)
6118 {
6119 regmatch.rm_ic = p_ic;
6120 p = ml_get_curline();
6121 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006122 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006123 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006124 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006125 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006126 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006127 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006128 eap->arg += STRLEN(eap->arg);
6129 }
6130 check_cursor();
6131
6132 eap->cmdidx = CMD_visual;
6133 do_exedit(eap, NULL);
6134}
6135
6136/*
6137 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 */
6139 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006140ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141{
6142 do_exedit(eap, NULL);
6143}
6144
6145/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006146 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006149do_exedit(
6150 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006151 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
6153 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006155 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006157 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6158 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006159 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 /*
6161 * ":vi" command ends Ex mode.
6162 */
6163 if (exmode_active && (eap->cmdidx == CMD_visual
6164 || eap->cmdidx == CMD_view))
6165 {
6166 exmode_active = FALSE;
6167 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006170 if (global_busy)
6171 {
6172 int rd = RedrawingDisabled;
6173 int nwr = no_wait_return;
6174 int ms = msg_scroll;
6175#ifdef FEAT_GUI
6176 int he = hold_gui_events;
6177#endif
6178
6179 if (eap->nextcmd != NULL)
6180 {
6181 stuffReadbuff(eap->nextcmd);
6182 eap->nextcmd = NULL;
6183 }
6184
6185 if (exmode_was != EXMODE_VIM)
6186 settmode(TMODE_RAW);
6187 RedrawingDisabled = 0;
6188 no_wait_return = 0;
6189 need_wait_return = FALSE;
6190 msg_scroll = 0;
6191#ifdef FEAT_GUI
6192 hold_gui_events = 0;
6193#endif
6194 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006195 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006196
6197 main_loop(FALSE, TRUE);
6198
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006199 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006200 RedrawingDisabled = rd;
6201 no_wait_return = nwr;
6202 msg_scroll = ms;
6203#ifdef FEAT_GUI
6204 hold_gui_events = he;
6205#endif
6206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210
6211 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006212 || eap->cmdidx == CMD_tabnew
6213 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006214 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006216 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 setpcmark();
6218 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006219 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6220 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006222 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 || *eap->arg != NUL
6224#ifdef FEAT_BROWSE
6225 || cmdmod.browse
6226#endif
6227 )
6228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006229 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6230 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006231 if (*eap->arg != NUL && curbuf_locked())
6232 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 n = readonlymode;
6235 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6236 readonlymode = TRUE;
6237 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006238 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6239 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 setpcmark();
6241 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6242 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006243 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6245 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6246 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006247 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006249 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006250 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006252 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006254 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 if (old_curwin != NULL)
6256 {
6257 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006258 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006260#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006261 cleanup_T cs;
6262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006263 // Reset the error/interrupt/exception state here so that
6264 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006265 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006266#endif
6267#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006269#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006270 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006271
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006272#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006273 // Restore the error/interrupt/exception state if not
6274 // discarded by a new aborting error, interrupt, or
6275 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006276 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
6279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281 else if (readonlymode && curbuf->b_nwindows == 1)
6282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006283 // When editing an already visited buffer, 'readonly' won't be set
6284 // but the previous value is kept. With ":view" and ":sview" we
6285 // want the file to be readonly, except when another window is
6286 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 curbuf->b_p_ro = TRUE;
6288 }
6289 readonlymode = n;
6290 }
6291 else
6292 {
6293 if (eap->do_ecmd_cmd != NULL)
6294 do_cmdline_cmd(eap->do_ecmd_cmd);
6295#ifdef FEAT_TITLE
6296 n = curwin->w_arg_idx_invalid;
6297#endif
6298 check_arg_idx(curwin);
6299#ifdef FEAT_TITLE
6300 if (n != curwin->w_arg_idx_invalid)
6301 maketitle();
6302#endif
6303 }
6304
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 /*
6306 * if ":split file" worked, set alternate file name in old window to new
6307 * file
6308 */
6309 if (old_curwin != NULL
6310 && *eap->arg != NUL
6311 && curwin != old_curwin
6312 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006313 && old_curwin->w_buffer != curbuf
6314 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317 ex_no_reprint = TRUE;
6318}
6319
6320#ifndef FEAT_GUI
6321/*
6322 * ":gui" and ":gvim" when there is no GUI.
6323 */
6324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006325ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 eap->errmsg = e_nogvim;
6328}
6329#endif
6330
Bram Moolenaar4f974752019-02-17 17:44:42 +01006331#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006333ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
6335 gui_make_tearoff(eap->arg);
6336}
6337#endif
6338
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006339#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6340 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006342ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006344# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6345 if (gui.in_use)
6346 gui_make_popup(eap->arg, eap->forceit);
6347# ifdef FEAT_TERM_POPUP_MENU
6348 else
6349# endif
6350# endif
6351# ifdef FEAT_TERM_POPUP_MENU
6352 pum_make_popup(eap->arg, eap->forceit);
6353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354}
6355#endif
6356
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006358ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359{
6360 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006361 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006363 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364}
6365
6366/*
6367 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6368 * offset.
6369 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006372ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006375 win_T *save_curwin = curwin;
6376 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 long topline;
6378 long y;
6379 linenr_T old_linenr = curwin->w_cursor.lnum;
6380
6381 setpcmark();
6382
6383 /*
6384 * determine max topline
6385 */
6386 if (curwin->w_p_scb)
6387 {
6388 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006389 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
6391 if (wp->w_p_scb && wp->w_buffer)
6392 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006393 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (topline > y)
6395 topline = y;
6396 }
6397 }
6398 if (topline < 1)
6399 topline = 1;
6400 }
6401 else
6402 {
6403 topline = 1;
6404 }
6405
6406
6407 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006408 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006410 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 {
6412 if (curwin->w_p_scb)
6413 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006414 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 y = topline - curwin->w_topline;
6416 if (y > 0)
6417 scrollup(y, TRUE);
6418 else
6419 scrolldown(-y, TRUE);
6420 curwin->w_scbind_pos = topline;
6421 redraw_later(VALID);
6422 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006426 curwin = save_curwin;
6427 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 if (curwin->w_p_scb)
6429 {
6430 did_syncbind = TRUE;
6431 checkpcmark();
6432 if (old_linenr != curwin->w_cursor.lnum)
6433 {
6434 char_u ctrl_o[2];
6435
6436 ctrl_o[0] = Ctrl_O;
6437 ctrl_o[1] = 0;
6438 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6439 }
6440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441}
6442
6443
6444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006445ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006447 int i;
6448 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6449 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006451 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 do_bang(1, eap, FALSE, FALSE, TRUE);
6453 else
6454 {
6455 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6456 return;
6457
6458#ifdef FEAT_BROWSE
6459 if (cmdmod.browse)
6460 {
6461 char_u *browseFile;
6462
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006463 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 NULL, NULL, NULL, curbuf);
6465 if (browseFile != NULL)
6466 {
6467 i = readfile(browseFile, NULL,
6468 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6469 vim_free(browseFile);
6470 }
6471 else
6472 i = OK;
6473 }
6474 else
6475#endif
6476 if (*eap->arg == NUL)
6477 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006478 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 return;
6480 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6481 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6482 }
6483 else
6484 {
6485 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6486 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6487 i = readfile(eap->arg, NULL,
6488 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6489
6490 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006491 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006493#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 if (!aborting())
6495#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006496 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 }
6498 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006499 {
6500 if (empty && exmode_active)
6501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006502 // Delete the empty line that remains. Historically ex does
6503 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006504 if (eap->line2 == 0)
6505 lnum = curbuf->b_ml.ml_line_count;
6506 else
6507 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006508 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006509 {
6510 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006511 if (curwin->w_cursor.lnum > 1
6512 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006513 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006514 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006515 }
6516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 }
6520}
6521
Bram Moolenaarea408852005-06-25 22:49:46 +00006522static char_u *prev_dir = NULL;
6523
6524#if defined(EXITFREE) || defined(PROTO)
6525 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006526free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006527{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006528 VIM_CLEAR(prev_dir);
6529 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006530}
6531#endif
6532
Bram Moolenaarf4258302013-06-02 18:20:17 +02006533/*
6534 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006535 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6536 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006537 */
6538 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006539post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006540{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006541 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006542 // Clear tab local directory for both :cd and :tcd
6543 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006544 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006545 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006546 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006547 // If still in global directory, need to remember current
6548 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006549 if (globaldir == NULL && prev_dir != NULL)
6550 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006551 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006552 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006553 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006554 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006555 curtab->tp_localdir = vim_strsave(NameBuff);
6556 else
6557 curwin->w_localdir = vim_strsave(NameBuff);
6558 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006559 }
6560 else
6561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006562 // We are now in the global directory, no need to remember its
6563 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006564 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006565 }
6566
6567 shorten_fnames(TRUE);
6568}
6569
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006570/*
6571 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6572 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6573 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6574 * Otherwise changes the global directory.
6575 * Returns TRUE if the directory is successfully changed.
6576 */
6577 int
6578changedir_func(
6579 char_u *new_dir,
6580 int forceit,
6581 cdscope_T scope)
6582{
6583 char_u *tofree;
6584 int dir_differs;
6585 int retval = FALSE;
6586
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006587 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006588 return FALSE;
6589
6590 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6591 {
6592 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6593 return FALSE;
6594 }
6595
6596 // ":cd -": Change to previous directory
6597 if (STRCMP(new_dir, "-") == 0)
6598 {
6599 if (prev_dir == NULL)
6600 {
6601 emsg(_("E186: No previous directory"));
6602 return FALSE;
6603 }
6604 new_dir = prev_dir;
6605 }
6606
6607 // Save current directory for next ":cd -"
6608 tofree = prev_dir;
6609 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6610 prev_dir = vim_strsave(NameBuff);
6611 else
6612 prev_dir = NULL;
6613
6614#if defined(UNIX) || defined(VMS)
6615 // for UNIX ":cd" means: go to home directory
6616 if (*new_dir == NUL)
6617 {
6618 // use NameBuff for home directory name
6619# ifdef VMS
6620 char_u *p;
6621
6622 p = mch_getenv((char_u *)"SYS$LOGIN");
6623 if (p == NULL || *p == NUL) // empty is the same as not set
6624 NameBuff[0] = NUL;
6625 else
6626 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6627# else
6628 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6629# endif
6630 new_dir = NameBuff;
6631 }
6632#endif
6633 dir_differs = new_dir == NULL || prev_dir == NULL
6634 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6635 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6636 emsg(_(e_failed));
6637 else
6638 {
6639 char_u *acmd_fname;
6640
6641 post_chdir(scope);
6642
6643 if (dir_differs)
6644 {
6645 if (scope == CDSCOPE_WINDOW)
6646 acmd_fname = (char_u *)"window";
6647 else if (scope == CDSCOPE_TABPAGE)
6648 acmd_fname = (char_u *)"tabpage";
6649 else
6650 acmd_fname = (char_u *)"global";
6651 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6652 curbuf);
6653 }
6654 retval = TRUE;
6655 }
6656 vim_free(tofree);
6657
6658 return retval;
6659}
Bram Moolenaarea408852005-06-25 22:49:46 +00006660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006662 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006664 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006665ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006667 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
6669 new_dir = eap->arg;
6670#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006671 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (*new_dir == NUL)
6673 ex_pwd(NULL);
6674 else
6675#endif
6676 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006677 cdscope_T scope = CDSCOPE_GLOBAL;
6678
6679 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6680 scope = CDSCOPE_WINDOW;
6681 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6682 scope = CDSCOPE_TABPAGE;
6683
6684 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006685 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006686 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006687 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 ex_pwd(eap);
6689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 }
6691}
6692
6693/*
6694 * ":pwd".
6695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006697ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698{
6699 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6700 {
6701#ifdef BACKSLASH_IN_FILENAME
6702 slash_adjust(NameBuff);
6703#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006704 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 }
6706 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006707 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708}
6709
6710/*
6711 * ":=".
6712 */
6713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006714ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006716 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006717 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718}
6719
6720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006721ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006723 int n;
6724 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
6726 if (cursor_valid())
6727 {
6728 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6729 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006730 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006732
6733 len = eap->line2;
6734 switch (*eap->arg)
6735 {
6736 case 'm': break;
6737 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006738 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006739 }
6740 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741}
6742
6743/*
6744 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6745 */
6746 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006747do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
Bram Moolenaar52953192019-08-16 10:27:13 +02006749 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006750 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006751# ifdef ELAPSED_FUNC
6752 elapsed_T start_tv;
6753
6754 // Remember at what time we started, so that we know how much longer we
6755 // should wait after waiting for a bit.
6756 ELAPSED_INIT(start_tv);
6757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006760 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006761 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006763 wait_now = msec - done > 1000L ? 1000L : msec - done;
6764#ifdef FEAT_TIMERS
6765 {
6766 long due_time = check_due_timer();
6767
6768 if (due_time > 0 && due_time < wait_now)
6769 wait_now = due_time;
6770 }
6771#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006772#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006773 if (has_any_channel() && wait_now > 20L)
6774 wait_now = 20L;
6775#endif
6776#ifdef FEAT_SOUND
6777 if (has_any_sound_callback() && wait_now > 20L)
6778 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006779#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006780 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006781
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006782#ifdef FEAT_JOB_CHANNEL
6783 if (has_any_channel())
6784 ui_breakcheck_force(TRUE);
6785 else
6786#endif
6787 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006788#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006789 // Process the netbeans and clientserver messages that may have been
6790 // received in the call to ui_breakcheck() when the GUI is in use. This
6791 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006792 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006793#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006794
6795# ifdef ELAPSED_FUNC
6796 // actual time passed
6797 done = ELAPSED_FUNC(start_tv);
6798# else
6799 // guestimate time passed (will actually be more)
6800 done += wait_now;
6801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006803
6804 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6805 // input buffer, otherwise a following call to input() fails.
6806 if (got_int)
6807 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808}
6809
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810/*
6811 * ":winsize" command (obsolete).
6812 */
6813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006814ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 int w, h;
6817 char_u *arg = eap->arg;
6818 char_u *p;
6819
6820 w = getdigits(&arg);
6821 arg = skipwhite(arg);
6822 p = arg;
6823 h = getdigits(&arg);
6824 if (*p != NUL && *arg == NUL)
6825 set_shellsize(w, h, TRUE);
6826 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006827 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828}
6829
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006831ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
6833 int xchar = NUL;
6834 char_u *p;
6835
6836 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006838 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 if (eap->arg[1] == NUL)
6840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006841 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 return;
6843 }
6844 xchar = eap->arg[1];
6845 p = eap->arg + 2;
6846 }
6847 else
6848 p = eap->arg + 1;
6849
6850 eap->nextcmd = check_nextcmd(p);
6851 p = skipwhite(p);
6852 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006853 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006854 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006856 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006858 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6860 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006861 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 }
6863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
Bram Moolenaar843ee412004-06-30 16:16:41 +00006865#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866/*
6867 * ":winpos".
6868 */
6869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006870ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
6872 int x, y;
6873 char_u *arg = eap->arg;
6874 char_u *p;
6875
6876 if (*arg == NUL)
6877 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006878# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006879# ifdef VIMDLL
6880 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6881 mch_get_winpos(&x, &y) != FAIL)
6882# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006884# else
6885 if (mch_get_winpos(&x, &y) != FAIL)
6886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
6888 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006889 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 }
6891 else
6892# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006893 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 }
6895 else
6896 {
6897 x = getdigits(&arg);
6898 arg = skipwhite(arg);
6899 p = arg;
6900 y = getdigits(&arg);
6901 if (*p == NUL || *arg != NUL)
6902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006903 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 return;
6905 }
6906# ifdef FEAT_GUI
6907 if (gui.in_use)
6908 gui_mch_set_winpos(x, y);
6909 else if (gui.starting)
6910 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006911 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 gui_win_x = x;
6913 gui_win_y = y;
6914 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006915# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 else
6917# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006918# endif
6919# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006920 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921# endif
6922# ifdef HAVE_TGETENT
6923 if (*T_CWP)
6924 term_set_winpos(x, y);
6925# endif
6926 }
6927}
6928#endif
6929
6930/*
6931 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6932 */
6933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006934ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936 oparg_T oa;
6937
6938 clear_oparg(&oa);
6939 oa.regname = eap->regname;
6940 oa.start.lnum = eap->line1;
6941 oa.end.lnum = eap->line2;
6942 oa.line_count = eap->line2 - eap->line1 + 1;
6943 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006945 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
6947 setpcmark();
6948 curwin->w_cursor.lnum = eap->line1;
6949 beginline(BL_SOL | BL_FIX);
6950 }
6951
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006952 if (VIsual_active)
6953 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 switch (eap->cmdidx)
6956 {
6957 case CMD_delete:
6958 oa.op_type = OP_DELETE;
6959 op_delete(&oa);
6960 break;
6961
6962 case CMD_yank:
6963 oa.op_type = OP_YANK;
6964 (void)op_yank(&oa, FALSE, TRUE);
6965 break;
6966
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006967 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02006968 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006970 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6971#else
6972 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006974 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 oa.op_type = OP_RSHIFT;
6976 else
6977 oa.op_type = OP_LSHIFT;
6978 op_shift(&oa, FALSE, eap->amount);
6979 break;
6980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006982 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983}
6984
6985/*
6986 * ":put".
6987 */
6988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006989ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006991 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (eap->line2 == 0)
6993 {
6994 eap->line2 = 1;
6995 eap->forceit = TRUE;
6996 }
6997 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006998 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6999 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000}
7001
7002/*
7003 * Handle ":copy" and ":move".
7004 */
7005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007006ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 long n;
7009
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007010 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007011 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {
7013 eap->nextcmd = NULL;
7014 return;
7015 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007016 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
7018 /*
7019 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7020 */
7021 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7022 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007023 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 return;
7025 }
7026
7027 if (eap->cmdidx == CMD_move)
7028 {
7029 if (do_move(eap->line1, eap->line2, n) == FAIL)
7030 return;
7031 }
7032 else
7033 ex_copy(eap->line1, eap->line2, n);
7034 u_clearline();
7035 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007036 ex_may_print(eap);
7037}
7038
7039/*
7040 * Print the current line if flags were given to the Ex command.
7041 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007042 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007043ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007044{
7045 if (eap->flags != 0)
7046 {
7047 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7048 (eap->flags & EXFLAG_LIST));
7049 ex_no_reprint = TRUE;
7050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051}
7052
7053/*
7054 * ":smagic" and ":snomagic".
7055 */
7056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007057ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 int magic_save = p_magic;
7060
7061 p_magic = (eap->cmdidx == CMD_smagic);
7062 do_sub(eap);
7063 p_magic = magic_save;
7064}
7065
7066/*
7067 * ":join".
7068 */
7069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007070ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
7072 curwin->w_cursor.lnum = eap->line1;
7073 if (eap->line1 == eap->line2)
7074 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007075 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 return;
7077 if (eap->line2 == curbuf->b_ml.ml_line_count)
7078 {
7079 beep_flush();
7080 return;
7081 }
7082 ++eap->line2;
7083 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007084 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007086 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087}
7088
7089/*
7090 * ":[addr]@r" or ":[addr]*r": execute register
7091 */
7092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007093ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
7095 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007096 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
7098 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007099 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100
7101#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007102 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103#endif
7104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007105 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 c = *eap->arg;
7107 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7108 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007109 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007110 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7111 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 else
7116 {
7117 int save_efr = exec_from_reg;
7118
7119 exec_from_reg = TRUE;
7120
7121 /*
7122 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007123 * Continue until the stuff buffer is empty and all added characters
7124 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007126 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7128
7129 exec_from_reg = save_efr;
7130 }
7131}
7132
7133/*
7134 * ":!".
7135 */
7136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007137ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
7139 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7140}
7141
7142/*
7143 * ":undo".
7144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007146ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007148 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007149 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007150 else
7151 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152}
7153
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007154#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007156ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007157{
7158 char_u hash[UNDO_HASH_SIZE];
7159
7160 u_compute_hash(hash);
7161 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7162}
7163
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007165ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007166{
7167 char_u hash[UNDO_HASH_SIZE];
7168
7169 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007170 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007171}
7172#endif
7173
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174/*
7175 * ":redo".
7176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007178ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
7180 u_redo(1);
7181}
7182
7183/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007184 * ":earlier" and ":later".
7185 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007187ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007188{
7189 long count = 0;
7190 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007191 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007192 char_u *p = eap->arg;
7193
7194 if (*p == NUL)
7195 count = 1;
7196 else if (isdigit(*p))
7197 {
7198 count = getdigits(&p);
7199 switch (*p)
7200 {
7201 case 's': ++p; sec = TRUE; break;
7202 case 'm': ++p; sec = TRUE; count *= 60; break;
7203 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007204 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7205 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007206 }
7207 }
7208
7209 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007210 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007211 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007212 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7213 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007214}
7215
7216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 * ":redir": start/stop redirection.
7218 */
7219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007220ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 char *mode;
7223 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007224 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
Bram Moolenaarba768492016-07-08 15:32:54 +02007226#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007227 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007229 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007230 return;
7231 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007232#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007233
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 if (STRICMP(eap->arg, "END") == 0)
7235 close_redir();
7236 else
7237 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007238 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007240 ++arg;
7241 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007243 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 mode = "a";
7245 }
7246 else
7247 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007248 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
7250 close_redir();
7251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007252 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007253 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 if (fname == NULL)
7255 return;
7256#ifdef FEAT_BROWSE
7257 if (cmdmod.browse)
7258 {
7259 char_u *browseFile;
7260
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007261 browseFile = do_browse(BROWSE_SAVE,
7262 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007263 fname, NULL, NULL,
7264 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007266 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 vim_free(fname);
7268 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007269 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 }
7271#endif
7272
7273 redir_fd = open_exfile(fname, eap->forceit, mode);
7274 vim_free(fname);
7275 }
7276#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007277 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007279 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007281 ++arg;
7282 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007284 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007285 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007287 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007289 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007290 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007291 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007294 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007295 if (*arg == '>')
7296 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007297 // Make register empty when not using @A-@Z and the
7298 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007299 if (*arg == NUL && !isupper(redir_reg))
7300 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007302 }
7303 if (*arg != NUL)
7304 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007305 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007306 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007307 }
7308 }
7309 else if (*arg == '=' && arg[1] == '>')
7310 {
7311 int append;
7312
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007313 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007314 close_redir();
7315 arg += 2;
7316
7317 if (*arg == '>')
7318 {
7319 ++arg;
7320 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 }
7322 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007323 append = FALSE;
7324
7325 if (var_redir_start(skipwhite(arg), append) == OK)
7326 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 }
7328#endif
7329
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007330 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007333 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007336 // Make sure redirection is not off. Can happen for cmdline completion
7337 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007338 if (redir_fd != NULL
7339#ifdef FEAT_EVAL
7340 || redir_reg || redir_vname
7341#endif
7342 )
7343 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344}
7345
7346/*
7347 * ":redraw": force redraw
7348 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007349 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007350ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
7352 int r = RedrawingDisabled;
7353 int p = p_lz;
7354
7355 RedrawingDisabled = 0;
7356 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007357 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007359 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360#ifdef FEAT_TITLE
7361 if (need_maketitle)
7362 maketitle();
7363#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007364#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7365# ifdef VIMDLL
7366 if (!gui.in_use)
7367# endif
7368 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 RedrawingDisabled = r;
7371 p_lz = p;
7372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007373 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 msg_didout = FALSE;
7375 msg_col = 0;
7376
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007377 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007378 need_wait_return = FALSE;
7379
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 out_flush();
7381}
7382
7383/*
7384 * ":redrawstatus": force redraw of status line(s)
7385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007387ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 int r = RedrawingDisabled;
7390 int p = p_lz;
7391
7392 RedrawingDisabled = 0;
7393 p_lz = FALSE;
7394 if (eap->forceit)
7395 status_redraw_all();
7396 else
7397 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007398 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 RedrawingDisabled = r;
7400 p_lz = p;
7401 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402}
7403
Bram Moolenaare12bab32019-01-08 22:02:56 +01007404/*
7405 * ":redrawtabline": force redraw of the tabline
7406 */
7407 static void
7408ex_redrawtabline(exarg_T *eap UNUSED)
7409{
7410 int r = RedrawingDisabled;
7411 int p = p_lz;
7412
7413 RedrawingDisabled = 0;
7414 p_lz = FALSE;
7415
7416 draw_tabline();
7417
7418 RedrawingDisabled = r;
7419 p_lz = p;
7420 out_flush();
7421}
7422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007424close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 if (redir_fd != NULL)
7427 {
7428 fclose(redir_fd);
7429 redir_fd = NULL;
7430 }
7431#ifdef FEAT_EVAL
7432 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007433 if (redir_vname)
7434 {
7435 var_redir_stop();
7436 redir_vname = 0;
7437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438#endif
7439}
7440
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007441#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007442 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007443vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007444{
7445 if (vim_mkdir(name, prot) != 0)
7446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007447 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007448 return FAIL;
7449 }
7450 return OK;
7451}
7452#endif
7453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454/*
7455 * Open a file for writing for an Ex command, with some checks.
7456 * Return file descriptor, or NULL on failure.
7457 */
7458 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007459open_exfile(
7460 char_u *fname,
7461 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007462 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463{
7464 FILE *fd;
7465
7466#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007467 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (mch_isdir(fname))
7469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007470 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 return NULL;
7472 }
7473#endif
7474 if (!forceit && *mode != 'a' && vim_fexists(fname))
7475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007476 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 return NULL;
7478 }
7479
7480 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007481 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482
7483 return fd;
7484}
7485
7486/*
7487 * ":mark" and ":k".
7488 */
7489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007490ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 pos_T pos;
7493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007494 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007495 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007496 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007497 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 else
7499 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007500 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 curwin->w_cursor.lnum = eap->line2;
7502 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007503 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007504 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007505 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 }
7507}
7508
7509/*
7510 * Update w_topline, w_leftcol and the cursor position.
7511 */
7512 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007513update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007515 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 update_topline();
7517 if (!curwin->w_p_wrap)
7518 validate_cursor();
7519 update_curswant();
7520}
7521
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007523 * Save the current State and go to Normal mode.
7524 * Return TRUE if the typeahead could be saved.
7525 */
7526 int
7527save_current_state(save_state_T *sst)
7528{
7529 sst->save_msg_scroll = msg_scroll;
7530 sst->save_restart_edit = restart_edit;
7531 sst->save_msg_didout = msg_didout;
7532 sst->save_State = State;
7533 sst->save_insertmode = p_im;
7534 sst->save_finish_op = finish_op;
7535 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007536 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007538 msg_scroll = FALSE; // no msg scrolling in Normal mode
7539 restart_edit = 0; // don't go to Insert mode
7540 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007541
7542 /*
7543 * Save the current typeahead. This is required to allow using ":normal"
7544 * from an event handler and makes sure we don't hang when the argument
7545 * ends with half a command.
7546 */
7547 save_typeahead(&sst->tabuf);
7548 return sst->tabuf.typebuf_valid;
7549}
7550
7551 void
7552restore_current_state(save_state_T *sst)
7553{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007554 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007555 restore_typeahead(&sst->tabuf);
7556
7557 msg_scroll = sst->save_msg_scroll;
7558 restart_edit = sst->save_restart_edit;
7559 p_im = sst->save_insertmode;
7560 finish_op = sst->save_finish_op;
7561 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007562 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007563 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007565 // Restore the state (needed when called from a function executed for
7566 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007567 State = sst->save_State;
7568#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007569 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007570#endif
7571}
7572
7573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 * ":normal[!] {commands}": Execute normal mode commands.
7575 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007576 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007577ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007579 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 char_u *arg = NULL;
7581 int l;
7582 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007584 if (ex_normal_lock > 0)
7585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007586 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007587 return;
7588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 if (ex_normal_busy >= p_mmd)
7590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007591 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 return;
7593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 /*
7596 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7597 * this for the K_SPECIAL leading byte, otherwise special keys will not
7598 * work.
7599 */
7600 if (has_mbyte)
7601 {
7602 int len = 0;
7603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007604 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 for (p = eap->arg; *p != NUL; ++p)
7606 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007607#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007608 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007610#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007611 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007612 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007613#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 )
7617 len += 2;
7618 }
7619 if (len > 0)
7620 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007621 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 if (arg != NULL)
7623 {
7624 len = 0;
7625 for (p = eap->arg; *p != NUL; ++p)
7626 {
7627 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007628#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 if (*p == CSI)
7630 {
7631 arg[len++] = KS_EXTRA;
7632 arg[len++] = (int)KE_CSI;
7633 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007634#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007635 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {
7637 arg[len++] = *++p;
7638 if (*p == K_SPECIAL)
7639 {
7640 arg[len++] = KS_SPECIAL;
7641 arg[len++] = KE_FILLER;
7642 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007643#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 else if (*p == CSI)
7645 {
7646 arg[len++] = KS_EXTRA;
7647 arg[len++] = (int)KE_CSI;
7648 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
7651 arg[len] = NUL;
7652 }
7653 }
7654 }
7655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007657 ++ex_normal_busy;
7658 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {
7660 /*
7661 * Repeat the :normal command for each line in the range. When no
7662 * range given, execute it just once, without positioning the cursor
7663 * first.
7664 */
7665 do
7666 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (eap->addr_count != 0)
7668 {
7669 curwin->w_cursor.lnum = eap->line1++;
7670 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007671 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 }
7673
Bram Moolenaar13505972019-01-24 15:04:48 +01007674 exec_normal_cmd(arg != NULL
7675 ? arg
7676 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7679 }
7680
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007681 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 update_topline_cursor();
7683
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007684 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007686 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007687#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007688 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007689#endif
7690
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692}
7693
7694/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007695 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 */
7697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007698ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007700 if (eap->forceit)
7701 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007702 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007703 if (!curwin->w_cursor.lnum)
7704 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007705 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007706 }
7707
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007708 // Ignore the command when already in Insert mode. Inserting an
7709 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007710 if (State & INSERT)
7711 return;
7712
Bram Moolenaar64969662005-12-14 21:59:55 +00007713 if (eap->cmdidx == CMD_startinsert)
7714 restart_edit = 'a';
7715 else if (eap->cmdidx == CMD_startreplace)
7716 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007718 restart_edit = 'V';
7719
7720 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007722 if (eap->cmdidx == CMD_startinsert)
7723 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007724 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 }
7726}
7727
7728/*
7729 * ":stopinsert"
7730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007732ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 restart_edit = 0;
7735 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007736 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007739/*
7740 * Execute normal mode command "cmd".
7741 * "remap" can be REMAP_NONE or REMAP_YES.
7742 */
7743 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007744exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007745{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007746 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007747 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007748 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007749}
Bram Moolenaar25281632016-01-21 23:32:32 +01007750
Bram Moolenaar25281632016-01-21 23:32:32 +01007751/*
7752 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007753 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007754 */
7755 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007756exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007757{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007758 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007759 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007760
Bram Moolenaar905dd902019-04-07 14:21:47 +02007761 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7762 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007763 clear_oparg(&oa);
7764 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007765 while ((!stuff_empty()
7766 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007767 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007768 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007769 {
7770 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007771#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007772 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007773 && oa.op_type == OP_NOP && oa.regname == NUL
7774 && !VIsual_active)
7775 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007776 // If terminal_loop() returns OK we got a key that is handled
7777 // in Normal model. With FAIL we first need to position the
7778 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007779 if (terminal_loop(TRUE) == OK)
7780 normal_cmd(&oa, TRUE);
7781 }
7782 else
7783#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007784 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007785 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007786 }
7787}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007788
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789#ifdef FEAT_FIND_ID
7790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007791ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7794 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7795 (linenr_T)1, (linenr_T)MAXLNUM);
7796}
7797
Bram Moolenaar4033c552017-09-16 20:54:51 +02007798#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799/*
7800 * ":psearch"
7801 */
7802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007803ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804{
7805 g_do_tagpreview = p_pvh;
7806 ex_findpat(eap);
7807 g_do_tagpreview = 0;
7808}
7809#endif
7810
7811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007812ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
7814 int whole = TRUE;
7815 long n;
7816 char_u *p;
7817 int action;
7818
7819 switch (cmdnames[eap->cmdidx].cmd_name[2])
7820 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007821 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7823 action = ACTION_GOTO;
7824 else
7825 action = ACTION_SHOW;
7826 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007827 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 action = ACTION_SHOW_ALL;
7829 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007830 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 action = ACTION_GOTO;
7832 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007833 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 action = ACTION_SPLIT;
7835 break;
7836 }
7837
7838 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007839 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {
7841 n = getdigits(&eap->arg);
7842 eap->arg = skipwhite(eap->arg);
7843 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007844 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {
7846 whole = FALSE;
7847 ++eap->arg;
7848 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7849 if (*p)
7850 {
7851 *p++ = NUL;
7852 p = skipwhite(p);
7853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007854 // Check for trailing illegal characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 if (!ends_excmd(*p))
7856 eap->errmsg = e_trailing;
7857 else
7858 eap->nextcmd = check_nextcmd(p);
7859 }
7860 }
7861 if (!eap->skip)
7862 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7863 whole, !eap->forceit,
7864 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7865 n, action, eap->line1, eap->line2);
7866}
7867#endif
7868
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869
Bram Moolenaar4033c552017-09-16 20:54:51 +02007870#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871/*
7872 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7873 */
7874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007875ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007877 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7879}
7880
7881/*
7882 * ":pedit"
7883 */
7884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007885ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 win_T *curwin_save = curwin;
7888
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007889 if (ERROR_IF_ANY_POPUP_WINDOW)
7890 return;
7891
Bram Moolenaar026587b2019-08-17 15:08:00 +02007892 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007894 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007895
Bram Moolenaar026587b2019-08-17 15:08:00 +02007896 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007898
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 if (curwin != curwin_save && win_valid(curwin_save))
7900 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007901 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 validate_cursor();
7903 redraw_later(VALID);
7904 win_enter(curwin_save, TRUE);
7905 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007906# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007907 else if (WIN_IS_POPUP(curwin))
7908 {
7909 // can't keep focus in popup window
7910 win_enter(firstwin, TRUE);
7911 }
7912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 g_do_tagpreview = 0;
7914}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916
7917/*
7918 * ":stag", ":stselect" and ":stjump".
7919 */
7920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007921ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 postponed_split = -1;
7924 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007925 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7927 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007928 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
7931/*
7932 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7933 */
7934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
7937 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7938}
7939
7940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007941ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943 int cmd;
7944
7945 switch (name[1])
7946 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007947 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007949 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007951 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007953 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007955 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007957 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007959 case 'f': // ":tfirst"
7960 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007962 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007964 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007966 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007968 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 return;
7970 }
7971#endif
7972 cmd = DT_TAG;
7973 break;
7974 }
7975
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007976 if (name[0] == 'l')
7977 {
7978#ifndef FEAT_QUICKFIX
7979 ex_ni(eap);
7980 return;
7981#else
7982 cmd = DT_LTAG;
7983#endif
7984 }
7985
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7987 eap->forceit, TRUE);
7988}
7989
7990/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007991 * Check "str" for starting with a special cmdline variable.
7992 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7993 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7994 */
7995 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007997{
7998 int len;
7999 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008000 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008001 "%",
8002#define SPEC_PERC 0
8003 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008004#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008005 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008006#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008008#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008009 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008010#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008011 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008012#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008013 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008014#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008015 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008016#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008017 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008018#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008019 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008020#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008021 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008022#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008023 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008024#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008025#ifdef FEAT_CLIENTSERVER
8026 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008027# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008028#endif
8029 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008030
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008031 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008032 {
8033 len = (int)STRLEN(spec_str[i]);
8034 if (STRNCMP(src, spec_str[i], len) == 0)
8035 {
8036 *usedlen = len;
8037 return i;
8038 }
8039 }
8040 return -1;
8041}
8042
8043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 * Evaluate cmdline variables.
8045 *
8046 * change '%' to curbuf->b_ffname
8047 * '#' to curwin->w_altfile
8048 * '<cword>' to word under the cursor
8049 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008050 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 * '<cfile>' to path name under the cursor
8052 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008053 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 * '<afile>' to file name for autocommand
8055 * '<abuf>' to buffer number for autocommand
8056 * '<amatch>' to matching name for autocommand
8057 *
8058 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8059 * "" for error without a message) and NULL is returned.
8060 * Returns an allocated string if a valid match was found.
8061 * Returns NULL if no match was found. "usedlen" then still contains the
8062 * number of characters to skip.
8063 */
8064 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008065eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 char_u *src, // pointer into commandline
8067 char_u *srcstart, // beginning of valid memory for src
8068 int *usedlen, // characters after src that are used
8069 linenr_T *lnump, // line number for :e command, or NULL
8070 char **errormsg, // pointer to error message
8071 int *escaped) // return value has escaped white space (can
8072 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074 int i;
8075 char_u *s;
8076 char_u *result;
8077 char_u *resultbuf = NULL;
8078 int resultlen;
8079 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008080 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008082 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085
8086 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008087 if (escaped != NULL)
8088 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089
8090 /*
8091 * Check if there is something to do.
8092 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008093 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008094 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {
8096 *usedlen = 1;
8097 return NULL;
8098 }
8099
8100 /*
8101 * Skip when preceded with a backslash "\%" and "\#".
8102 * Note: In "\\%" the % is also not recognized!
8103 */
8104 if (src > srcstart && src[-1] == '\\')
8105 {
8106 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008107 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 return NULL;
8109 }
8110
8111 /*
8112 * word or WORD under cursor
8113 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008114 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8115 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008117 resultlen = find_ident_under_cursor(&result,
8118 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8119 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8120 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (resultlen == 0)
8122 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008123 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 return NULL;
8125 }
8126 }
8127
8128 /*
8129 * '#': Alternate file name
8130 * '%': Current file name
8131 * File name under the cursor
8132 * File name for autocommand
8133 * and following modifiers
8134 */
8135 else
8136 {
8137 switch (spec_idx)
8138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008139 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 if (curbuf->b_fname == NULL)
8141 {
8142 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008143 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 }
8145 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008148 tilde_file = STRCMP(result, "~") == 0;
8149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 break;
8151
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008152 case SPEC_HASH: // '#' or "#99": alternate file
8153 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
8155 result = arg_all();
8156 resultbuf = result;
8157 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008158 if (escaped != NULL)
8159 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 break;
8162 }
8163 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008164 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008165 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008167 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008169 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008170 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008172 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008174 if (*usedlen < 2)
8175 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008176 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008177 *usedlen = 1;
8178 return NULL;
8179 }
8180#ifdef FEAT_EVAL
8181 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8182 (long)i);
8183 if (result == NULL)
8184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008185 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008186 return NULL;
8187 }
8188#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008189 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 }
8193 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008194 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008195 if (i == 0 && src[1] == '<' && *usedlen > 1)
8196 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008197 buf = buflist_findnr(i);
8198 if (buf == NULL)
8199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008200 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008201 return NULL;
8202 }
8203 if (lnump != NULL)
8204 *lnump = ECMD_LAST;
8205 if (buf->b_fname == NULL)
8206 {
8207 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008208 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008209 }
8210 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008211 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008212 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008213 tilde_file = STRCMP(result, "~") == 0;
8214 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 break;
8217
8218#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008219 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008220 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (result == NULL)
8222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008223 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 return NULL;
8225 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008226 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 break;
8228#endif
8229
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008230 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008232 if (result != NULL && !autocmd_fname_full)
8233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008234 // Still need to turn the fname into a full path. It is
8235 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008236 autocmd_fname_full = TRUE;
8237 result = FullName_save(autocmd_fname, FALSE);
8238 vim_free(autocmd_fname);
8239 autocmd_fname = result;
8240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 if (result == NULL)
8242 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008243 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 return NULL;
8245 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008246 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 break;
8248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008249 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 if (autocmd_bufnr <= 0)
8251 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008252 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 return NULL;
8254 }
8255 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8256 result = strbuf;
8257 break;
8258
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008259 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 result = autocmd_match;
8261 if (result == NULL)
8262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008263 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 return NULL;
8265 }
8266 break;
8267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008268 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008269 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 if (result == NULL)
8271 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008272 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 return NULL;
8274 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008275 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008277
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008279 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008281 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008282 return NULL;
8283 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008284 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008285 result = strbuf;
8286 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008287
8288#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008289 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008290 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008292 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008293 return NULL;
8294 }
8295 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008296 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008297 result = strbuf;
8298 break;
8299#endif
8300
8301#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008302 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008303 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8304 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 result = strbuf;
8306 break;
8307#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008308
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008309 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008310 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008311 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 }
8313
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008314 resultlen = (int)STRLEN(result); // length of new string
8315 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {
8317 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 resultlen = (int)(s - result);
8320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 else if (!skip_mod)
8322 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008323 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 &resultlen);
8325 if (result == NULL)
8326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008327 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 return NULL;
8329 }
8330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 }
8332
8333 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8334 {
8335 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008337 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008339 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 result = NULL;
8341 }
8342 else
8343 result = vim_strnsave(result, resultlen);
8344 vim_free(resultbuf);
8345 return result;
8346}
8347
8348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 * Expand the <sfile> string in "arg".
8350 *
8351 * Returns an allocated string, or NULL for any error.
8352 */
8353 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008354expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008356 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 int len;
8358 char_u *result;
8359 char_u *newres;
8360 char_u *repl;
8361 int srclen;
8362 char_u *p;
8363
8364 result = vim_strsave(arg);
8365 if (result == NULL)
8366 return NULL;
8367
8368 for (p = result; *p; )
8369 {
8370 if (STRNCMP(p, "<sfile>", 7) != 0)
8371 ++p;
8372 else
8373 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008374 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008375 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 if (errormsg != NULL)
8377 {
8378 if (*errormsg)
8379 emsg(errormsg);
8380 vim_free(result);
8381 return NULL;
8382 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008383 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
8385 p += srclen;
8386 continue;
8387 }
8388 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8389 newres = alloc(len);
8390 if (newres == NULL)
8391 {
8392 vim_free(repl);
8393 vim_free(result);
8394 return NULL;
8395 }
8396 mch_memmove(newres, result, (size_t)(p - result));
8397 STRCPY(newres + (p - result), repl);
8398 len = (int)STRLEN(newres);
8399 STRCAT(newres, p + srclen);
8400 vim_free(repl);
8401 vim_free(result);
8402 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008403 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 }
8405 }
8406
8407 return result;
8408}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008411/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008412 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008413 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008416dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (fname == NULL)
8419 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008420 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421}
8422#endif
8423
8424/*
8425 * ":behave {mswin,xterm}"
8426 */
8427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008428ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429{
8430 if (STRCMP(eap->arg, "mswin") == 0)
8431 {
8432 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8433 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8434 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8435 set_option_value((char_u *)"keymodel", 0L,
8436 (char_u *)"startsel,stopsel", 0);
8437 }
8438 else if (STRCMP(eap->arg, "xterm") == 0)
8439 {
8440 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8441 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8442 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8443 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8444 }
8445 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008446 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447}
8448
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449static int filetype_detect = FALSE;
8450static int filetype_plugin = FALSE;
8451static int filetype_indent = FALSE;
8452
8453/*
8454 * ":filetype [plugin] [indent] {on,off,detect}"
8455 * on: Load the filetype.vim file to install autocommands for file types.
8456 * off: Load the ftoff.vim file to remove all autocommands for file types.
8457 * plugin on: load filetype.vim and ftplugin.vim
8458 * plugin off: load ftplugof.vim
8459 * indent on: load filetype.vim and indent.vim
8460 * indent off: load indoff.vim
8461 */
8462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008463ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 char_u *arg = eap->arg;
8466 int plugin = FALSE;
8467 int indent = FALSE;
8468
8469 if (*eap->arg == NUL)
8470 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008472 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 filetype_detect ? "ON" : "OFF",
8474 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8475 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8476 return;
8477 }
8478
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008479 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 for (;;)
8481 {
8482 if (STRNCMP(arg, "plugin", 6) == 0)
8483 {
8484 plugin = TRUE;
8485 arg = skipwhite(arg + 6);
8486 continue;
8487 }
8488 if (STRNCMP(arg, "indent", 6) == 0)
8489 {
8490 indent = TRUE;
8491 arg = skipwhite(arg + 6);
8492 continue;
8493 }
8494 break;
8495 }
8496 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8497 {
8498 if (*arg == 'o' || !filetype_detect)
8499 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008500 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 filetype_detect = TRUE;
8502 if (plugin)
8503 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008504 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 filetype_plugin = TRUE;
8506 }
8507 if (indent)
8508 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008509 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 filetype_indent = TRUE;
8511 }
8512 }
8513 if (*arg == 'd')
8514 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008515 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008516 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 }
8518 }
8519 else if (STRCMP(arg, "off") == 0)
8520 {
8521 if (plugin || indent)
8522 {
8523 if (plugin)
8524 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008525 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 filetype_plugin = FALSE;
8527 }
8528 if (indent)
8529 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008530 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 filetype_indent = FALSE;
8532 }
8533 }
8534 else
8535 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008536 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 filetype_detect = FALSE;
8538 }
8539 }
8540 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008541 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542}
8543
8544/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008545 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 */
8547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008548ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549{
8550 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008551 {
8552 char_u *arg = eap->arg;
8553
8554 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8555 arg += 9;
8556
8557 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8558 if (arg != eap->arg)
8559 did_filetype = FALSE;
8560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008564ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566#ifdef FEAT_DIGRAPHS
8567 if (*eap->arg != NUL)
8568 putdigraph(eap->arg);
8569 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008570 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008572 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#endif
8574}
8575
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008576#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8577 void
8578set_no_hlsearch(int flag)
8579{
8580 no_hlsearch = flag;
8581# ifdef FEAT_EVAL
8582 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8583# endif
8584}
8585
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586/*
8587 * ":nohlsearch"
8588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008590ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008592 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008593 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#endif
8596
8597#ifdef FEAT_CRYPT
8598/*
8599 * ":X": Get crypt key
8600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008602ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008604 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008605 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606}
8607#endif
8608
8609#ifdef FEAT_FOLDING
8610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008611ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612{
8613 if (foldManualAllowed(TRUE))
8614 foldCreate(eap->line1, eap->line2);
8615}
8616
8617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008618ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
8620 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8621 eap->forceit, FALSE);
8622}
8623
8624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008625ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
8627 linenr_T lnum;
8628
Bram Moolenaar4facea32019-10-12 20:17:40 +02008629# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008630 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008631# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008633 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8635 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8636 ml_setmarked(lnum);
8637
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008638 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008640 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008641# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008642 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008643# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644}
8645#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008647#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008648/*
8649 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8650 * instead of a quickfix command.
8651 */
8652 int
8653is_loclist_cmd(int cmdidx)
8654{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008655 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008656 return FALSE;
8657 return cmdnames[cmdidx].cmd_name[0] == 'l';
8658}
8659#endif
8660
Bram Moolenaar4facea32019-10-12 20:17:40 +02008661#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008662 int
8663get_pressedreturn(void)
8664{
8665 return ex_pressedreturn;
8666}
8667
8668 void
8669set_pressedreturn(int val)
8670{
8671 ex_pressedreturn = val;
8672}
8673#endif