blob: e2d771e194dbc63d446c909fc2fcc19c30f47644 [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 Moolenaar2196bce2020-04-12 20:01:11 +0200650#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100651 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
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 Moolenaara80faa82020-04-12 19:37:17 +0200731 CLEAR_FIELD(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
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001298 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 * 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 Moolenaar292b90d2020-03-18 15:23:16 +01001349
1350 // Cleanup if "cs_emsg_silent_list" remains.
1351 if (cstack.cs_emsg_silent_list != NULL)
1352 {
1353 eslist_T *elem, *temp;
1354
1355 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1356 {
1357 temp = elem->next;
1358 vim_free(elem);
1359 }
1360 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001361#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
1363 /*
1364 * If there was too much output to fit on the command line, ask the user to
1365 * hit return before redrawing the screen. With the ":global" command we do
1366 * this only once after the command is finished.
1367 */
1368 if (did_inc)
1369 {
1370 --RedrawingDisabled;
1371 --no_wait_return;
1372 msg_scroll = FALSE;
1373
1374 /*
1375 * When just finished an ":if"-":else" which was typed, no need to
1376 * wait for hit-return. Also for an error situation.
1377 */
1378 if (retval == FAIL
1379#ifdef FEAT_EVAL
1380 || (did_endif && KeyTyped && !did_emsg)
1381#endif
1382 )
1383 {
1384 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001385 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 else if (need_wait_return)
1388 {
1389 /*
1390 * The msg_start() above clears msg_didout. The wait_return we do
1391 * here should not overwrite the command that may be shown before
1392 * doing that.
1393 */
1394 msg_didout |= msg_didout_before_start;
1395 wait_return(FALSE);
1396 }
1397 }
1398
Bram Moolenaar2a942252012-11-28 23:03:07 +01001399#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001400 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001401#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 /*
1403 * Reset if_level, in case a sourced script file contains more ":if" than
1404 * ":endif" (could be ":if x | foo | endif").
1405 */
1406 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001407#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 --call_depth;
1410 return retval;
1411}
1412
1413#ifdef FEAT_EVAL
1414/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001415 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 */
1417 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001418get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001420 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 wcmd_T *wp;
1422 char_u *line;
1423
1424 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1425 {
1426 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001427 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001429 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001431 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001433 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001434 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 ++cp->current_line;
1436
1437 return line;
1438 }
1439
1440 KeyTyped = FALSE;
1441 ++cp->current_line;
1442 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001443 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 return vim_strsave(wp->line);
1445}
1446
1447/*
1448 * Store a line in "gap" so that a ":while" loop can execute it again.
1449 */
1450 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001451store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 if (ga_grow(gap, 1) == FAIL)
1454 return FAIL;
1455 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001456 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 return OK;
1459}
1460
1461/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001462 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
1464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001465free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 while (gap->ga_len > 0)
1468 {
1469 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1470 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
1472}
1473#endif
1474
1475/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001476 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1477 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001480getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001481 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001482 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001483 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001486 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001487 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001489 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1490 // function that's originally used to obtain the lines. This may be
1491 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001492 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001493 cp = (struct loop_cookie *)cookie;
1494 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 gp = cp->getline;
1497 cp = cp->cookie;
1498 }
1499 return gp == func;
1500#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001501 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502#endif
1503}
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 * getline function. Otherwise return "cookie".
1508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001510getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001511 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaar13505972019-01-24 15:04:48 +01001514#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001515 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001516 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1519 // cookie that's originally used to obtain the lines. This may be nested
1520 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001521 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001522 cp = (struct loop_cookie *)cookie;
1523 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
1525 gp = cp->getline;
1526 cp = cp->cookie;
1527 }
1528 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001529#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001534
1535/*
1536 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1537 * ":bwipeout", etc.
1538 * Returns the buffer number.
1539 */
1540 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001541compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001542{
1543 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001544 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001545 int count = offset;
1546
1547 buf = firstbuf;
1548 while (buf->b_next != NULL && buf->b_fnum < lnum)
1549 buf = buf->b_next;
1550 while (count != 0)
1551 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001552 count += (offset < 0) ? 1 : -1;
1553 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1554 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001555 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001556 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001557 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001558 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001559 while (buf->b_ml.ml_mfp == NULL)
1560 {
1561 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1562 if (nextbuf == NULL)
1563 break;
1564 buf = nextbuf;
1565 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001566 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001567 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001568 if (addr_type == ADDR_LOADED_BUFFERS)
1569 while (buf->b_ml.ml_mfp == NULL)
1570 {
1571 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1572 if (nextbuf == NULL)
1573 break;
1574 buf = nextbuf;
1575 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001576 return buf->b_fnum;
1577}
1578
Bram Moolenaarb7316892019-05-01 18:08:42 +02001579/*
1580 * Return the window number of "win".
1581 * When "win" is NULL return the number of windows.
1582 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001583 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001584current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001585{
1586 win_T *wp;
1587 int nr = 0;
1588
Bram Moolenaar29323592016-07-24 22:04:11 +02001589 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001590 {
1591 ++nr;
1592 if (wp == win)
1593 break;
1594 }
1595 return nr;
1596}
1597
1598 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001599current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001600{
1601 tabpage_T *tp;
1602 int nr = 0;
1603
Bram Moolenaar29323592016-07-24 22:04:11 +02001604 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001605 {
1606 ++nr;
1607 if (tp == tab)
1608 break;
1609 }
1610 return nr;
1611}
1612
1613# define CURRENT_WIN_NR current_win_nr(curwin)
1614# define LAST_WIN_NR current_win_nr(NULL)
1615# define CURRENT_TAB_NR current_tab_nr(curtab)
1616# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618/*
1619 * Execute one Ex command.
1620 *
1621 * If 'sourcing' is TRUE, the command will be included in the error message.
1622 *
1623 * 1. skip comment lines and leading space
1624 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001625 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001627 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 * 6. parse arguments
1629 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 *
1633 * This function may be called recursively!
1634 */
1635#if (_MSC_VER == 1200)
1636/*
Bram Moolenaared203462004-06-16 11:19:22 +00001637 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001639 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
1641 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001642do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001643 char_u **cmdlinep,
1644 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001646 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001648 char_u *(*fgetline)(int, void *, int, int),
1649 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001651 char_u *p;
1652 linenr_T lnum;
1653 long n;
1654 char *errormsg = NULL; // error message
1655 char_u *after_modifier = NULL;
1656 exarg_T ea; // Ex command arguments
1657 int save_msg_scroll = msg_scroll;
1658 cmdmod_T save_cmdmod;
1659 int save_reg_executing = reg_executing;
1660 int ni; // set when Not Implemented
1661 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001662#ifdef FEAT_EVAL
1663 int starts_with_colon;
1664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaara80faa82020-04-12 19:37:17 +02001666 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 ea.line1 = 1;
1668 ea.line2 = 1;
1669#ifdef FEAT_EVAL
1670 ++ex_nesting_level;
1671#endif
1672
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001673 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (quitmore
1675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001676 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001677 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001678#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001679 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001680 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 --quitmore;
1682
1683 /*
1684 * Reset browse, confirm, etc.. They are restored when returning, for
1685 * recursive calls.
1686 */
1687 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001689 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001690 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1691 goto doend;
1692
Bram Moolenaar4facea32019-10-12 20:17:40 +02001693 if (p_verbose >= 16)
1694 msg_verbose_cmd(0, *cmdlinep);
1695
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001696/*
1697 * 1. Skip comment lines and leading white space and colons.
1698 * 2. Handle command modifiers.
1699 */
1700 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001702 ea.cmdlinep = cmdlinep;
1703 ea.getline = fgetline;
1704 ea.cookie = cookie;
1705#ifdef FEAT_EVAL
1706 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001707 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001709 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001710 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001712 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714#ifdef FEAT_EVAL
1715 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1716 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1717#else
1718 ea.skip = (if_level > 0);
1719#endif
1720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001722 * 3. Skip over the range to find the command. Let "p" point to after it.
1723 *
1724 * We need the command to know what kind of range it uses.
1725 */
1726 cmd = ea.cmd;
1727 ea.cmd = skip_range(ea.cmd, NULL);
1728 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1729 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730
1731#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001732 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1734 else
1735#endif
1736 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001737
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001738#ifdef FEAT_EVAL
1739# ifdef FEAT_PROFILE
1740 // Count this line for profiling if skip is TRUE.
1741 if (do_profiling == PROF_YES
1742 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1743 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1744 {
1745 int skip = did_emsg || got_int || did_throw;
1746
1747 if (ea.cmdidx == CMD_catch)
1748 skip = !skip && !(cstack->cs_idx >= 0
1749 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1750 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1751 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1752 skip = skip || !(cstack->cs_idx >= 0
1753 && !(cstack->cs_flags[cstack->cs_idx]
1754 & (CSF_ACTIVE | CSF_TRUE)));
1755 else if (ea.cmdidx == CMD_finally)
1756 skip = FALSE;
1757 else if (ea.cmdidx != CMD_endif
1758 && ea.cmdidx != CMD_endfor
1759 && ea.cmdidx != CMD_endtry
1760 && ea.cmdidx != CMD_endwhile)
1761 skip = ea.skip;
1762
1763 if (!skip)
1764 {
1765 if (getline_equal(fgetline, cookie, get_func_line))
1766 func_line_exec(getline_cookie(fgetline, cookie));
1767 else if (getline_equal(fgetline, cookie, getsourceline))
1768 script_line_exec();
1769 }
1770 }
1771# endif
1772
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001773 // May go to debug mode. If this happens and the ">quit" debug command is
1774 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001775 dbg_check_breakpoint(&ea);
1776 if (!ea.skip && got_int)
1777 {
1778 ea.skip = TRUE;
1779 (void)do_intthrow(cstack);
1780 }
1781#endif
1782
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001783/*
1784 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 *
1786 * where 'addr' is:
1787 *
1788 * % (entire file)
1789 * $ [+-NUM]
1790 * 'x [+-NUM] (where x denotes a currently defined mark)
1791 * . [+-NUM]
1792 * [+-NUM]..
1793 * NUM
1794 *
1795 * The ea.cmd pointer is updated to point to the first character following the
1796 * range spec. If an initial address is found, but no second, the upper bound
1797 * is equal to the lower.
1798 */
1799
Bram Moolenaar25190db2019-05-04 15:05:28 +02001800 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001801 if (!IS_USER_CMDIDX(ea.cmdidx))
1802 {
1803 if (ea.cmdidx != CMD_SIZE)
1804 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1805 else
1806 ea.addr_type = ADDR_LINES;
1807
Bram Moolenaar25190db2019-05-04 15:05:28 +02001808 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001809 if (ea.cmdidx == CMD_wincmd && p != NULL)
1810 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001811#ifdef FEAT_QUICKFIX
1812 // :.cc in quickfix window uses line number
1813 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1814 ea.addr_type = ADDR_OTHER;
1815#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001816 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001817
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001818 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001819 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001820 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001823 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 */
1825
1826 /*
1827 * Skip ':' and any white space
1828 */
1829 ea.cmd = skipwhite(ea.cmd);
1830 while (*ea.cmd == ':')
1831 ea.cmd = skipwhite(ea.cmd + 1);
1832
1833 /*
1834 * If we got a line, but no command, then go to the line.
1835 * If we find a '|' or '\n' we set ea.nextcmd.
1836 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001837 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001838#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001839 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1840 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001841#endif
1842 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
1844 /*
1845 * strange vi behaviour:
1846 * ":3" jumps to line 3
1847 * ":3|..." prints line 3
1848 * ":|" prints current line
1849 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001850 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001852 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
1854 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001855 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if ((errormsg = invalid_range(&ea)) == NULL)
1857 {
1858 correct_range(&ea);
1859 ex_print(&ea);
1860 }
1861 }
1862 else if (ea.addr_count != 0)
1863 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001864 if (ea.line2 > curbuf->b_ml.ml_line_count)
1865 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001866 // With '-' in 'cpoptions' a line number past the file is an
1867 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001868 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1869 ea.line2 = -1;
1870 else
1871 ea.line2 = curbuf->b_ml.ml_line_count;
1872 }
1873
1874 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001875 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 else
1877 {
1878 if (ea.line2 == 0)
1879 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 else
1881 curwin->w_cursor.lnum = ea.line2;
1882 beginline(BL_SOL | BL_FIX);
1883 }
1884 }
1885 goto doend;
1886 }
1887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001888 // If this looks like an undefined user command and there are CmdUndefined
1889 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001890 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1891 && ASCII_ISUPPER(*ea.cmd)
1892 && has_cmdundefined())
1893 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001894 int ret;
1895
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001896 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001897 while (ASCII_ISALNUM(*p))
1898 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001899 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001900 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1901 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001902 // If the autocommands did something and didn't cause an error, try
1903 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001904 p = (ret
1905#ifdef FEAT_EVAL
1906 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001907#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001909 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (p == NULL)
1912 {
1913 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001914 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 goto doend;
1916 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001917 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001918 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1919 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
1921 errormsg = uc_fun_cmd();
1922 goto doend;
1923 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (ea.cmdidx == CMD_SIZE)
1926 {
1927 if (!ea.skip)
1928 {
1929 STRCPY(IObuff, _("E492: Not an editor command"));
1930 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001931 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001932 // If the modifier was parsed OK the error must be in the
1933 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001934 if (after_modifier != NULL)
1935 append_command(after_modifier);
1936 else
1937 append_command(*cmdlinep);
1938 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001939 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001940 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942 goto doend;
1943 }
1944
Bram Moolenaar958636c2014-10-21 20:01:58 +02001945 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1946 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001947#ifdef HAVE_EX_SCRIPT_NI
1948 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1949#endif
1950 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
1952#ifndef FEAT_EVAL
1953 /*
1954 * When the expression evaluation is disabled, recognize the ":if" and
1955 * ":endif" commands and ignore everything in between it.
1956 */
1957 if (ea.cmdidx == CMD_if)
1958 ++if_level;
1959 if (if_level)
1960 {
1961 if (ea.cmdidx == CMD_endif)
1962 --if_level;
1963 goto doend;
1964 }
1965
1966#endif
1967
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001968 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001969 if (*p == '!' && ea.cmdidx != CMD_substitute
1970 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 ++p;
1973 ea.forceit = TRUE;
1974 }
1975 else
1976 ea.forceit = FALSE;
1977
1978/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001979 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001981 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001982 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
1984 if (!ea.skip)
1985 {
1986#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001987 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001989 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001990 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 goto doend;
1992 }
1993#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001994 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001995 {
1996 errormsg = _("E981: Command not allowed in rvim");
1997 goto doend;
1998 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001999 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002001 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002002 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 goto doend;
2004 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002005
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002006 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002007 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002009 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002010 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 goto doend;
2012 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002013
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 // Disallow editing another buffer when "curbuf_lock" is set.
2015 // Do allow ":checktime" (it is postponed).
2016 // Do allow ":edit" (check for an argument later).
2017 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002018 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002019 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002020 && ea.cmdidx != CMD_edit
2021 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002022 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002023 && curbuf_locked())
2024 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002026 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002028 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 goto doend;
2031 }
2032 }
2033
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002034 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002036 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 goto doend;
2038 }
2039
2040 /*
2041 * Don't complain about the range if it is not used
2042 * (could happen if line_count is accidentally set to 0).
2043 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002044 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
2046 /*
2047 * If the range is backwards, ask for confirmation and, if given, swap
2048 * ea.line1 & ea.line2 so it's forwards again.
2049 * When global command is busy, don't ask, will fail below.
2050 */
2051 if (!global_busy && ea.line1 > ea.line2)
2052 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002053 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002055 if (sourcing || exmode_active)
2056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002057 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002058 goto doend;
2059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 if (ask_yesno((char_u *)
2061 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002062 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064 lnum = ea.line1;
2065 ea.line1 = ea.line2;
2066 ea.line2 = lnum;
2067 }
2068 if ((errormsg = invalid_range(&ea)) != NULL)
2069 goto doend;
2070 }
2071
Bram Moolenaarb7316892019-05-01 18:08:42 +02002072 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2073 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 ea.line2 = 1;
2075
2076 correct_range(&ea);
2077
2078#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002079 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002080 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002082 // Put the first line at the start of a closed fold, put the last line
2083 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 (void)hasFolding(ea.line1, &ea.line1, NULL);
2085 (void)hasFolding(ea.line2, NULL, &ea.line2);
2086 }
2087#endif
2088
2089#ifdef FEAT_QUICKFIX
2090 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002091 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 * option here, so things like % get expanded.
2093 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002094 p = replace_makeprg(&ea, p, cmdlinep);
2095 if (p == NULL)
2096 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097#endif
2098
2099 /*
2100 * Skip to start of argument.
2101 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2102 */
2103 if (ea.cmdidx == CMD_bang)
2104 ea.arg = p;
2105 else
2106 ea.arg = skipwhite(p);
2107
Bram Moolenaar379fb762018-08-30 15:58:28 +02002108 // ":file" cannot be run with an argument when "curbuf_lock" is set
2109 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2110 goto doend;
2111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 /*
2113 * Check for "++opt=val" argument.
2114 * Must be first, allow ":w ++enc=utf8 !cmd"
2115 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002116 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2118 if (getargopt(&ea) == FAIL && !ni)
2119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002120 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 goto doend;
2122 }
2123
2124 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002126 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002128 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002130 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 goto doend;
2132 }
2133 ea.arg = skipwhite(ea.arg + 1);
2134 ea.append = TRUE;
2135 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002136 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
2138 ++ea.arg;
2139 ea.usefilter = TRUE;
2140 }
2141 }
2142
2143 if (ea.cmdidx == CMD_read)
2144 {
2145 if (ea.forceit)
2146 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002147 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 ea.forceit = FALSE;
2149 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002150 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152 ++ea.arg;
2153 ea.usefilter = TRUE;
2154 }
2155 }
2156
2157 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2158 {
2159 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002160 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 ++ea.arg;
2163 ++ea.amount;
2164 }
2165 ea.arg = skipwhite(ea.arg);
2166 }
2167
2168 /*
2169 * Check for "+command" argument, before checking for next command.
2170 * Don't do this for ":read !cmd" and ":write !cmd".
2171 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002172 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2174
2175 /*
2176 * Check for '|' to separate commands and '"' to start comments.
2177 * Don't do this for ":read !cmd" and ":write !cmd".
2178 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002179 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 separate_nextcmd(&ea);
2181
2182 /*
2183 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002184 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2185 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002187 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002188 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002189 || ea.cmdidx == CMD_global
2190 || ea.cmdidx == CMD_vglobal
2191 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 for (p = ea.arg; *p; ++p)
2194 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002195 // Remove one backslash before a newline, so that it's possible to
2196 // pass a newline to the shell and also a newline that is preceded
2197 // with a backslash. This makes it impossible to end a shell
2198 // command in a backslash, but that doesn't appear useful.
2199 // Halving the number of backslashes is incompatible with previous
2200 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002202 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 else if (*p == '\n')
2204 {
2205 ea.nextcmd = p + 1;
2206 *p = NUL;
2207 break;
2208 }
2209 }
2210 }
2211
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002212 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002214 buf_T *buf;
2215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002217 switch (ea.addr_type)
2218 {
2219 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002220 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002221 ea.line2 = curbuf->b_ml.ml_line_count;
2222 break;
2223 case ADDR_LOADED_BUFFERS:
2224 buf = firstbuf;
2225 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2226 buf = buf->b_next;
2227 ea.line1 = buf->b_fnum;
2228 buf = lastbuf;
2229 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2230 buf = buf->b_prev;
2231 ea.line2 = buf->b_fnum;
2232 break;
2233 case ADDR_BUFFERS:
2234 ea.line1 = firstbuf->b_fnum;
2235 ea.line2 = lastbuf->b_fnum;
2236 break;
2237 case ADDR_WINDOWS:
2238 ea.line2 = LAST_WIN_NR;
2239 break;
2240 case ADDR_TABS:
2241 ea.line2 = LAST_TAB_NR;
2242 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002243 case ADDR_TABS_RELATIVE:
2244 ea.line2 = 1;
2245 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002246 case ADDR_ARGUMENTS:
2247 if (ARGCOUNT == 0)
2248 ea.line1 = ea.line2 = 0;
2249 else
2250 ea.line2 = ARGCOUNT;
2251 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002252 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002253#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002254 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002255 if (ea.line2 == 0)
2256 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002257#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002258 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002259 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002260 case ADDR_UNSIGNED:
2261 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002262 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002263 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
2266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002268 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002270 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002271 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002272 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002274#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002275 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002276 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002278 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002279 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002282 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2283 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002284 {
2285 ea.regname = *ea.arg++;
2286#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002287 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002288 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2289 {
2290 set_expr_line(vim_strsave(ea.arg));
2291 ea.arg += STRLEN(ea.arg);
2292 }
2293#endif
2294 ea.arg = skipwhite(ea.arg);
2295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297
2298 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002299 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 * count, it's a buffer name.
2301 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002302 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2303 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002304 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 n = getdigits(&ea.arg);
2307 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002308 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002310 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 goto doend;
2312 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002313 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 ea.line2 = n;
2316 if (ea.addr_count == 0)
2317 ea.addr_count = 1;
2318 }
2319 else
2320 {
2321 ea.line1 = ea.line2;
2322 ea.line2 += n - 1;
2323 ++ea.addr_count;
2324 /*
2325 * Be vi compatible: no error message for out of range.
2326 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002327 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 ea.line2 = curbuf->b_ml.ml_line_count;
2329 }
2330 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002331
2332 /*
2333 * Check for flags: 'l', 'p' and '#'.
2334 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002335 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002336 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002337 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2338 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002340 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002341 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 goto doend;
2343 }
2344
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002345 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002347 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 goto doend;
2349 }
2350
2351#ifdef FEAT_EVAL
2352 /*
2353 * Skip the command when it's not going to be executed.
2354 * The commands like :if, :endif, etc. always need to be executed.
2355 * Also make an exception for commands that handle a trailing command
2356 * themselves.
2357 */
2358 if (ea.skip)
2359 {
2360 switch (ea.cmdidx)
2361 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 case CMD_while:
2364 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002365 case CMD_for:
2366 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 case CMD_if:
2368 case CMD_elseif:
2369 case CMD_else:
2370 case CMD_endif:
2371 case CMD_try:
2372 case CMD_catch:
2373 case CMD_finally:
2374 case CMD_endtry:
2375 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002376 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 break;
2378
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002379 // Commands that handle '|' themselves. Check: A command should
2380 // either have the EX_TRLBAR flag, appear in this list or appear in
2381 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 case CMD_aboveleft:
2383 case CMD_and:
2384 case CMD_belowright:
2385 case CMD_botright:
2386 case CMD_browse:
2387 case CMD_call:
2388 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002389 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_delfunction:
2391 case CMD_djump:
2392 case CMD_dlist:
2393 case CMD_dsearch:
2394 case CMD_dsplit:
2395 case CMD_echo:
2396 case CMD_echoerr:
2397 case CMD_echomsg:
2398 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002399 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002401 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 case CMD_help:
2403 case CMD_hide:
2404 case CMD_ijump:
2405 case CMD_ilist:
2406 case CMD_isearch:
2407 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002408 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 case CMD_keepjumps:
2410 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002411 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 case CMD_leftabove:
2413 case CMD_let:
2414 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002415 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002416 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002418 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002419 case CMD_noautocmd:
2420 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 case CMD_perl:
2422 case CMD_psearch:
2423 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002424 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002425 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 case CMD_return:
2427 case CMD_rightbelow:
2428 case CMD_ruby:
2429 case CMD_silent:
2430 case CMD_smagic:
2431 case CMD_snomagic:
2432 case CMD_substitute:
2433 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002434 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CMD_tcl:
2436 case CMD_throw:
2437 case CMD_tilde:
2438 case CMD_topleft:
2439 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002440 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_verbose:
2442 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002443 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 break;
2445
2446 default: goto doend;
2447 }
2448 }
2449#endif
2450
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002451 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
2453 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2454 goto doend;
2455 }
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 /*
2458 * Accept buffer name. Cannot be used at the same time with a buffer
2459 * number. Don't do this for a user command.
2460 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002461 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002462 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
2464 /*
2465 * :bdelete, :bwipeout and :bunload take several arguments, separated
2466 * by spaces: find next space (skipping over escaped characters).
2467 * The others take one argument: ignore trailing spaces.
2468 */
2469 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2470 || ea.cmdidx == CMD_bunload)
2471 p = skiptowhite_esc(ea.arg);
2472 else
2473 {
2474 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002475 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 --p;
2477 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002478 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002479 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002480 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 goto doend;
2482 ea.addr_count = 1;
2483 ea.arg = skipwhite(p);
2484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002486 // The :try command saves the emsg_silent flag, reset it here when
2487 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002488 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002489 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002490 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002491 if (emsg_silent < 0)
2492 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002493 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002494 }
2495
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002497 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
Bram Moolenaar958636c2014-10-21 20:01:58 +02002500 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
2502 /*
2503 * Execute a user-defined command.
2504 */
2505 do_ucmd(&ea);
2506 }
2507 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002510 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 */
2512 ea.errmsg = NULL;
2513 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2514 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002515 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517
2518#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002520 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2521 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002522 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 /*
2525 * If the command just executed called do_cmdline(), any throw or ":return"
2526 * or ":finish" encountered there must also check the cstack of the still
2527 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2528 * exception, or reanimate a returned function or finished script file and
2529 * return or finish it again.
2530 */
2531 if (need_rethrow)
2532 do_throw(cstack);
2533 else if (check_cstack)
2534 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002535 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002537 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 && current_func_returned())
2539 do_return(&ea, TRUE, FALSE, NULL);
2540 }
2541 need_rethrow = check_cstack = FALSE;
2542#endif
2543
2544doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002545 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002548 curwin->w_cursor.col = 0;
2549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
2551 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2552 {
2553 if (sourcing)
2554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002555 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
2557 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002558 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002560 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562 emsg(errormsg);
2563 }
2564#ifdef FEAT_EVAL
2565 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002566 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2567 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568#endif
2569
Bram Moolenaareffed932018-08-14 13:38:17 +02002570 if (ea.verbose_save >= 0)
2571 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002572
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002573 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002575 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
Bram Moolenaareffed932018-08-14 13:38:17 +02002577 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002579 // messages could be enabled for a serious error, need to check if the
2580 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002581 if (!did_emsg || msg_silent > ea.save_msg_silent)
2582 msg_silent = ea.save_msg_silent;
2583 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (emsg_silent < 0)
2585 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002586 // Restore msg_scroll, it's set by file I/O commands, even when no
2587 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002590 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2591 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002592 if (redirecting())
2593 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002596#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002597 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002598 --sandbox;
2599#endif
2600
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002601 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 ea.nextcmd = NULL;
2603
2604#ifdef FEAT_EVAL
2605 --ex_nesting_level;
2606#endif
2607
2608 return ea.nextcmd;
2609}
2610#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002611 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612#endif
2613
2614/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002615 * Parse and skip over command modifiers:
2616 * - update eap->cmd
2617 * - store flags in "cmdmod".
2618 * - Set ex_pressedreturn for an empty command line.
2619 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002620 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 * - set p_verbose for ":verbose"
2622 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002623 * When "skip_only" is TRUE the global variables are not changed, except for
2624 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002625 * Return FAIL when the command is not to be executed.
2626 * May set "errormsg" to an error message.
2627 */
2628 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002629parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002630{
2631 char_u *p;
2632
Bram Moolenaara80faa82020-04-12 19:37:17 +02002633 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002634 eap->verbose_save = -1;
2635 eap->save_msg_silent = -1;
2636
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002637 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002638 for (;;)
2639 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002640 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2641 ++eap->cmd;
2642
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002643 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002644 if (*eap->cmd == NUL && exmode_active
2645 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2646 || getline_equal(eap->getline, eap->cookie, getexline))
2647 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2648 {
2649 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002650 if (!skip_only)
2651 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002652 }
2653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002654 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002655 if (*eap->cmd == '"')
2656 return FAIL;
2657 if (*eap->cmd == NUL)
2658 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002659 if (!skip_only)
2660 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002661 return FAIL;
2662 }
2663
Bram Moolenaareffed932018-08-14 13:38:17 +02002664 p = skip_range(eap->cmd, NULL);
2665 switch (*p)
2666 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002667 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002668 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2669 break;
2670 cmdmod.split |= WSP_ABOVE;
2671 continue;
2672
2673 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2674 {
2675 cmdmod.split |= WSP_BELOW;
2676 continue;
2677 }
2678 if (checkforcmd(&eap->cmd, "browse", 3))
2679 {
2680#ifdef FEAT_BROWSE_CMD
2681 cmdmod.browse = TRUE;
2682#endif
2683 continue;
2684 }
2685 if (!checkforcmd(&eap->cmd, "botright", 2))
2686 break;
2687 cmdmod.split |= WSP_BOT;
2688 continue;
2689
2690 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2691 break;
2692#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2693 cmdmod.confirm = TRUE;
2694#endif
2695 continue;
2696
2697 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2698 {
2699 cmdmod.keepmarks = TRUE;
2700 continue;
2701 }
2702 if (checkforcmd(&eap->cmd, "keepalt", 5))
2703 {
2704 cmdmod.keepalt = TRUE;
2705 continue;
2706 }
2707 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2708 {
2709 cmdmod.keeppatterns = TRUE;
2710 continue;
2711 }
2712 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2713 break;
2714 cmdmod.keepjumps = TRUE;
2715 continue;
2716
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002717 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002718 {
2719 char_u *reg_pat;
2720
2721 if (!checkforcmd(&p, "filter", 4)
2722 || *p == NUL || ends_excmd(*p))
2723 break;
2724 if (*p == '!')
2725 {
2726 cmdmod.filter_force = TRUE;
2727 p = skipwhite(p + 1);
2728 if (*p == NUL || ends_excmd(*p))
2729 break;
2730 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002731 if (skip_only)
2732 p = skip_vimgrep_pat(p, NULL, NULL);
2733 else
2734 // NOTE: This puts a NUL after the pattern.
2735 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002736 if (p == NULL || *p == NUL)
2737 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002738 if (!skip_only)
2739 {
2740 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002741 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002742 if (cmdmod.filter_regmatch.regprog == NULL)
2743 break;
2744 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002745 eap->cmd = p;
2746 continue;
2747 }
2748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002749 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002750 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2751 || *p == NUL || ends_excmd(*p))
2752 break;
2753 eap->cmd = p;
2754 cmdmod.hide = TRUE;
2755 continue;
2756
2757 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2758 {
2759 cmdmod.lockmarks = TRUE;
2760 continue;
2761 }
2762
2763 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2764 break;
2765 cmdmod.split |= WSP_ABOVE;
2766 continue;
2767
2768 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2769 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002770 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002771 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002772 // Set 'eventignore' to "all". Restore the
2773 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002774 cmdmod.save_ei = vim_strsave(p_ei);
2775 set_string_option_direct((char_u *)"ei", -1,
2776 (char_u *)"all", OPT_FREE, SID_NONE);
2777 }
2778 continue;
2779 }
2780 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2781 break;
2782 cmdmod.noswapfile = TRUE;
2783 continue;
2784
2785 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2786 break;
2787 cmdmod.split |= WSP_BELOW;
2788 continue;
2789
2790 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2791 {
2792#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002793 if (!skip_only)
2794 {
2795 if (!eap->did_sandbox)
2796 ++sandbox;
2797 eap->did_sandbox = TRUE;
2798 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002799#endif
2800 continue;
2801 }
2802 if (!checkforcmd(&eap->cmd, "silent", 3))
2803 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002804 if (!skip_only)
2805 {
2806 if (eap->save_msg_silent == -1)
2807 eap->save_msg_silent = msg_silent;
2808 ++msg_silent;
2809 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2811 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002812 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002813 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002814 if (!skip_only)
2815 {
2816 ++emsg_silent;
2817 ++eap->did_esilent;
2818 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 }
2820 continue;
2821
2822 case 't': if (checkforcmd(&p, "tab", 3))
2823 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002824 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002826 long tabnr = get_address(eap, &eap->cmd,
2827 ADDR_TABS, eap->skip,
2828 skip_only, FALSE, 1);
2829 if (tabnr == MAXLNUM)
2830 cmdmod.tab = tabpage_index(curtab) + 1;
2831 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002833 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002835 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002836 return FAIL;
2837 }
2838 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 }
2841 eap->cmd = p;
2842 continue;
2843 }
2844 if (!checkforcmd(&eap->cmd, "topleft", 2))
2845 break;
2846 cmdmod.split |= WSP_TOP;
2847 continue;
2848
2849 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2850 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002851 if (!skip_only)
2852 {
2853 if (eap->save_msg_silent == -1)
2854 eap->save_msg_silent = msg_silent;
2855 msg_silent = 0;
2856 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 continue;
2858
2859 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2860 {
2861 cmdmod.split |= WSP_VERT;
2862 continue;
2863 }
2864 if (!checkforcmd(&p, "verbose", 4))
2865 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002866 if (!skip_only)
2867 {
2868 if (eap->verbose_save < 0)
2869 eap->verbose_save = p_verbose;
2870 if (vim_isdigit(*eap->cmd))
2871 p_verbose = atoi((char *)eap->cmd);
2872 else
2873 p_verbose = 1;
2874 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 eap->cmd = p;
2876 continue;
2877 }
2878 break;
2879 }
2880
2881 return OK;
2882}
2883
2884/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002885 * Free contents of "cmdmod".
2886 */
2887 static void
2888free_cmdmod(void)
2889{
2890 if (cmdmod.save_ei != NULL)
2891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002892 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002893 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2894 OPT_FREE, SID_NONE);
2895 free_string_option(cmdmod.save_ei);
2896 }
2897
2898 if (cmdmod.filter_regmatch.regprog != NULL)
2899 vim_regfree(cmdmod.filter_regmatch.regprog);
2900}
2901
2902/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002903 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002904 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002905 * Return FAIL and set "errormsg" or return OK.
2906 */
2907 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002908parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002909{
2910 int address_count = 1;
2911 linenr_T lnum;
2912
2913 // Repeat for all ',' or ';' separated addresses.
2914 for (;;)
2915 {
2916 eap->line1 = eap->line2;
2917 switch (eap->addr_type)
2918 {
2919 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002920 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002921 // default is current line number
2922 eap->line2 = curwin->w_cursor.lnum;
2923 break;
2924 case ADDR_WINDOWS:
2925 eap->line2 = CURRENT_WIN_NR;
2926 break;
2927 case ADDR_ARGUMENTS:
2928 eap->line2 = curwin->w_arg_idx + 1;
2929 if (eap->line2 > ARGCOUNT)
2930 eap->line2 = ARGCOUNT;
2931 break;
2932 case ADDR_LOADED_BUFFERS:
2933 case ADDR_BUFFERS:
2934 eap->line2 = curbuf->b_fnum;
2935 break;
2936 case ADDR_TABS:
2937 eap->line2 = CURRENT_TAB_NR;
2938 break;
2939 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002940 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002941 eap->line2 = 1;
2942 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002943 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002944#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002945 eap->line2 = qf_get_cur_idx(eap);
2946#endif
2947 break;
2948 case ADDR_QUICKFIX_VALID:
2949#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002950 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002951#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002952 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002953 case ADDR_NONE:
2954 // Will give an error later if a range is found.
2955 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002956 }
2957 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002958 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002959 eap->addr_count == 0, address_count++);
2960 if (eap->cmd == NULL) // error detected
2961 return FAIL;
2962 if (lnum == MAXLNUM)
2963 {
2964 if (*eap->cmd == '%') // '%' - all lines
2965 {
2966 ++eap->cmd;
2967 switch (eap->addr_type)
2968 {
2969 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002970 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002971 eap->line1 = 1;
2972 eap->line2 = curbuf->b_ml.ml_line_count;
2973 break;
2974 case ADDR_LOADED_BUFFERS:
2975 {
2976 buf_T *buf = firstbuf;
2977
2978 while (buf->b_next != NULL
2979 && buf->b_ml.ml_mfp == NULL)
2980 buf = buf->b_next;
2981 eap->line1 = buf->b_fnum;
2982 buf = lastbuf;
2983 while (buf->b_prev != NULL
2984 && buf->b_ml.ml_mfp == NULL)
2985 buf = buf->b_prev;
2986 eap->line2 = buf->b_fnum;
2987 break;
2988 }
2989 case ADDR_BUFFERS:
2990 eap->line1 = firstbuf->b_fnum;
2991 eap->line2 = lastbuf->b_fnum;
2992 break;
2993 case ADDR_WINDOWS:
2994 case ADDR_TABS:
2995 if (IS_USER_CMDIDX(eap->cmdidx))
2996 {
2997 eap->line1 = 1;
2998 eap->line2 = eap->addr_type == ADDR_WINDOWS
2999 ? LAST_WIN_NR : LAST_TAB_NR;
3000 }
3001 else
3002 {
3003 // there is no Vim command which uses '%' and
3004 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003005 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003006 return FAIL;
3007 }
3008 break;
3009 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003010 case ADDR_UNSIGNED:
3011 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003012 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003013 return FAIL;
3014 case ADDR_ARGUMENTS:
3015 if (ARGCOUNT == 0)
3016 eap->line1 = eap->line2 = 0;
3017 else
3018 {
3019 eap->line1 = 1;
3020 eap->line2 = ARGCOUNT;
3021 }
3022 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003023 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003024#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003025 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003026 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003027 if (eap->line2 == 0)
3028 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003029#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003030 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003031 case ADDR_NONE:
3032 // Will give an error later if a range is found.
3033 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003034 }
3035 ++eap->addr_count;
3036 }
3037 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3038 {
3039 pos_T *fp;
3040
3041 // '*' - visual area
3042 if (eap->addr_type != ADDR_LINES)
3043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003044 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003045 return FAIL;
3046 }
3047
3048 ++eap->cmd;
3049 if (!eap->skip)
3050 {
3051 fp = getmark('<', FALSE);
3052 if (check_mark(fp) == FAIL)
3053 return FAIL;
3054 eap->line1 = fp->lnum;
3055 fp = getmark('>', FALSE);
3056 if (check_mark(fp) == FAIL)
3057 return FAIL;
3058 eap->line2 = fp->lnum;
3059 ++eap->addr_count;
3060 }
3061 }
3062 }
3063 else
3064 eap->line2 = lnum;
3065 eap->addr_count++;
3066
3067 if (*eap->cmd == ';')
3068 {
3069 if (!eap->skip)
3070 {
3071 curwin->w_cursor.lnum = eap->line2;
3072 // don't leave the cursor on an illegal line or column
3073 check_cursor();
3074 }
3075 }
3076 else if (*eap->cmd != ',')
3077 break;
3078 ++eap->cmd;
3079 }
3080
3081 // One address given: set start and end lines.
3082 if (eap->addr_count == 1)
3083 {
3084 eap->line1 = eap->line2;
3085 // ... but only implicit: really no address given
3086 if (lnum == MAXLNUM)
3087 eap->addr_count = 0;
3088 }
3089 return OK;
3090}
3091
3092/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003093 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 * If there is a match advance "pp" to the argument and return TRUE.
3095 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003096 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003097checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003098 char_u **pp, // start of command
3099 char *cmd, // name of command
3100 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101{
3102 int i;
3103
3104 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003105 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 break;
3107 if (i >= len && !isalpha((*pp)[i]))
3108 {
3109 *pp = skipwhite(*pp + i);
3110 return TRUE;
3111 }
3112 return FALSE;
3113}
3114
3115/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003116 * Append "cmd" to the error message in IObuff.
3117 * Takes care of limiting the length and handling 0xa0, which would be
3118 * invisible otherwise.
3119 */
3120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003121append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003122{
3123 char_u *s = cmd;
3124 char_u *d;
3125
3126 STRCAT(IObuff, ": ");
3127 d = IObuff + STRLEN(IObuff);
3128 while (*s != NUL && d - IObuff < IOSIZE - 7)
3129 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003130 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003131 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003132 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003133 STRCPY(d, "<a0>");
3134 d += 4;
3135 }
3136 else
3137 MB_COPY_CHAR(s, d);
3138 }
3139 *d = NUL;
3140}
3141
3142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003144 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003145 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 *
3148 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3149 * assignment without "let". Sets eap->cmdidx to the command while returning
3150 * "eap->cmd".
3151 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 * Returns NULL for an ambiguous user command.
3153 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 char_u *
3155find_ex_command(
3156 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003157 int *full UNUSED,
3158 int (*lookup)(char_u *, size_t, cctx_T *) UNUSED,
3159 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
3161 int len;
3162 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003163 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165#ifdef FEAT_EVAL
3166 /*
3167 * Recognize a Vim9 script function/method call and assignment:
3168 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3169 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003170 p = eap->cmd;
3171 if (lookup != NULL && (*p == '('
3172 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 {
3174 int oplen;
3175 int heredoc;
3176
3177 // "funcname(" is always a function call.
3178 // "varname[]" is an expression.
3179 // "g:varname" is an expression.
3180 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003181 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 if (*p == '('
3183 || *p == '['
3184 || p[1] == ':'
3185 || (*p == '-' && p[1] == '>'))
3186 {
3187 eap->cmdidx = CMD_eval;
3188 return eap->cmd;
3189 }
3190
3191 oplen = assignment_len(skipwhite(p), &heredoc);
3192 if (oplen > 0)
3193 {
3194 // Recognize an assignment if we recognize the variable name:
3195 // "g:var = expr"
3196 // "var = expr" where "var" is a local var name.
3197 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3198 || lookup(eap->cmd, p - eap->cmd, cctx) >= 0)
3199 {
3200 eap->cmdidx = CMD_let;
3201 return eap->cmd;
3202 }
3203 }
3204 }
3205#endif
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 /*
3208 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003209 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 * - the 'k' command can directly be followed by any character.
3211 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003212 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003214 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 */
3216 p = eap->cmd;
3217 if (*p == 'k')
3218 {
3219 eap->cmdidx = CMD_k;
3220 ++p;
3221 }
3222 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003223 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3224 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 || p[1] == 'g'
3226 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3227 || p[1] == 'I'
3228 || (p[1] == 'r' && p[2] != 'e')))
3229 {
3230 eap->cmdidx = CMD_substitute;
3231 ++p;
3232 }
3233 else
3234 {
3235 while (ASCII_ISALPHA(*p))
3236 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003237 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003238 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003240 while (ASCII_ISALNUM(*p))
3241 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 }
3243 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3244 {
3245 // include "9" for "vim9script"
3246 ++p;
3247 while (ASCII_ISALPHA(*p))
3248 ++p;
3249 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003251 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3253 ++p;
3254 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003255 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3256 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003257 // Check for ":dl", ":dell", etc. to ":deletel": that's
3258 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003259 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003260 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003261 break;
3262 if (i == len - 1)
3263 {
3264 --len;
3265 if (p[-1] == 'l')
3266 eap->flags |= EXFLAG_LIST;
3267 else
3268 eap->flags |= EXFLAG_PRINT;
3269 }
3270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003272 if (ASCII_ISLOWER(eap->cmd[0]))
3273 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003274 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003275 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003276
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003277 if (command_count != (int)CMD_SIZE)
3278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003279 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003280 getout(1);
3281 }
3282
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003283 // Use a precomputed index for fast look-up in cmdnames[]
3284 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003285 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3286 if (ASCII_ISLOWER(c2))
3287 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3288 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003289 else if (ASCII_ISUPPER(eap->cmd[0]))
3290 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003292 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3295 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3296 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3297 (size_t)len) == 0)
3298 {
3299#ifdef FEAT_EVAL
3300 if (full != NULL
3301 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3302 *full = TRUE;
3303#endif
3304 break;
3305 }
3306
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003307 // Look for a user defined command as a last resort. Let ":Print" be
3308 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003309 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3310 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003312 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 while (ASCII_ISALNUM(*p))
3314 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003315 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 eap->cmdidx = CMD_SIZE;
3319 }
3320
3321 return p;
3322}
3323
3324#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003325static struct cmdmod
3326{
3327 char *name;
3328 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003329 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003330} cmdmods[] = {
3331 {"aboveleft", 3, FALSE},
3332 {"belowright", 3, FALSE},
3333 {"botright", 2, FALSE},
3334 {"browse", 3, FALSE},
3335 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003336 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003337 {"hide", 3, FALSE},
3338 {"keepalt", 5, FALSE},
3339 {"keepjumps", 5, FALSE},
3340 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003341 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003342 {"leftabove", 5, FALSE},
3343 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003344 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003345 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003346 {"rightbelow", 6, FALSE},
3347 {"sandbox", 3, FALSE},
3348 {"silent", 3, FALSE},
3349 {"tab", 3, TRUE},
3350 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003351 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003352 {"verbose", 4, TRUE},
3353 {"vertical", 4, FALSE},
3354};
3355
3356/*
3357 * Return length of a command modifier (including optional count).
3358 * Return zero when it's not a modifier.
3359 */
3360 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003361modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003362{
3363 int i, j;
3364 char_u *p = cmd;
3365
3366 if (VIM_ISDIGIT(*cmd))
3367 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003368 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003369 {
3370 for (j = 0; p[j] != NUL; ++j)
3371 if (p[j] != cmdmods[i].name[j])
3372 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003373 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003374 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003375 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003376 }
3377 return 0;
3378}
3379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380/*
3381 * Return > 0 if an Ex command "name" exists.
3382 * Return 2 if there is an exact match.
3383 * Return 3 if there is an ambiguous match.
3384 */
3385 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003386cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 exarg_T ea;
3389 int full = FALSE;
3390 int i;
3391 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003392 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003394 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003395 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
3397 for (j = 0; name[j] != NUL; ++j)
3398 if (name[j] != cmdmods[i].name[j])
3399 break;
3400 if (name[j] == NUL && j >= cmdmods[i].minlen)
3401 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3402 }
3403
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003404 // Check built-in commands and user defined commands.
3405 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003406 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003409 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003411 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3412 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003413 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003414 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3416}
3417#endif
3418
Bram Moolenaard0190392019-08-23 21:17:35 +02003419 cmdidx_T
3420excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
Bram Moolenaard0190392019-08-23 21:17:35 +02003422 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaard0190392019-08-23 21:17:35 +02003424 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3425 idx = (cmdidx_T)((int)idx + 1))
3426 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 break;
3428
Bram Moolenaard0190392019-08-23 21:17:35 +02003429 return idx;
3430}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaard0190392019-08-23 21:17:35 +02003432 long
3433excmd_get_argt(cmdidx_T idx)
3434{
3435 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436}
3437
3438/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003439 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 *
3441 * Backslashed delimiters after / or ? will be skipped, and commands will
3442 * not be expanded between /'s and ?'s or after "'".
3443 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003444 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 * Returns the "cmd" pointer advanced to beyond the range.
3446 */
3447 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003448skip_range(
3449 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003450 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003452 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003454 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003456 if (*cmd == '\\')
3457 {
3458 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3459 ++cmd;
3460 else
3461 break;
3462 }
3463 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 if (*++cmd == NUL && ctx != NULL)
3466 *ctx = EXPAND_NOTHING;
3467 }
3468 else if (*cmd == '/' || *cmd == '?')
3469 {
3470 delim = *cmd++;
3471 while (*cmd != NUL && *cmd != delim)
3472 if (*cmd++ == '\\' && *cmd != NUL)
3473 ++cmd;
3474 if (*cmd == NUL && ctx != NULL)
3475 *ctx = EXPAND_NOTHING;
3476 }
3477 if (*cmd != NUL)
3478 ++cmd;
3479 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003481 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003482 while (*cmd == ':')
3483 cmd = skipwhite(cmd + 1);
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 return cmd;
3486}
3487
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003488 static void
3489addr_error(cmd_addr_T addr_type)
3490{
3491 if (addr_type == ADDR_NONE)
3492 emsg(_(e_norange));
3493 else
3494 emsg(_(e_invrange));
3495}
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003498 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 *
3500 * Set ptr to the next character after the part that was interpreted.
3501 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003502 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 *
3504 * Return MAXLNUM when no Ex address was found.
3505 */
3506 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003507get_address(
3508 exarg_T *eap UNUSED,
3509 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003510 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003511 int skip, // only skip the address, don't use it
3512 int silent, // no errors or side effects
3513 int to_other_file, // flag: may jump to other file
3514 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 int c;
3517 int i;
3518 long n;
3519 char_u *cmd;
3520 pos_T pos;
3521 pos_T *fp;
3522 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003523 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 cmd = skipwhite(*ptr);
3526 lnum = MAXLNUM;
3527 do
3528 {
3529 switch (*cmd)
3530 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003531 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003532 ++cmd;
3533 switch (addr_type)
3534 {
3535 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003536 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 lnum = curwin->w_cursor.lnum;
3538 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003539 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003540 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003541 break;
3542 case ADDR_ARGUMENTS:
3543 lnum = curwin->w_arg_idx + 1;
3544 break;
3545 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003546 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003547 lnum = curbuf->b_fnum;
3548 break;
3549 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003550 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003551 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003552 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003553 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003554 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003555 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003556 cmd = NULL;
3557 goto error;
3558 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003559 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003560#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003561 lnum = qf_get_cur_idx(eap);
3562#endif
3563 break;
3564 case ADDR_QUICKFIX_VALID:
3565#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003566 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003567#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003568 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003569 }
3570 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003572 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003573 ++cmd;
3574 switch (addr_type)
3575 {
3576 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003577 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 lnum = curbuf->b_ml.ml_line_count;
3579 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003580 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003581 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003582 break;
3583 case ADDR_ARGUMENTS:
3584 lnum = ARGCOUNT;
3585 break;
3586 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003587 buf = lastbuf;
3588 while (buf->b_ml.ml_mfp == NULL)
3589 {
3590 if (buf->b_prev == NULL)
3591 break;
3592 buf = buf->b_prev;
3593 }
3594 lnum = buf->b_fnum;
3595 break;
3596 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003597 lnum = lastbuf->b_fnum;
3598 break;
3599 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003600 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003601 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003602 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003603 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003604 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003605 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003606 cmd = NULL;
3607 goto error;
3608 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003609 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003610#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003611 lnum = qf_get_size(eap);
3612 if (lnum == 0)
3613 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003614#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003615 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003616 case ADDR_QUICKFIX_VALID:
3617#ifdef FEAT_QUICKFIX
3618 lnum = qf_get_valid_size(eap);
3619 if (lnum == 0)
3620 lnum = 1;
3621#endif
3622 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003623 }
3624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003626 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003627 if (*++cmd == NUL)
3628 {
3629 cmd = NULL;
3630 goto error;
3631 }
3632 if (addr_type != ADDR_LINES)
3633 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003634 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003635 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003636 goto error;
3637 }
3638 if (skip)
3639 ++cmd;
3640 else
3641 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003642 // Only accept a mark in another file when it is
3643 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003644 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3645 ++cmd;
3646 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003647 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003648 lnum = curwin->w_cursor.lnum;
3649 else
3650 {
3651 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
3653 cmd = NULL;
3654 goto error;
3655 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003656 lnum = fp->lnum;
3657 }
3658 }
3659 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003662 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003663 c = *cmd++;
3664 if (addr_type != ADDR_LINES)
3665 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003666 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003667 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003668 goto error;
3669 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003670 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003671 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003672 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003673 if (*cmd == c)
3674 ++cmd;
3675 }
3676 else
3677 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003678 int flags;
3679
3680 pos = curwin->w_cursor; // save curwin->w_cursor
3681
3682 // When '/' or '?' follows another address, start from
3683 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003684 if (lnum != MAXLNUM)
3685 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003686
3687 // Start a forward search at the end of the line (unless
3688 // before the first line).
3689 // Start a backward search at the start of the line.
3690 // This makes sure we never match in the current
3691 // line, and can match anywhere in the
3692 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003693 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003694 curwin->w_cursor.col = MAXCOL;
3695 else
3696 curwin->w_cursor.col = 0;
3697 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003698 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003699 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003700 {
3701 curwin->w_cursor = pos;
3702 cmd = NULL;
3703 goto error;
3704 }
3705 lnum = curwin->w_cursor.lnum;
3706 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003707 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003708 cmd += searchcmdlen;
3709 }
3710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003712 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003713 ++cmd;
3714 if (addr_type != ADDR_LINES)
3715 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003716 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003717 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003718 goto error;
3719 }
3720 if (*cmd == '&')
3721 i = RE_SUBST;
3722 else if (*cmd == '?' || *cmd == '/')
3723 i = RE_SEARCH;
3724 else
3725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003726 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003727 cmd = NULL;
3728 goto error;
3729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003731 if (!skip)
3732 {
3733 /*
3734 * When search follows another address, start from
3735 * there.
3736 */
3737 if (lnum != MAXLNUM)
3738 pos.lnum = lnum;
3739 else
3740 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003742 /*
3743 * Start the search just like for the above
3744 * do_search().
3745 */
3746 if (*cmd != '?')
3747 pos.col = MAXCOL;
3748 else
3749 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003750 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003751 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003752 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003753 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003754 lnum = pos.lnum;
3755 else
3756 {
3757 cmd = NULL;
3758 goto error;
3759 }
3760 }
3761 ++cmd;
3762 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003765 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003766 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
3768
3769 for (;;)
3770 {
3771 cmd = skipwhite(cmd);
3772 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3773 break;
3774
3775 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003776 {
3777 switch (addr_type)
3778 {
3779 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003780 case ADDR_OTHER:
3781 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003782 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003783 break;
3784 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003785 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003786 break;
3787 case ADDR_ARGUMENTS:
3788 lnum = curwin->w_arg_idx + 1;
3789 break;
3790 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003791 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003792 lnum = curbuf->b_fnum;
3793 break;
3794 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003795 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003796 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003797 case ADDR_TABS_RELATIVE:
3798 lnum = 1;
3799 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003800 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003801#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003802 lnum = qf_get_cur_idx(eap);
3803#endif
3804 break;
3805 case ADDR_QUICKFIX_VALID:
3806#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003807 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003808#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003809 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003810 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003811 case ADDR_UNSIGNED:
3812 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003813 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003814 }
3815 }
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003818 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 else
3820 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003821 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 n = 1;
3823 else
3824 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003825
3826 if (addr_type == ADDR_TABS_RELATIVE)
3827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003828 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003829 cmd = NULL;
3830 goto error;
3831 }
3832 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003833 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003834 lnum = compute_buffer_local_count(
3835 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003837 {
3838#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003839 // Relative line addressing, need to adjust for folded lines
3840 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003841 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3842 && address_count >= 2)
3843 (void)hasFolding(lnum, NULL, &lnum);
3844#endif
3845 if (i == '-')
3846 lnum -= n;
3847 else
3848 lnum += n;
3849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851 } while (*cmd == '/' || *cmd == '?');
3852
3853error:
3854 *ptr = cmd;
3855 return lnum;
3856}
3857
3858/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003859 * Get flags from an Ex command argument.
3860 */
3861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003862get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003863{
3864 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3865 {
3866 if (*eap->arg == 'l')
3867 eap->flags |= EXFLAG_LIST;
3868 else if (*eap->arg == 'p')
3869 eap->flags |= EXFLAG_PRINT;
3870 else
3871 eap->flags |= EXFLAG_NR;
3872 eap->arg = skipwhite(eap->arg + 1);
3873 }
3874}
3875
3876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 * Function called for command which is Not Implemented. NI!
3878 */
3879 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003880ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
3882 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003883 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884}
3885
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003886#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887/*
3888 * Function called for script command which is Not Implemented. NI!
3889 * Skips over ":perl <<EOF" constructs.
3890 */
3891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003892ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
3894 if (!eap->skip)
3895 ex_ni(eap);
3896 else
3897 vim_free(script_get(eap, eap->arg));
3898}
3899#endif
3900
3901/*
3902 * Check range in Ex command for validity.
3903 * Return NULL when valid, error message when invalid.
3904 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003905 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003906invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003908 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003909
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 if ( eap->line1 < 0
3911 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003912 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003913 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003914
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003915 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003916 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003917 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003918 {
3919 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003920 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003921#ifdef FEAT_DIFF
3922 + (eap->cmdidx == CMD_diffget)
3923#endif
3924 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003925 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003926 break;
3927 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003928 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003929 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003930 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003931 break;
3932 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003933 // Only a boundary check, not whether the buffers actually
3934 // exist.
3935 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003936 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003937 break;
3938 case ADDR_LOADED_BUFFERS:
3939 buf = firstbuf;
3940 while (buf->b_ml.ml_mfp == NULL)
3941 {
3942 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003943 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003944 buf = buf->b_next;
3945 }
3946 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003947 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003948 buf = lastbuf;
3949 while (buf->b_ml.ml_mfp == NULL)
3950 {
3951 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003952 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003953 buf = buf->b_prev;
3954 }
3955 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003956 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003957 break;
3958 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003959 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003960 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003961 break;
3962 case ADDR_TABS:
3963 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003964 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003965 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003966 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003967 case ADDR_OTHER:
3968 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003969 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003970 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003971#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003972 // No error for value that is too big, will use the last entry.
3973 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003974 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003975#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003976 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003977 case ADDR_QUICKFIX_VALID:
3978#ifdef FEAT_QUICKFIX
3979 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3980 || eap->line2 < 0)
3981 return _(e_invrange);
3982#endif
3983 break;
3984 case ADDR_UNSIGNED:
3985 if (eap->line2 < 0)
3986 return _(e_invrange);
3987 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003988 case ADDR_NONE:
3989 // Will give an error elsewhere.
3990 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003991 }
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 return NULL;
3994}
3995
3996/*
3997 * Correct the range for zero line number, if required.
3998 */
3999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004000correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004002 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
4004 if (eap->line1 == 0)
4005 eap->line1 = 1;
4006 if (eap->line2 == 0)
4007 eap->line2 = 1;
4008 }
4009}
4010
Bram Moolenaar748bf032005-02-02 23:04:36 +00004011#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004012/*
4013 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4014 * pattern. Otherwise return eap->arg.
4015 */
4016 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004017skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004018{
4019 char_u *p = eap->arg;
4020
Bram Moolenaara37420f2006-02-04 22:37:47 +00004021 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4022 || eap->cmdidx == CMD_vimgrepadd
4023 || eap->cmdidx == CMD_lvimgrepadd
4024 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004025 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004026 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004027 if (p == NULL)
4028 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004029 }
4030 return p;
4031}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004032
4033/*
4034 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4035 * in the command line, so that things like % get expanded.
4036 */
4037 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004038replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004039{
4040 char_u *new_cmdline;
4041 char_u *program;
4042 char_u *pos;
4043 char_u *ptr;
4044 int len;
4045 int i;
4046
4047 /*
4048 * Don't do it when ":vimgrep" is used for ":grep".
4049 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004050 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4051 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4052 || eap->cmdidx == CMD_grepadd
4053 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004054 && !grep_internal(eap->cmdidx))
4055 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004056 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4057 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004058 {
4059 if (*curbuf->b_p_gp == NUL)
4060 program = p_gp;
4061 else
4062 program = curbuf->b_p_gp;
4063 }
4064 else
4065 {
4066 if (*curbuf->b_p_mp == NUL)
4067 program = p_mp;
4068 else
4069 program = curbuf->b_p_mp;
4070 }
4071
4072 p = skipwhite(p);
4073
4074 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4075 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004076 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004077 i = 1;
4078 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4079 ++i;
4080 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004081 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004082 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004083 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004084 ptr = new_cmdline;
4085 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4086 {
4087 i = (int)(pos - program);
4088 STRNCPY(ptr, program, i);
4089 STRCPY(ptr += i, p);
4090 ptr += len;
4091 program = pos + 2;
4092 }
4093 STRCPY(ptr, program);
4094 }
4095 else
4096 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004097 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004098 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004099 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004100 STRCPY(new_cmdline, program);
4101 STRCAT(new_cmdline, " ");
4102 STRCAT(new_cmdline, p);
4103 }
4104 msg_make(p);
4105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004106 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004107 vim_free(*cmdlinep);
4108 *cmdlinep = new_cmdline;
4109 p = new_cmdline;
4110 }
4111 return p;
4112}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004113#endif
4114
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115/*
4116 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004117 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * Return FAIL for failure, OK otherwise.
4119 */
4120 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004121expand_filename(
4122 exarg_T *eap,
4123 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004124 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004126 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 char_u *repl;
4128 int srclen;
4129 char_u *p;
4130 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004131 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
Bram Moolenaar748bf032005-02-02 23:04:36 +00004133#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004134 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004135 p = skip_grep_pat(eap);
4136#else
4137 p = eap->arg;
4138#endif
4139
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 /*
4141 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4142 * the file name contains a wildcard it should not cause expanding.
4143 * (it will be expanded anyway if there is a wildcard before replacing).
4144 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004145 has_wildcards = mch_has_wildcard(p);
4146 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004148#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004149 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004150 if (p[0] == '`' && p[1] == '=')
4151 {
4152 p += 2;
4153 (void)skip_expr(&p);
4154 if (*p == '`')
4155 ++p;
4156 continue;
4157 }
4158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 /*
4160 * Quick check if this cannot be the start of a special string.
4161 * Also removes backslash before '%', '#' and '<'.
4162 */
4163 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4164 {
4165 ++p;
4166 continue;
4167 }
4168
4169 /*
4170 * Try to find a match at this position.
4171 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004172 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4173 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004174 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004176 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 p += srclen;
4179 continue;
4180 }
4181
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004182 // Wildcards won't be expanded below, the replacement is taken
4183 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004184 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4185 {
4186 char_u *l = repl;
4187
4188 repl = expand_env_save(repl);
4189 vim_free(l);
4190 }
4191
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004192 // Need to escape white space et al. with a backslash.
4193 // Don't do this for:
4194 // - replacement that already has been escaped: "##"
4195 // - shell commands (may have to use quotes instead).
4196 // - non-unix systems when there is a single argument (spaces don't
4197 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004199 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 && eap->cmdidx != CMD_grep
4202 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004203 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004204 && eap->cmdidx != CMD_lgrep
4205 && eap->cmdidx != CMD_lgrepadd
4206 && eap->cmdidx != CMD_lmake
4207 && eap->cmdidx != CMD_make
4208 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004210 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211#endif
4212 )
4213 {
4214 char_u *l;
4215#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004216 // Don't escape a backslash here, because rem_backslash() doesn't
4217 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 static char_u *nobslash = (char_u *)" \t\"|";
4219# define ESCAPE_CHARS nobslash
4220#else
4221# define ESCAPE_CHARS escape_chars
4222#endif
4223
4224 for (l = repl; *l; ++l)
4225 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4226 {
4227 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4228 if (l != NULL)
4229 {
4230 vim_free(repl);
4231 repl = l;
4232 }
4233 break;
4234 }
4235 }
4236
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004237 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004238 if ((eap->usefilter || eap->cmdidx == CMD_bang
4239 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004240 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
4242 char_u *l;
4243
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004244 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 if (l != NULL)
4246 {
4247 vim_free(repl);
4248 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 }
4251
4252 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4253 vim_free(repl);
4254 if (p == NULL)
4255 return FAIL;
4256 }
4257
4258 /*
4259 * One file argument: Expand wildcards.
4260 * Don't do this with ":r !command" or ":w !command".
4261 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004262 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
4264 /*
4265 * May do this twice:
4266 * 1. Replace environment variables.
4267 * 2. Replace any other wildcards, remove backslashes.
4268 */
4269 for (n = 1; n <= 2; ++n)
4270 {
4271 if (n == 2)
4272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 /*
4274 * Halve the number of backslashes (this is Vi compatible).
4275 * For Unix and OS/2, when wildcards are expanded, this is
4276 * done by ExpandOne() below.
4277 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004278#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (!has_wildcards)
4280#endif
4281 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283
4284 if (has_wildcards)
4285 {
4286 if (n == 1)
4287 {
4288 /*
4289 * First loop: May expand environment variables. This
4290 * can be done much faster with expand_env() than with
4291 * something else (e.g., calling a shell).
4292 * After expanding environment variables, check again
4293 * if there are still wildcards present.
4294 */
4295 if (vim_strchr(eap->arg, '$') != NULL
4296 || vim_strchr(eap->arg, '~') != NULL)
4297 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004298 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004299 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 has_wildcards = mch_has_wildcard(NameBuff);
4301 p = NameBuff;
4302 }
4303 else
4304 p = NULL;
4305 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004306 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
4308 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004309 int options = WILD_LIST_NOTFOUND
4310 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 ExpandInit(&xpc);
4313 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004314 if (p_wic)
4315 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004317 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (p == NULL)
4319 return FAIL;
4320 }
4321 if (p != NULL)
4322 {
4323 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4324 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004325 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 vim_free(p);
4327 }
4328 }
4329 }
4330 }
4331 return OK;
4332}
4333
4334/*
4335 * Replace part of the command line, keeping eap->cmd, eap->arg and
4336 * eap->nextcmd correct.
4337 * "src" points to the part that is to be replaced, of length "srclen".
4338 * "repl" is the replacement string.
4339 * Returns a pointer to the character after the replaced string.
4340 * Returns NULL for failure.
4341 */
4342 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004343repl_cmdline(
4344 exarg_T *eap,
4345 char_u *src,
4346 int srclen,
4347 char_u *repl,
4348 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
4350 int len;
4351 int i;
4352 char_u *new_cmdline;
4353
4354 /*
4355 * The new command line is build in new_cmdline[].
4356 * First allocate it.
4357 * Careful: a "+cmd" argument may have been NUL terminated.
4358 */
4359 len = (int)STRLEN(repl);
4360 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004361 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004362 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004363 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004364 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
4366 /*
4367 * Copy the stuff before the expanded part.
4368 * Copy the expanded stuff.
4369 * Copy what came after the expanded part.
4370 * Copy the next commands, if there are any.
4371 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004376 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004378 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004380 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
4382 i = (int)STRLEN(new_cmdline) + 1;
4383 STRCPY(new_cmdline + i, eap->nextcmd);
4384 eap->nextcmd = new_cmdline + i;
4385 }
4386 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4387 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4388 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4389 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4390 vim_free(*cmdlinep);
4391 *cmdlinep = new_cmdline;
4392
4393 return src;
4394}
4395
4396/*
4397 * Check for '|' to separate commands and '"' to start comments.
4398 */
4399 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004400separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
4402 char_u *p;
4403
Bram Moolenaar86b68352004-12-27 21:59:20 +00004404#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004405 p = skip_grep_pat(eap);
4406#else
4407 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004408#endif
4409
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004410 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
4412 if (*p == Ctrl_V)
4413 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004414 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004415 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004417 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004418 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004419 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 break;
4421 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004422
4423#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004424 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004425 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004426 {
4427 p += 2;
4428 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004429 }
4430#endif
4431
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004432 // Check for '"': start of comment or '|': next command
4433 // :@" and :*" do not start a comment!
4434 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004435 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4437 || p != eap->arg)
4438 && (eap->cmdidx != CMD_redir
4439 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004440#ifdef FEAT_EVAL
4441 || (*p == '#' && in_vim9script()
4442 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 || *p == '|' || *p == '\n')
4445 {
4446 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004447 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 * AND 'b' is present in 'cpoptions'.
4449 */
4450 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004451 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004453 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 --p;
4455 }
4456 else
4457 {
4458 eap->nextcmd = check_nextcmd(p);
4459 *p = NUL;
4460 break;
4461 }
4462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004464
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004465 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 del_trailing_spaces(eap->arg);
4467}
4468
4469/*
4470 * get + command from ex argument
4471 */
4472 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004473getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
4475 char_u *arg = *argp;
4476 char_u *command = NULL;
4477
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004478 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004481 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 command = dollar_command;
4483 else
4484 {
4485 command = arg;
4486 arg = skip_cmd_arg(command, TRUE);
4487 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004488 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 *argp = arg;
4493 }
4494 return command;
4495}
4496
4497/*
4498 * Find end of "+command" argument. Skip over "\ " and "\\".
4499 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004500 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004501skip_cmd_arg(
4502 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004503 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504{
4505 while (*p && !vim_isspace(*p))
4506 {
4507 if (*p == '\\' && p[1] != NUL)
4508 {
4509 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004510 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 else
4512 ++p;
4513 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004514 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516 return p;
4517}
4518
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004519 int
4520get_bad_opt(char_u *p, exarg_T *eap)
4521{
4522 if (STRICMP(p, "keep") == 0)
4523 eap->bad_char = BAD_KEEP;
4524 else if (STRICMP(p, "drop") == 0)
4525 eap->bad_char = BAD_DROP;
4526 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4527 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004528 else
4529 return FAIL;
4530 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004531}
4532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533/*
4534 * Get "++opt=arg" argument.
4535 * Return FAIL or OK.
4536 */
4537 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004538getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539{
4540 char_u *arg = eap->arg + 2;
4541 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004542 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004545 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4547 {
4548 if (*arg == 'n')
4549 {
4550 arg += 2;
4551 eap->force_bin = FORCE_NOBIN;
4552 }
4553 else
4554 eap->force_bin = FORCE_BIN;
4555 if (!checkforcmd(&arg, "binary", 3))
4556 return FAIL;
4557 eap->arg = skipwhite(arg);
4558 return OK;
4559 }
4560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004561 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004562 if (STRNCMP(arg, "edit", 4) == 0)
4563 {
4564 eap->read_edit = TRUE;
4565 eap->arg = skipwhite(arg + 4);
4566 return OK;
4567 }
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 if (STRNCMP(arg, "ff", 2) == 0)
4570 {
4571 arg += 2;
4572 pp = &eap->force_ff;
4573 }
4574 else if (STRNCMP(arg, "fileformat", 10) == 0)
4575 {
4576 arg += 10;
4577 pp = &eap->force_ff;
4578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 else if (STRNCMP(arg, "enc", 3) == 0)
4580 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004581 if (STRNCMP(arg, "encoding", 8) == 0)
4582 arg += 8;
4583 else
4584 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 pp = &eap->force_enc;
4586 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004587 else if (STRNCMP(arg, "bad", 3) == 0)
4588 {
4589 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004590 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 if (pp == NULL || *arg != '=')
4594 return FAIL;
4595
4596 ++arg;
4597 *pp = (int)(arg - eap->cmd);
4598 arg = skip_cmd_arg(arg, FALSE);
4599 eap->arg = skipwhite(arg);
4600 *arg = NUL;
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (pp == &eap->force_ff)
4603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4605 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004606 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004608 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004610 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4612 *p = TOLOWER_ASC(*p);
4613 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004614 else
4615 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004616 // Check ++bad= argument. Must be a single-byte character, "keep" or
4617 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004618 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004619 return FAIL;
4620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
4622 return OK;
4623}
4624
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004626ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627{
4628 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004629 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 * directory for security reasons.
4631 */
4632 if (secure)
4633 {
4634 secure = 2;
4635 eap->errmsg = e_curdir;
4636 }
4637 else if (eap->cmdidx == CMD_autocmd)
4638 do_autocmd(eap->arg, eap->forceit);
4639 else
4640 do_augroup(eap->arg, eap->forceit);
4641}
4642
4643/*
4644 * ":doautocmd": Apply the automatic commands to the current buffer.
4645 */
4646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004647ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004649 char_u *arg = eap->arg;
4650 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004651 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004652
Bram Moolenaar1610d052016-06-09 22:53:01 +02004653 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004654 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004655 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004656 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659/*
4660 * :[N]bunload[!] [N] [bufname] unload buffer
4661 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4662 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4663 */
4664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004665ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004667 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004668 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 eap->errmsg = do_bufdel(
4670 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4671 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4672 : DOBUF_UNLOAD, eap->arg,
4673 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4674}
4675
4676/*
4677 * :[N]buffer [N] to buffer N
4678 * :[N]sbuffer [N] to buffer N
4679 */
4680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004681ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004683 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004684 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (*eap->arg)
4686 eap->errmsg = e_trailing;
4687 else
4688 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004689 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4691 else
4692 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004693 if (eap->do_ecmd_cmd != NULL)
4694 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696}
4697
4698/*
4699 * :[N]bmodified [N] to next mod. buffer
4700 * :[N]sbmodified [N] to next mod. buffer
4701 */
4702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004703ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004706 if (eap->do_ecmd_cmd != NULL)
4707 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708}
4709
4710/*
4711 * :[N]bnext [N] to next buffer
4712 * :[N]sbnext [N] split and to next buffer
4713 */
4714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004715ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004717 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004718 return;
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004721 if (eap->do_ecmd_cmd != NULL)
4722 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723}
4724
4725/*
4726 * :[N]bNext [N] to previous buffer
4727 * :[N]bprevious [N] to previous buffer
4728 * :[N]sbNext [N] split and to previous buffer
4729 * :[N]sbprevious [N] split and to previous buffer
4730 */
4731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004734 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004735 return;
4736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004738 if (eap->do_ecmd_cmd != NULL)
4739 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740}
4741
4742/*
4743 * :brewind to first buffer
4744 * :bfirst to first buffer
4745 * :sbrewind split and to first buffer
4746 * :sbfirst split and to first buffer
4747 */
4748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004749ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004751 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004752 return;
4753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004755 if (eap->do_ecmd_cmd != NULL)
4756 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757}
4758
4759/*
4760 * :blast to last buffer
4761 * :sblast split and to last buffer
4762 */
4763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004764ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004766 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004767 return;
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004770 if (eap->do_ecmd_cmd != NULL)
4771 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
Bram Moolenaar7a092242020-04-16 22:10:49 +02004774/*
4775 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004776 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004779ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004781#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004782 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004783 return in_vim9script();
4784#endif
4785 return (c == NUL || c == '|' || c == '"' || c == '\n');
4786}
4787
4788/*
4789 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4790 * to "cmd_start" or has a white space character before it.
4791 */
4792 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004793ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004794{
4795 int c = *cmd;
4796
4797#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004798 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004799 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 return (c == NUL || c == '|' || c == '"' || c == '\n');
4802}
4803
4804#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4805 || defined(PROTO)
4806/*
4807 * Return the next command, after the first '|' or '\n'.
4808 * Return NULL if not found.
4809 */
4810 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004811find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
4813 while (*p != '|' && *p != '\n')
4814 {
4815 if (*p == NUL)
4816 return NULL;
4817 ++p;
4818 }
4819 return (p + 1);
4820}
4821#endif
4822
4823/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004824 * Check if *p is a separator between Ex commands, skipping over white space.
4825 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 */
4827 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004828check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004830 char_u *s = skipwhite(p);
4831
4832 if (*s == '|' || *s == '\n')
4833 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 else
4835 return NULL;
4836}
4837
4838/*
4839 * - if there are more files to edit
4840 * - and this is the last window
4841 * - and forceit not used
4842 * - and not repeated twice on a row
4843 * return FAIL and give error message if 'message' TRUE
4844 * return OK otherwise
4845 */
4846 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004847check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004848 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004849 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850{
4851 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4852
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004853 if (!forceit && only_one_window()
4854 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
4856 if (message)
4857 {
4858#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4859 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4860 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004861 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004863 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4864 NGETTEXT("%d more file to edit. Quit anyway?",
4865 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4867 return OK;
4868 return FAIL;
4869 }
4870#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004871 semsg(NGETTEXT("E173: %d more file to edit",
4872 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004873 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 return FAIL;
4876 }
4877 return OK;
4878}
4879
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880/*
4881 * Function given to ExpandGeneric() to obtain the list of command names.
4882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004884get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
4886 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return cmdnames[idx].cmd_name;
4889}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004892ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
Bram Moolenaare6850792010-05-14 15:28:44 +02004894 if (*eap->arg == NUL)
4895 {
4896#ifdef FEAT_EVAL
4897 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4898 char_u *p = NULL;
4899
4900 if (expr != NULL)
4901 {
4902 ++emsg_off;
4903 p = eval_to_string(expr, NULL, FALSE);
4904 --emsg_off;
4905 vim_free(expr);
4906 }
4907 if (p != NULL)
4908 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004909 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004910 vim_free(p);
4911 }
4912 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004913 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004914#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004915 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004916#endif
4917 }
4918 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004919 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004920
4921#ifdef FEAT_VTP
4922 else if (has_vtp_working())
4923 {
4924 // background color change requires clear + redraw
4925 update_screen(CLEAR);
4926 redrawcmd();
4927 }
4928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929}
4930
4931 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004932ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
4934 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004935 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 do_highlight(eap->arg, eap->forceit, FALSE);
4937}
4938
4939
4940/*
4941 * Call this function if we thought we were going to exit, but we won't
4942 * (because of an error). May need to restore the terminal mode.
4943 */
4944 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004945not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 exiting = FALSE;
4948 settmode(TMODE_RAW);
4949}
4950
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004951 static int
4952before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4953{
4954 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4955
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004956 // Bail out when autocommands closed the window.
4957 // Refuse to quit when the buffer in the last window is being closed (can
4958 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004959 if (!win_valid(wp)
4960 || curbuf_locked()
4961 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4962 return TRUE;
4963
4964 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4965 {
4966 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004967 // Refuse to quit when locked or when the window was closed or the
4968 // buffer in the last window is being closed (can only happen in
4969 // autocommands).
4970 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004971 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4972 return TRUE;
4973 }
4974
4975 return FALSE;
4976}
4977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004979 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004980 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004981 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004983 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004984ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004986 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004987
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988#ifdef FEAT_CMDWIN
4989 if (cmdwin_type != 0)
4990 {
4991 cmdwin_result = Ctrl_C;
4992 return;
4993 }
4994#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004995 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004996 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004997 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004998 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004999 return;
5000 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005001 if (eap->addr_count > 0)
5002 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005003 int wnr = eap->line2;
5004
5005 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5006 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005007 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005008 }
5009 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005010 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005012 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005013 if (curbuf_locked())
5014 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005016 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005017 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005018 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
5020#ifdef FEAT_NETBEANS_INTG
5021 netbeansForcedQuit = eap->forceit;
5022#endif
5023
5024 /*
5025 * If there are more files or windows we won't exit.
5026 */
5027 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5028 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005029 if ((!buf_hide(wp->w_buffer)
5030 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005031 | (eap->forceit ? CCGD_FORCEIT : 0)
5032 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005034 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
5036 not_exiting();
5037 }
5038 else
5039 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 // quit last window
5041 // Note: only_one_window() returns true, even so a help window is
5042 // still open. In that case only quit, if no address has been
5043 // specified. Example:
5044 // :h|wincmd w|1q - don't quit
5045 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005046 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005048 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005049#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005051#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005052 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005053 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055}
5056
5057/*
5058 * ":cquit".
5059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005061ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005063 // this does not always pass on the exit code to the Manx compiler. why?
5064 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065}
5066
5067/*
5068 * ":qall": try to quit all windows
5069 */
5070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005071ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072{
5073# ifdef FEAT_CMDWIN
5074 if (cmdwin_type != 0)
5075 {
5076 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005077 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 else
5079 cmdwin_result = K_XF2;
5080 return;
5081 }
5082# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005084 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005085 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005086 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005087 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005088 return;
5089 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005090
5091 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005092 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005095 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 getout(0);
5097 not_exiting();
5098}
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100/*
5101 * ":close": close current window, unless it is the last one
5102 */
5103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005104ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005106 win_T *win;
5107 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005108#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005110 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005112#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005113 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005114 {
5115 if (eap->addr_count == 0)
5116 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005117 else
5118 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005119 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005120 {
5121 winnr++;
5122 if (winnr == eap->line2)
5123 break;
5124 }
5125 if (win == NULL)
5126 win = lastwin;
5127 ex_win_close(eap->forceit, win, NULL);
5128 }
5129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130}
5131
Bram Moolenaar4033c552017-09-16 20:54:51 +02005132#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005133/*
5134 * ":pclose": Close any preview window.
5135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005137ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005138{
5139 win_T *win;
5140
Bram Moolenaar79648732019-07-18 21:43:07 +02005141 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005142 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005143 if (win->w_p_pvw)
5144 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005145 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005146 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005147 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005148# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005149 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005150 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005151# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005152}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005153#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005154
Bram Moolenaarf740b292006-02-16 22:11:02 +00005155/*
5156 * Close window "win" and take care of handling closing the last window for a
5157 * modified buffer.
5158 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005160ex_win_close(
5161 int forceit,
5162 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005163 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164{
5165 int need_hide;
5166 buf_T *buf = win->w_buffer;
5167
5168 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005169 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005171#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if ((p_confirm || cmdmod.confirm) && p_write)
5173 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005174 bufref_T bufref;
5175
5176 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005178 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 return;
5180 need_hide = FALSE;
5181 }
5182 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005185 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 return;
5187 }
5188 }
5189
Bram Moolenaar4033c552017-09-16 20:54:51 +02005190#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005192#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005193
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005194 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005195 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005196 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005197 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005198 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199}
5200
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005202 * Handle the argument for a tabpage related ex command.
5203 * Returns a tabpage number.
5204 * When an error is encountered then eap->errmsg is set.
5205 */
5206 static int
5207get_tabpage_arg(exarg_T *eap)
5208{
5209 int tab_number;
5210 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5211
5212 if (eap->arg && *eap->arg != NUL)
5213 {
5214 char_u *p = eap->arg;
5215 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005216 int relative = 0; // argument +N/-N means: go to N places to the
5217 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005218
5219 if (*p == '-')
5220 {
5221 relative = -1;
5222 p++;
5223 }
5224 else if (*p == '+')
5225 {
5226 relative = 1;
5227 p++;
5228 }
5229
5230 p_save = p;
5231 tab_number = getdigits(&p);
5232
5233 if (relative == 0)
5234 {
5235 if (STRCMP(p, "$") == 0)
5236 tab_number = LAST_TAB_NR;
5237 else if (p == p_save || *p_save == '-' || *p != NUL
5238 || tab_number > LAST_TAB_NR)
5239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005240 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005241 eap->errmsg = e_invarg;
5242 goto theend;
5243 }
5244 }
5245 else
5246 {
5247 if (*p_save == NUL)
5248 tab_number = 1;
5249 else if (p == p_save || *p_save == '-' || *p != NUL
5250 || tab_number == 0)
5251 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005252 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005253 eap->errmsg = e_invarg;
5254 goto theend;
5255 }
5256 tab_number = tab_number * relative + tabpage_index(curtab);
5257 if (!unaccept_arg0 && relative == -1)
5258 --tab_number;
5259 }
5260 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5261 eap->errmsg = e_invarg;
5262 }
5263 else if (eap->addr_count > 0)
5264 {
5265 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005266 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005267 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005268 tab_number = 0;
5269 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005270 else
5271 {
5272 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005273 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005274 {
5275 --tab_number;
5276 if (tab_number < unaccept_arg0)
5277 eap->errmsg = e_invarg;
5278 }
5279 }
5280 }
5281 else
5282 {
5283 switch (eap->cmdidx)
5284 {
5285 case CMD_tabnext:
5286 tab_number = tabpage_index(curtab) + 1;
5287 if (tab_number > LAST_TAB_NR)
5288 tab_number = 1;
5289 break;
5290 case CMD_tabmove:
5291 tab_number = LAST_TAB_NR;
5292 break;
5293 default:
5294 tab_number = tabpage_index(curtab);
5295 }
5296 }
5297
5298theend:
5299 return tab_number;
5300}
5301
5302/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005303 * ":tabclose": close current tab page, unless it is the last one.
5304 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 */
5306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005307ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005309 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005310 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005311
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005312# ifdef FEAT_CMDWIN
5313 if (cmdwin_type != 0)
5314 cmdwin_result = K_IGNORE;
5315 else
5316# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005317 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005318 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005321 tab_number = get_tabpage_arg(eap);
5322 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005323 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005324 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005325 if (tp == NULL)
5326 {
5327 beep_flush();
5328 return;
5329 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005330 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005331 {
5332 tabpage_close_other(tp, eap->forceit);
5333 return;
5334 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005335 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005336 tabpage_close(eap->forceit);
5337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005339}
5340
5341/*
5342 * ":tabonly": close all tab pages except the current one
5343 */
5344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005346{
5347 tabpage_T *tp;
5348 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005349 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005350
5351# ifdef FEAT_CMDWIN
5352 if (cmdwin_type != 0)
5353 cmdwin_result = K_IGNORE;
5354 else
5355# endif
5356 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005357 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005358 else
5359 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005360 tab_number = get_tabpage_arg(eap);
5361 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005362 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005363 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005364 // Repeat this up to a 1000 times, because autocommands may
5365 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005366 for (done = 0; done < 1000; ++done)
5367 {
5368 FOR_ALL_TABPAGES(tp)
5369 if (tp->tp_topframe != topframe)
5370 {
5371 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005372 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005373 if (valid_tabpage(tp))
5374 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005375 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005376 break;
5377 }
5378 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005379 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005380 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005381 }
5382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005386 * Close the current tab page.
5387 */
5388 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005389tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005391 // First close all the windows but the current one. If that worked then
5392 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005393 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005394 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005395 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005396 ex_win_close(forceit, curwin, NULL);
5397# ifdef FEAT_GUI
5398 need_mouse_correct = TRUE;
5399# endif
5400}
5401
5402/*
5403 * Close tab page "tp", which is not the current tab page.
5404 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005405 * Also takes care of the tab pages line disappearing when closing the
5406 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005407 */
5408 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005409tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005410{
5411 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005412 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005413 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005415 // Limit to 1000 windows, autocommands may add a window while we close
5416 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005417 while (++done < 1000)
5418 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005419 wp = tp->tp_firstwin;
5420 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005421
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005422 // Autocommands may delete the tab page under our fingers and we may
5423 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005424 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005425 break;
5426 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005427
Bram Moolenaar29323592016-07-24 22:04:11 +02005428 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005429
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005430 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005431 if (h != tabline_height())
5432 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005433}
5434
5435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 * ":only".
5437 */
5438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005439ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005441 win_T *wp;
5442 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443# ifdef FEAT_GUI
5444 need_mouse_correct = TRUE;
5445# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005446 if (eap->addr_count > 0)
5447 {
5448 wnr = eap->line2;
5449 for (wp = firstwin; --wnr > 0; )
5450 {
5451 if (wp->w_next == NULL)
5452 break;
5453 else
5454 wp = wp->w_next;
5455 }
5456 win_goto(wp);
5457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 close_others(TRUE, eap->forceit);
5459}
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005462ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005464 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005465 if (!eap->skip)
5466 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005467#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005468 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005469#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005470 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005471 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005472 else
5473 {
5474 int winnr = 0;
5475 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005476
Bram Moolenaar2256c992016-11-15 21:17:07 +01005477 FOR_ALL_WINDOWS(win)
5478 {
5479 winnr++;
5480 if (winnr == eap->line2)
5481 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005482 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005483 if (win == NULL)
5484 win = lastwin;
5485 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488}
5489
5490/*
5491 * ":stop" and ":suspend": Suspend Vim.
5492 */
5493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005494ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
5496 /*
5497 * Disallow suspending for "rvim".
5498 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005499 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {
5501 if (!eap->forceit)
5502 autowrite_all();
5503 windgoto((int)Rows - 1, 0);
5504 out_char('\n');
5505 out_flush();
5506 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005507 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005509 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005511 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512#ifdef FEAT_TITLE
5513 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005514 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515#endif
5516 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005517 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005519 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 }
5521}
5522
5523/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005524 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 */
5526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005527ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529#ifdef FEAT_CMDWIN
5530 if (cmdwin_type != 0)
5531 {
5532 cmdwin_result = Ctrl_C;
5533 return;
5534 }
5535#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005536 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005537 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005538 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005539 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005540 return;
5541 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005542
5543 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005544 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
5546 /*
5547 * if more files or windows we won't exit
5548 */
5549 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5550 exiting = TRUE;
5551 if ( ((eap->cmdidx == CMD_wq
5552 || curbufIsChanged())
5553 && do_write(eap) == FAIL)
5554 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005555 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 {
5557 not_exiting();
5558 }
5559 else
5560 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005561 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005563 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564# ifdef FEAT_GUI
5565 need_mouse_correct = TRUE;
5566# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005567 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005568 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570}
5571
5572/*
5573 * ":print", ":list", ":number".
5574 */
5575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005578 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005579 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005580 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005582 for ( ;!got_int; ui_breakcheck())
5583 {
5584 print_line(eap->line1,
5585 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5586 || (eap->flags & EXFLAG_NR)),
5587 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5588 if (++eap->line1 > eap->line2)
5589 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005590 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005591 }
5592 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005593 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005594 curwin->w_cursor.lnum = eap->line2;
5595 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599}
5600
5601#ifdef FEAT_BYTEOFF
5602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005603ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605 goto_byte(eap->line2);
5606}
5607#endif
5608
5609/*
5610 * ":shell".
5611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 do_shell(NULL, 0);
5616}
5617
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005618#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005620static int drop_busy = FALSE;
5621static int drop_filec;
5622static char_u **drop_filev = NULL;
5623static int drop_split;
5624static void (*drop_callback)(void *);
5625static void *drop_cookie;
5626
5627 static void
5628handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 exarg_T ea;
5631 int save_msg_scroll = msg_scroll;
5632
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005633 // Setting the argument list may cause screen updates and being called
5634 // recursively. Avoid that by setting drop_busy.
5635 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005637 // Check whether the current buffer is changed. If so, we will need
5638 // to split the current window or data could be lost.
5639 // We don't need to check if the 'hidden' option is set, as in this
5640 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005641 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005644 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 --emsg_off;
5646 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005647 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 if (win_split(0, 0) == FAIL)
5650 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005651 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005653 // When splitting the window, create a new alist. Otherwise the
5654 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 alist_unlink(curwin->w_alist);
5656 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658
5659 /*
5660 * Set up the new argument list.
5661 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005662 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
5664 /*
5665 * Move to the first file.
5666 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005667 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005668 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 ea.cmd = (char_u *)"next";
5670 do_argfile(&ea, 0);
5671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005672 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5673 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 need_start_insertmode = FALSE;
5675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005676 // Restore msg_scroll, otherwise a following command may cause scrolling
5677 // unexpectedly. The screen will be redrawn by the caller, thus
5678 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005680
5681 if (drop_callback != NULL)
5682 drop_callback(drop_cookie);
5683
5684 drop_filev = NULL;
5685 drop_busy = FALSE;
5686}
5687
5688/*
5689 * Handle a file drop. The code is here because a drop is *nearly* like an
5690 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005691 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005692 * code is very similar to :args and hence needs access to a lot of the static
5693 * functions in this file.
5694 *
5695 * The "filev" list must have been allocated using alloc(), as should each item
5696 * in the list. This function takes over responsibility for freeing the "filev"
5697 * list.
5698 */
5699 void
5700handle_drop(
5701 int filec, // the number of files dropped
5702 char_u **filev, // the list of files dropped
5703 int split, // force splitting the window
5704 void (*callback)(void *), // to be called after setting the argument
5705 // list
5706 void *cookie) // argument for "callback" (allocated)
5707{
5708 // Cannot handle recursive drops, finish the pending one.
5709 if (drop_busy)
5710 {
5711 FreeWild(filec, filev);
5712 vim_free(cookie);
5713 return;
5714 }
5715
5716 // When calling handle_drop() more than once in a row we only use the last
5717 // one.
5718 if (drop_filev != NULL)
5719 {
5720 FreeWild(drop_filec, drop_filev);
5721 vim_free(drop_cookie);
5722 }
5723
5724 drop_filec = filec;
5725 drop_filev = filev;
5726 drop_split = split;
5727 drop_callback = callback;
5728 drop_cookie = cookie;
5729
5730 // Postpone this when:
5731 // - editing the command line
5732 // - not possible to change the current buffer
5733 // - updating the screen
5734 // As it may change buffers and window structures that are in use and cause
5735 // freed memory to be used.
5736 if (text_locked() || curbuf_locked() || updating_screen)
5737 return;
5738
5739 handle_drop_internal();
5740}
5741
5742/*
5743 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5744 * reset: Handle a postponed drop.
5745 */
5746 void
5747handle_any_postponed_drop(void)
5748{
5749 if (!drop_busy && drop_filev != NULL
5750 && !text_locked() && !curbuf_locked() && !updating_screen)
5751 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752}
5753#endif
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 * ":preserve".
5757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005759ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005761 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 ml_preserve(curbuf, TRUE);
5763}
5764
5765/*
5766 * ":recover".
5767 */
5768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005769ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005771 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005773 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5774 | CCGD_MULTWIN
5775 | (eap->forceit ? CCGD_FORCEIT : 0)
5776 | CCGD_EXCMD)
5777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 && (*eap->arg == NUL
5779 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005780 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 recoverymode = FALSE;
5782}
5783
5784/*
5785 * Command modifier used in a wrong way.
5786 */
5787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005788ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 eap->errmsg = e_invcmd;
5791}
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793/*
5794 * :sview [+command] file split window with new file, read-only
5795 * :split [[+command] file] split window with current or new file
5796 * :vsplit [[+command] file] split window vertically with current or new file
5797 * :new [[+command] file] split window with no or new file
5798 * :vnew [[+command] file] split vertically window with no or new file
5799 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005800 *
5801 * :tabedit open new Tab page with empty window
5802 * :tabedit [+command] file open new Tab page and edit "file"
5803 * :tabnew [[+command] file] just like :tabedit
5804 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 */
5806 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005807ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005809 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005810#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005812#endif
5813#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005815#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005816 int use_tab = eap->cmdidx == CMD_tabedit
5817 || eap->cmdidx == CMD_tabfind
5818 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005820 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005821 return;
5822
Bram Moolenaar4033c552017-09-16 20:54:51 +02005823#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
Bram Moolenaar4033c552017-09-16 20:54:51 +02005827#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005828 // A ":split" in the quickfix window works like ":new". Don't want two
5829 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005830 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
5832 if (eap->cmdidx == CMD_split)
5833 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (eap->cmdidx == CMD_vsplit)
5835 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaar4033c552017-09-16 20:54:51 +02005839#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005840 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
5842 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5843 FNAME_MESS, TRUE, curbuf->b_ffname);
5844 if (fname == NULL)
5845 goto theend;
5846 eap->arg = fname;
5847 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005848# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005849 else
5850# endif
5851#endif
5852#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 && eap->cmdidx != CMD_new)
5856 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005857 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005858# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005859 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005860# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005861 au_has_group((char_u *)"FileExplorer"))
5862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005863 // No browsing supported but we do have the file explorer:
5864 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005865 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5866 eap->arg = (char_u *)".";
5867 }
5868 else
5869 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005870 fname = do_browse(0, (char_u *)(use_tab
5871 ? _("Edit File in new tab page")
5872 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005874 if (fname == NULL)
5875 goto theend;
5876 eap->arg = fname;
5877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005879 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005882 /*
5883 * Either open new tab page or split the window.
5884 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005885 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005886 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005887 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5888 : eap->addr_count == 0 ? 0
5889 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005890 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005891 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005893 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005894 if (curwin != old_curwin
5895 && win_valid(old_curwin)
5896 && old_curwin->w_buffer != curbuf
5897 && !cmdmod.keepalt)
5898 old_curwin->w_alt_fnum = curbuf->b_fnum;
5899 }
5900 }
5901 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005904 // Reset 'scrollbind' when editing another file, but keep it when
5905 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005907# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005911 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 else
5913 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 do_exedit(eap, old_curwin);
5915 }
5916
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005917# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005919# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005921# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922theend:
5923 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005926
5927/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005928 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005929 */
5930 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005931tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005932{
5933 exarg_T ea;
5934
Bram Moolenaara80faa82020-04-12 19:37:17 +02005935 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005936 ea.cmdidx = CMD_tabnew;
5937 ea.cmd = (char_u *)"tabn";
5938 ea.arg = (char_u *)"";
5939 ex_splitview(&ea);
5940}
5941
5942/*
5943 * :tabnext command
5944 */
5945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005946ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005947{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005948 int tab_number;
5949
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005950 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005951 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005952 switch (eap->cmdidx)
5953 {
5954 case CMD_tabfirst:
5955 case CMD_tabrewind:
5956 goto_tabpage(1);
5957 break;
5958 case CMD_tablast:
5959 goto_tabpage(9999);
5960 break;
5961 case CMD_tabprevious:
5962 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005963 if (eap->arg && *eap->arg != NUL)
5964 {
5965 char_u *p = eap->arg;
5966 char_u *p_save = p;
5967
5968 tab_number = getdigits(&p);
5969 if (p == p_save || *p_save == '-' || *p != NUL
5970 || tab_number == 0)
5971 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005972 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005973 eap->errmsg = e_invarg;
5974 return;
5975 }
5976 }
5977 else
5978 {
5979 if (eap->addr_count == 0)
5980 tab_number = 1;
5981 else
5982 {
5983 tab_number = eap->line2;
5984 if (tab_number < 1)
5985 {
5986 eap->errmsg = e_invrange;
5987 return;
5988 }
5989 }
5990 }
5991 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005992 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005993 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005994 tab_number = get_tabpage_arg(eap);
5995 if (eap->errmsg == NULL)
5996 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005997 break;
5998 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005999}
6000
6001/*
6002 * :tabmove command
6003 */
6004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006005ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006006{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006007 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006008
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006009 tab_number = get_tabpage_arg(eap);
6010 if (eap->errmsg == NULL)
6011 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006012}
6013
6014/*
6015 * :tabs command: List tabs and their contents.
6016 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006018ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006019{
6020 tabpage_T *tp;
6021 win_T *wp;
6022 int tabcount = 1;
6023
6024 msg_start();
6025 msg_scroll = TRUE;
6026 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6027 {
6028 msg_putchar('\n');
6029 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006030 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006031 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006032 ui_breakcheck();
6033
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006034 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006035 wp = firstwin;
6036 else
6037 wp = tp->tp_firstwin;
6038 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6039 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006040 msg_putchar('\n');
6041 msg_putchar(wp == curwin ? '>' : ' ');
6042 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006043 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6044 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006045 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006046 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006047 else
6048 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6049 IObuff, IOSIZE, TRUE);
6050 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006051 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006052 ui_breakcheck();
6053 }
6054 }
6055}
6056
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057/*
6058 * ":mode": Set screen mode.
6059 * If no argument given, just get the screen size and redraw.
6060 */
6061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006062ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
6064 if (*eap->arg == NUL)
6065 shell_resized();
6066 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006067 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068}
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070/*
6071 * ":resize".
6072 * set, increment or decrement current window height
6073 */
6074 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006075ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 int n;
6078 win_T *wp = curwin;
6079
6080 if (eap->addr_count > 0)
6081 {
6082 n = eap->line2;
6083 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6084 ;
6085 }
6086
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006087# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006089# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (cmdmod.split & WSP_VERT)
6092 {
6093 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006094 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006095 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 n = 9999;
6097 win_setwidth_win((int)n, wp);
6098 }
6099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
6101 if (*eap->arg == '-' || *eap->arg == '+')
6102 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 n = 9999;
6105 win_setheight_win((int)n, wp);
6106 }
6107}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109/*
6110 * ":find [+command] <file>" command.
6111 */
6112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006113ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114{
6115#ifdef FEAT_SEARCHPATH
6116 char_u *fname;
6117 int count;
6118
6119 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6120 TRUE, curbuf->b_ffname);
6121 if (eap->addr_count > 0)
6122 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006123 // Repeat finding the file "count" times. This matters when it
6124 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 count = eap->line2;
6126 while (fname != NULL && --count > 0)
6127 {
6128 vim_free(fname);
6129 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6130 FALSE, curbuf->b_ffname);
6131 }
6132 }
6133
6134 if (fname != NULL)
6135 {
6136 eap->arg = fname;
6137#endif
6138 do_exedit(eap, NULL);
6139#ifdef FEAT_SEARCHPATH
6140 vim_free(fname);
6141 }
6142#endif
6143}
6144
6145/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006146 * ":open" simulation: for now just work like ":visual".
6147 */
6148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006149ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006150{
6151 regmatch_T regmatch;
6152 char_u *p;
6153
6154 curwin->w_cursor.lnum = eap->line2;
6155 beginline(BL_SOL | BL_FIX);
6156 if (*eap->arg == '/')
6157 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006159 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006160 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006161 *p = NUL;
6162 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6163 if (regmatch.regprog != NULL)
6164 {
6165 regmatch.rm_ic = p_ic;
6166 p = ml_get_curline();
6167 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006168 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006169 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006170 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006171 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006172 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006173 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006174 eap->arg += STRLEN(eap->arg);
6175 }
6176 check_cursor();
6177
6178 eap->cmdidx = CMD_visual;
6179 do_exedit(eap, NULL);
6180}
6181
6182/*
6183 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 */
6185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006186ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
6188 do_exedit(eap, NULL);
6189}
6190
6191/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006192 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006195do_exedit(
6196 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006197 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006201 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006203 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6204 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006205 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 /*
6207 * ":vi" command ends Ex mode.
6208 */
6209 if (exmode_active && (eap->cmdidx == CMD_visual
6210 || eap->cmdidx == CMD_view))
6211 {
6212 exmode_active = FALSE;
6213 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006215 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006216 if (global_busy)
6217 {
6218 int rd = RedrawingDisabled;
6219 int nwr = no_wait_return;
6220 int ms = msg_scroll;
6221#ifdef FEAT_GUI
6222 int he = hold_gui_events;
6223#endif
6224
6225 if (eap->nextcmd != NULL)
6226 {
6227 stuffReadbuff(eap->nextcmd);
6228 eap->nextcmd = NULL;
6229 }
6230
6231 if (exmode_was != EXMODE_VIM)
6232 settmode(TMODE_RAW);
6233 RedrawingDisabled = 0;
6234 no_wait_return = 0;
6235 need_wait_return = FALSE;
6236 msg_scroll = 0;
6237#ifdef FEAT_GUI
6238 hold_gui_events = 0;
6239#endif
6240 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006241 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006242
6243 main_loop(FALSE, TRUE);
6244
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006245 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006246 RedrawingDisabled = rd;
6247 no_wait_return = nwr;
6248 msg_scroll = ms;
6249#ifdef FEAT_GUI
6250 hold_gui_events = he;
6251#endif
6252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 }
6256
6257 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006258 || eap->cmdidx == CMD_tabnew
6259 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006260 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006262 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 setpcmark();
6264 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006265 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6266 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006268 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 || *eap->arg != NUL
6270#ifdef FEAT_BROWSE
6271 || cmdmod.browse
6272#endif
6273 )
6274 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006275 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6276 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006277 if (*eap->arg != NUL && curbuf_locked())
6278 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006279
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 n = readonlymode;
6281 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6282 readonlymode = TRUE;
6283 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006284 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6285 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 setpcmark();
6287 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6288 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006289 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6291 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6292 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006293 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006295 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006296 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006298 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006300 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (old_curwin != NULL)
6302 {
6303 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006304 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006306#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006307 cleanup_T cs;
6308
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006309 // Reset the error/interrupt/exception state here so that
6310 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006311 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006312#endif
6313#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006315#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006316 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006317
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006318#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006319 // Restore the error/interrupt/exception state if not
6320 // discarded by a new aborting error, interrupt, or
6321 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006322 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
6325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 }
6327 else if (readonlymode && curbuf->b_nwindows == 1)
6328 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006329 // When editing an already visited buffer, 'readonly' won't be set
6330 // but the previous value is kept. With ":view" and ":sview" we
6331 // want the file to be readonly, except when another window is
6332 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 curbuf->b_p_ro = TRUE;
6334 }
6335 readonlymode = n;
6336 }
6337 else
6338 {
6339 if (eap->do_ecmd_cmd != NULL)
6340 do_cmdline_cmd(eap->do_ecmd_cmd);
6341#ifdef FEAT_TITLE
6342 n = curwin->w_arg_idx_invalid;
6343#endif
6344 check_arg_idx(curwin);
6345#ifdef FEAT_TITLE
6346 if (n != curwin->w_arg_idx_invalid)
6347 maketitle();
6348#endif
6349 }
6350
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 /*
6352 * if ":split file" worked, set alternate file name in old window to new
6353 * file
6354 */
6355 if (old_curwin != NULL
6356 && *eap->arg != NUL
6357 && curwin != old_curwin
6358 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006359 && old_curwin->w_buffer != curbuf
6360 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362
6363 ex_no_reprint = TRUE;
6364}
6365
6366#ifndef FEAT_GUI
6367/*
6368 * ":gui" and ":gvim" when there is no GUI.
6369 */
6370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006371ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 eap->errmsg = e_nogvim;
6374}
6375#endif
6376
Bram Moolenaar4f974752019-02-17 17:44:42 +01006377#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006379ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
6381 gui_make_tearoff(eap->arg);
6382}
6383#endif
6384
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006385#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6386 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006388ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006390# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6391 if (gui.in_use)
6392 gui_make_popup(eap->arg, eap->forceit);
6393# ifdef FEAT_TERM_POPUP_MENU
6394 else
6395# endif
6396# endif
6397# ifdef FEAT_TERM_POPUP_MENU
6398 pum_make_popup(eap->arg, eap->forceit);
6399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400}
6401#endif
6402
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006404ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
6406 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006407 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006409 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411
6412/*
6413 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6414 * offset.
6415 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006418ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006421 win_T *save_curwin = curwin;
6422 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 long topline;
6424 long y;
6425 linenr_T old_linenr = curwin->w_cursor.lnum;
6426
6427 setpcmark();
6428
6429 /*
6430 * determine max topline
6431 */
6432 if (curwin->w_p_scb)
6433 {
6434 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006435 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
6437 if (wp->w_p_scb && wp->w_buffer)
6438 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006439 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 if (topline > y)
6441 topline = y;
6442 }
6443 }
6444 if (topline < 1)
6445 topline = 1;
6446 }
6447 else
6448 {
6449 topline = 1;
6450 }
6451
6452
6453 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006454 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006456 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 {
6458 if (curwin->w_p_scb)
6459 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006460 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 y = topline - curwin->w_topline;
6462 if (y > 0)
6463 scrollup(y, TRUE);
6464 else
6465 scrolldown(-y, TRUE);
6466 curwin->w_scbind_pos = topline;
6467 redraw_later(VALID);
6468 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 }
6471 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006472 curwin = save_curwin;
6473 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (curwin->w_p_scb)
6475 {
6476 did_syncbind = TRUE;
6477 checkpcmark();
6478 if (old_linenr != curwin->w_cursor.lnum)
6479 {
6480 char_u ctrl_o[2];
6481
6482 ctrl_o[0] = Ctrl_O;
6483 ctrl_o[1] = 0;
6484 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6485 }
6486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487}
6488
6489
6490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006491ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006493 int i;
6494 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6495 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006497 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 do_bang(1, eap, FALSE, FALSE, TRUE);
6499 else
6500 {
6501 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6502 return;
6503
6504#ifdef FEAT_BROWSE
6505 if (cmdmod.browse)
6506 {
6507 char_u *browseFile;
6508
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006509 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 NULL, NULL, NULL, curbuf);
6511 if (browseFile != NULL)
6512 {
6513 i = readfile(browseFile, NULL,
6514 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6515 vim_free(browseFile);
6516 }
6517 else
6518 i = OK;
6519 }
6520 else
6521#endif
6522 if (*eap->arg == NUL)
6523 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006524 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 return;
6526 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6527 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6528 }
6529 else
6530 {
6531 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6532 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6533 i = readfile(eap->arg, NULL,
6534 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6535
6536 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006537 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006539#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (!aborting())
6541#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006542 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
6544 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006545 {
6546 if (empty && exmode_active)
6547 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006548 // Delete the empty line that remains. Historically ex does
6549 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006550 if (eap->line2 == 0)
6551 lnum = curbuf->b_ml.ml_line_count;
6552 else
6553 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006554 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006555 {
6556 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006557 if (curwin->w_cursor.lnum > 1
6558 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006559 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006560 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006561 }
6562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 }
6566}
6567
Bram Moolenaarea408852005-06-25 22:49:46 +00006568static char_u *prev_dir = NULL;
6569
6570#if defined(EXITFREE) || defined(PROTO)
6571 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006572free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006573{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006574 VIM_CLEAR(prev_dir);
6575 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006576}
6577#endif
6578
Bram Moolenaarf4258302013-06-02 18:20:17 +02006579/*
6580 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006581 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6582 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006583 */
6584 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006585post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006586{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006587 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006588 // Clear tab local directory for both :cd and :tcd
6589 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006590 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006591 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006592 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006593 // If still in global directory, need to remember current
6594 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006595 if (globaldir == NULL && prev_dir != NULL)
6596 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006597 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006598 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006599 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006600 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006601 curtab->tp_localdir = vim_strsave(NameBuff);
6602 else
6603 curwin->w_localdir = vim_strsave(NameBuff);
6604 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006605 }
6606 else
6607 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006608 // We are now in the global directory, no need to remember its
6609 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006610 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006611 }
6612
6613 shorten_fnames(TRUE);
6614}
6615
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006616/*
6617 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6618 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6619 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6620 * Otherwise changes the global directory.
6621 * Returns TRUE if the directory is successfully changed.
6622 */
6623 int
6624changedir_func(
6625 char_u *new_dir,
6626 int forceit,
6627 cdscope_T scope)
6628{
6629 char_u *tofree;
6630 int dir_differs;
6631 int retval = FALSE;
6632
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006633 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006634 return FALSE;
6635
6636 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6637 {
6638 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6639 return FALSE;
6640 }
6641
6642 // ":cd -": Change to previous directory
6643 if (STRCMP(new_dir, "-") == 0)
6644 {
6645 if (prev_dir == NULL)
6646 {
6647 emsg(_("E186: No previous directory"));
6648 return FALSE;
6649 }
6650 new_dir = prev_dir;
6651 }
6652
6653 // Save current directory for next ":cd -"
6654 tofree = prev_dir;
6655 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6656 prev_dir = vim_strsave(NameBuff);
6657 else
6658 prev_dir = NULL;
6659
6660#if defined(UNIX) || defined(VMS)
6661 // for UNIX ":cd" means: go to home directory
6662 if (*new_dir == NUL)
6663 {
6664 // use NameBuff for home directory name
6665# ifdef VMS
6666 char_u *p;
6667
6668 p = mch_getenv((char_u *)"SYS$LOGIN");
6669 if (p == NULL || *p == NUL) // empty is the same as not set
6670 NameBuff[0] = NUL;
6671 else
6672 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6673# else
6674 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6675# endif
6676 new_dir = NameBuff;
6677 }
6678#endif
6679 dir_differs = new_dir == NULL || prev_dir == NULL
6680 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6681 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6682 emsg(_(e_failed));
6683 else
6684 {
6685 char_u *acmd_fname;
6686
6687 post_chdir(scope);
6688
6689 if (dir_differs)
6690 {
6691 if (scope == CDSCOPE_WINDOW)
6692 acmd_fname = (char_u *)"window";
6693 else if (scope == CDSCOPE_TABPAGE)
6694 acmd_fname = (char_u *)"tabpage";
6695 else
6696 acmd_fname = (char_u *)"global";
6697 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6698 curbuf);
6699 }
6700 retval = TRUE;
6701 }
6702 vim_free(tofree);
6703
6704 return retval;
6705}
Bram Moolenaarea408852005-06-25 22:49:46 +00006706
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006708 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006710 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006711ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006713 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
6715 new_dir = eap->arg;
6716#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006717 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 if (*new_dir == NUL)
6719 ex_pwd(NULL);
6720 else
6721#endif
6722 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006723 cdscope_T scope = CDSCOPE_GLOBAL;
6724
6725 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6726 scope = CDSCOPE_WINDOW;
6727 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6728 scope = CDSCOPE_TABPAGE;
6729
6730 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006731 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006732 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006733 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 ex_pwd(eap);
6735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
6737}
6738
6739/*
6740 * ":pwd".
6741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006743ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6746 {
6747#ifdef BACKSLASH_IN_FILENAME
6748 slash_adjust(NameBuff);
6749#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006750 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 }
6752 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006753 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754}
6755
6756/*
6757 * ":=".
6758 */
6759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006760ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006762 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006763 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764}
6765
6766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006769 int n;
6770 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
6772 if (cursor_valid())
6773 {
6774 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6775 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006776 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006778
6779 len = eap->line2;
6780 switch (*eap->arg)
6781 {
6782 case 'm': break;
6783 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006784 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006785 }
6786 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787}
6788
6789/*
6790 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6791 */
6792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006793do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
Bram Moolenaar52953192019-08-16 10:27:13 +02006795 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006796 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006797# ifdef ELAPSED_FUNC
6798 elapsed_T start_tv;
6799
6800 // Remember at what time we started, so that we know how much longer we
6801 // should wait after waiting for a bit.
6802 ELAPSED_INIT(start_tv);
6803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
6805 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006806 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006807 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006809 wait_now = msec - done > 1000L ? 1000L : msec - done;
6810#ifdef FEAT_TIMERS
6811 {
6812 long due_time = check_due_timer();
6813
6814 if (due_time > 0 && due_time < wait_now)
6815 wait_now = due_time;
6816 }
6817#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006818#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006819 if (has_any_channel() && wait_now > 20L)
6820 wait_now = 20L;
6821#endif
6822#ifdef FEAT_SOUND
6823 if (has_any_sound_callback() && wait_now > 20L)
6824 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006825#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006826 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006827
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006828#ifdef FEAT_JOB_CHANNEL
6829 if (has_any_channel())
6830 ui_breakcheck_force(TRUE);
6831 else
6832#endif
6833 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006834#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006835 // Process the netbeans and clientserver messages that may have been
6836 // received in the call to ui_breakcheck() when the GUI is in use. This
6837 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006838 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006839#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006840
6841# ifdef ELAPSED_FUNC
6842 // actual time passed
6843 done = ELAPSED_FUNC(start_tv);
6844# else
6845 // guestimate time passed (will actually be more)
6846 done += wait_now;
6847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006849
6850 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6851 // input buffer, otherwise a following call to input() fails.
6852 if (got_int)
6853 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854}
6855
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856/*
6857 * ":winsize" command (obsolete).
6858 */
6859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006860ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 int w, h;
6863 char_u *arg = eap->arg;
6864 char_u *p;
6865
6866 w = getdigits(&arg);
6867 arg = skipwhite(arg);
6868 p = arg;
6869 h = getdigits(&arg);
6870 if (*p != NUL && *arg == NUL)
6871 set_shellsize(w, h, TRUE);
6872 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006873 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874}
6875
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006877ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
6879 int xchar = NUL;
6880 char_u *p;
6881
6882 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006884 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (eap->arg[1] == NUL)
6886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006887 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 return;
6889 }
6890 xchar = eap->arg[1];
6891 p = eap->arg + 2;
6892 }
6893 else
6894 p = eap->arg + 1;
6895
6896 eap->nextcmd = check_nextcmd(p);
6897 p = skipwhite(p);
6898 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006899 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006900 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006902 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006904 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6906 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006907 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 }
6909}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
Bram Moolenaar843ee412004-06-30 16:16:41 +00006911#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912/*
6913 * ":winpos".
6914 */
6915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006916ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917{
6918 int x, y;
6919 char_u *arg = eap->arg;
6920 char_u *p;
6921
6922 if (*arg == NUL)
6923 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006924# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006925# ifdef VIMDLL
6926 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6927 mch_get_winpos(&x, &y) != FAIL)
6928# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006930# else
6931 if (mch_get_winpos(&x, &y) != FAIL)
6932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
6934 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006935 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 }
6937 else
6938# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006939 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941 else
6942 {
6943 x = getdigits(&arg);
6944 arg = skipwhite(arg);
6945 p = arg;
6946 y = getdigits(&arg);
6947 if (*p == NUL || *arg != NUL)
6948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006949 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 return;
6951 }
6952# ifdef FEAT_GUI
6953 if (gui.in_use)
6954 gui_mch_set_winpos(x, y);
6955 else if (gui.starting)
6956 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006957 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 gui_win_x = x;
6959 gui_win_y = y;
6960 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006961# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 else
6963# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006964# endif
6965# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006966 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967# endif
6968# ifdef HAVE_TGETENT
6969 if (*T_CWP)
6970 term_set_winpos(x, y);
6971# endif
6972 }
6973}
6974#endif
6975
6976/*
6977 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6978 */
6979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
6982 oparg_T oa;
6983
6984 clear_oparg(&oa);
6985 oa.regname = eap->regname;
6986 oa.start.lnum = eap->line1;
6987 oa.end.lnum = eap->line2;
6988 oa.line_count = eap->line2 - eap->line1 + 1;
6989 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006991 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
6993 setpcmark();
6994 curwin->w_cursor.lnum = eap->line1;
6995 beginline(BL_SOL | BL_FIX);
6996 }
6997
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006998 if (VIsual_active)
6999 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007000
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 switch (eap->cmdidx)
7002 {
7003 case CMD_delete:
7004 oa.op_type = OP_DELETE;
7005 op_delete(&oa);
7006 break;
7007
7008 case CMD_yank:
7009 oa.op_type = OP_YANK;
7010 (void)op_yank(&oa, FALSE, TRUE);
7011 break;
7012
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007013 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007014 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007016 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7017#else
7018 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007020 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 oa.op_type = OP_RSHIFT;
7022 else
7023 oa.op_type = OP_LSHIFT;
7024 op_shift(&oa, FALSE, eap->amount);
7025 break;
7026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007028 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029}
7030
7031/*
7032 * ":put".
7033 */
7034 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007035ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007037 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 if (eap->line2 == 0)
7039 {
7040 eap->line2 = 1;
7041 eap->forceit = TRUE;
7042 }
7043 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007044 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7045 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046}
7047
7048/*
7049 * Handle ":copy" and ":move".
7050 */
7051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007052ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
7054 long n;
7055
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007056 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007057 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {
7059 eap->nextcmd = NULL;
7060 return;
7061 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007062 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
7064 /*
7065 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7066 */
7067 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7068 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007069 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 return;
7071 }
7072
7073 if (eap->cmdidx == CMD_move)
7074 {
7075 if (do_move(eap->line1, eap->line2, n) == FAIL)
7076 return;
7077 }
7078 else
7079 ex_copy(eap->line1, eap->line2, n);
7080 u_clearline();
7081 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007082 ex_may_print(eap);
7083}
7084
7085/*
7086 * Print the current line if flags were given to the Ex command.
7087 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007088 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007089ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007090{
7091 if (eap->flags != 0)
7092 {
7093 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7094 (eap->flags & EXFLAG_LIST));
7095 ex_no_reprint = TRUE;
7096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097}
7098
7099/*
7100 * ":smagic" and ":snomagic".
7101 */
7102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007103ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
7105 int magic_save = p_magic;
7106
7107 p_magic = (eap->cmdidx == CMD_smagic);
7108 do_sub(eap);
7109 p_magic = magic_save;
7110}
7111
7112/*
7113 * ":join".
7114 */
7115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007116ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
7118 curwin->w_cursor.lnum = eap->line1;
7119 if (eap->line1 == eap->line2)
7120 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007121 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 return;
7123 if (eap->line2 == curbuf->b_ml.ml_line_count)
7124 {
7125 beep_flush();
7126 return;
7127 }
7128 ++eap->line2;
7129 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007130 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007132 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133}
7134
7135/*
7136 * ":[addr]@r" or ":[addr]*r": execute register
7137 */
7138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007139ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
7141 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007142 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143
7144 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007145 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
7147#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007148 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149#endif
7150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007151 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 c = *eap->arg;
7153 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7154 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007155 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007156 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7157 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 else
7162 {
7163 int save_efr = exec_from_reg;
7164
7165 exec_from_reg = TRUE;
7166
7167 /*
7168 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007169 * Continue until the stuff buffer is empty and all added characters
7170 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007172 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7174
7175 exec_from_reg = save_efr;
7176 }
7177}
7178
7179/*
7180 * ":!".
7181 */
7182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007183ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7186}
7187
7188/*
7189 * ":undo".
7190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007192ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007194 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007195 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007196 else
7197 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198}
7199
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007200#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007202ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007203{
7204 char_u hash[UNDO_HASH_SIZE];
7205
7206 u_compute_hash(hash);
7207 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7208}
7209
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007211ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007212{
7213 char_u hash[UNDO_HASH_SIZE];
7214
7215 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007216 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007217}
7218#endif
7219
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220/*
7221 * ":redo".
7222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007224ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
7226 u_redo(1);
7227}
7228
7229/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007230 * ":earlier" and ":later".
7231 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007233ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007234{
7235 long count = 0;
7236 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007237 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007238 char_u *p = eap->arg;
7239
7240 if (*p == NUL)
7241 count = 1;
7242 else if (isdigit(*p))
7243 {
7244 count = getdigits(&p);
7245 switch (*p)
7246 {
7247 case 's': ++p; sec = TRUE; break;
7248 case 'm': ++p; sec = TRUE; count *= 60; break;
7249 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007250 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7251 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007252 }
7253 }
7254
7255 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007256 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007257 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007258 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7259 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007260}
7261
7262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 * ":redir": start/stop redirection.
7264 */
7265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007266ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267{
7268 char *mode;
7269 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007270 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaarba768492016-07-08 15:32:54 +02007272#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007273 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007275 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007276 return;
7277 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007278#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007279
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 if (STRICMP(eap->arg, "END") == 0)
7281 close_redir();
7282 else
7283 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007284 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007286 ++arg;
7287 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007289 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 mode = "a";
7291 }
7292 else
7293 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007294 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295
7296 close_redir();
7297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007298 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007299 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 if (fname == NULL)
7301 return;
7302#ifdef FEAT_BROWSE
7303 if (cmdmod.browse)
7304 {
7305 char_u *browseFile;
7306
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007307 browseFile = do_browse(BROWSE_SAVE,
7308 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007309 fname, NULL, NULL,
7310 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007312 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 vim_free(fname);
7314 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007315 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
7317#endif
7318
7319 redir_fd = open_exfile(fname, eap->forceit, mode);
7320 vim_free(fname);
7321 }
7322#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007323 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007325 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007327 ++arg;
7328 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007330 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007331 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007333 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007335 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007336 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007337 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007340 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007341 if (*arg == '>')
7342 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007343 // Make register empty when not using @A-@Z and the
7344 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007345 if (*arg == NUL && !isupper(redir_reg))
7346 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007348 }
7349 if (*arg != NUL)
7350 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007351 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007352 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007353 }
7354 }
7355 else if (*arg == '=' && arg[1] == '>')
7356 {
7357 int append;
7358
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007359 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007360 close_redir();
7361 arg += 2;
7362
7363 if (*arg == '>')
7364 {
7365 ++arg;
7366 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 }
7368 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007369 append = FALSE;
7370
7371 if (var_redir_start(skipwhite(arg), append) == OK)
7372 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 }
7374#endif
7375
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007376 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007379 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007382 // Make sure redirection is not off. Can happen for cmdline completion
7383 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007384 if (redir_fd != NULL
7385#ifdef FEAT_EVAL
7386 || redir_reg || redir_vname
7387#endif
7388 )
7389 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390}
7391
7392/*
7393 * ":redraw": force redraw
7394 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007395 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007396ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398 int r = RedrawingDisabled;
7399 int p = p_lz;
7400
7401 RedrawingDisabled = 0;
7402 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007403 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007405 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406#ifdef FEAT_TITLE
7407 if (need_maketitle)
7408 maketitle();
7409#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007410#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7411# ifdef VIMDLL
7412 if (!gui.in_use)
7413# endif
7414 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 RedrawingDisabled = r;
7417 p_lz = p;
7418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007419 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 msg_didout = FALSE;
7421 msg_col = 0;
7422
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007423 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007424 need_wait_return = FALSE;
7425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 out_flush();
7427}
7428
7429/*
7430 * ":redrawstatus": force redraw of status line(s)
7431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007433ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 int r = RedrawingDisabled;
7436 int p = p_lz;
7437
7438 RedrawingDisabled = 0;
7439 p_lz = FALSE;
7440 if (eap->forceit)
7441 status_redraw_all();
7442 else
7443 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007444 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 RedrawingDisabled = r;
7446 p_lz = p;
7447 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448}
7449
Bram Moolenaare12bab32019-01-08 22:02:56 +01007450/*
7451 * ":redrawtabline": force redraw of the tabline
7452 */
7453 static void
7454ex_redrawtabline(exarg_T *eap UNUSED)
7455{
7456 int r = RedrawingDisabled;
7457 int p = p_lz;
7458
7459 RedrawingDisabled = 0;
7460 p_lz = FALSE;
7461
7462 draw_tabline();
7463
7464 RedrawingDisabled = r;
7465 p_lz = p;
7466 out_flush();
7467}
7468
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007470close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471{
7472 if (redir_fd != NULL)
7473 {
7474 fclose(redir_fd);
7475 redir_fd = NULL;
7476 }
7477#ifdef FEAT_EVAL
7478 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007479 if (redir_vname)
7480 {
7481 var_redir_stop();
7482 redir_vname = 0;
7483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484#endif
7485}
7486
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007487#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007488 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007489vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007490{
7491 if (vim_mkdir(name, prot) != 0)
7492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007493 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007494 return FAIL;
7495 }
7496 return OK;
7497}
7498#endif
7499
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500/*
7501 * Open a file for writing for an Ex command, with some checks.
7502 * Return file descriptor, or NULL on failure.
7503 */
7504 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007505open_exfile(
7506 char_u *fname,
7507 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007508 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510 FILE *fd;
7511
7512#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007513 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 if (mch_isdir(fname))
7515 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007516 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 return NULL;
7518 }
7519#endif
7520 if (!forceit && *mode != 'a' && vim_fexists(fname))
7521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007522 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 return NULL;
7524 }
7525
7526 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007527 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
7529 return fd;
7530}
7531
7532/*
7533 * ":mark" and ":k".
7534 */
7535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007536ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 pos_T pos;
7539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007540 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007541 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007542 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 else
7545 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007546 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 curwin->w_cursor.lnum = eap->line2;
7548 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007549 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007550 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007551 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 }
7553}
7554
7555/*
7556 * Update w_topline, w_leftcol and the cursor position.
7557 */
7558 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007559update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007561 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 update_topline();
7563 if (!curwin->w_p_wrap)
7564 validate_cursor();
7565 update_curswant();
7566}
7567
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007569 * Save the current State and go to Normal mode.
7570 * Return TRUE if the typeahead could be saved.
7571 */
7572 int
7573save_current_state(save_state_T *sst)
7574{
7575 sst->save_msg_scroll = msg_scroll;
7576 sst->save_restart_edit = restart_edit;
7577 sst->save_msg_didout = msg_didout;
7578 sst->save_State = State;
7579 sst->save_insertmode = p_im;
7580 sst->save_finish_op = finish_op;
7581 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007582 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007584 msg_scroll = FALSE; // no msg scrolling in Normal mode
7585 restart_edit = 0; // don't go to Insert mode
7586 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007587
7588 /*
7589 * Save the current typeahead. This is required to allow using ":normal"
7590 * from an event handler and makes sure we don't hang when the argument
7591 * ends with half a command.
7592 */
7593 save_typeahead(&sst->tabuf);
7594 return sst->tabuf.typebuf_valid;
7595}
7596
7597 void
7598restore_current_state(save_state_T *sst)
7599{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007600 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007601 restore_typeahead(&sst->tabuf);
7602
7603 msg_scroll = sst->save_msg_scroll;
7604 restart_edit = sst->save_restart_edit;
7605 p_im = sst->save_insertmode;
7606 finish_op = sst->save_finish_op;
7607 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007608 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007609 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007610
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007611 // Restore the state (needed when called from a function executed for
7612 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007613 State = sst->save_State;
7614#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007615 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007616#endif
7617}
7618
7619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 * ":normal[!] {commands}": Execute normal mode commands.
7621 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007622 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007623ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007625 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 char_u *arg = NULL;
7627 int l;
7628 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007630 if (ex_normal_lock > 0)
7631 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007632 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007633 return;
7634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (ex_normal_busy >= p_mmd)
7636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007637 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 return;
7639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 /*
7642 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7643 * this for the K_SPECIAL leading byte, otherwise special keys will not
7644 * work.
7645 */
7646 if (has_mbyte)
7647 {
7648 int len = 0;
7649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007650 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 for (p = eap->arg; *p != NUL; ++p)
7652 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007653#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007654 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007656#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007657 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007658 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007659#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 )
7663 len += 2;
7664 }
7665 if (len > 0)
7666 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007667 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 if (arg != NULL)
7669 {
7670 len = 0;
7671 for (p = eap->arg; *p != NUL; ++p)
7672 {
7673 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007674#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 if (*p == CSI)
7676 {
7677 arg[len++] = KS_EXTRA;
7678 arg[len++] = (int)KE_CSI;
7679 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007680#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007681 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {
7683 arg[len++] = *++p;
7684 if (*p == K_SPECIAL)
7685 {
7686 arg[len++] = KS_SPECIAL;
7687 arg[len++] = KE_FILLER;
7688 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007689#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 else if (*p == CSI)
7691 {
7692 arg[len++] = KS_EXTRA;
7693 arg[len++] = (int)KE_CSI;
7694 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 }
7697 arg[len] = NUL;
7698 }
7699 }
7700 }
7701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007703 ++ex_normal_busy;
7704 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
7706 /*
7707 * Repeat the :normal command for each line in the range. When no
7708 * range given, execute it just once, without positioning the cursor
7709 * first.
7710 */
7711 do
7712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 if (eap->addr_count != 0)
7714 {
7715 curwin->w_cursor.lnum = eap->line1++;
7716 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007717 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
7719
Bram Moolenaar13505972019-01-24 15:04:48 +01007720 exec_normal_cmd(arg != NULL
7721 ? arg
7722 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7725 }
7726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007727 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 update_topline_cursor();
7729
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007730 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007732 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007733#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007734 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007735#endif
7736
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738}
7739
7740/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007741 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 */
7743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007744ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007746 if (eap->forceit)
7747 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007748 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007749 if (!curwin->w_cursor.lnum)
7750 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007751 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007752 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007753#ifdef FEAT_TERMINAL
7754 // Ignore this when running in an active terminal.
7755 if (term_job_running(curbuf->b_term))
7756 return;
7757#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007758
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007759 // Ignore the command when already in Insert mode. Inserting an
7760 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007761 if (State & INSERT)
7762 return;
7763
Bram Moolenaar64969662005-12-14 21:59:55 +00007764 if (eap->cmdidx == CMD_startinsert)
7765 restart_edit = 'a';
7766 else if (eap->cmdidx == CMD_startreplace)
7767 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007769 restart_edit = 'V';
7770
7771 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007773 if (eap->cmdidx == CMD_startinsert)
7774 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007775 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777}
7778
7779/*
7780 * ":stopinsert"
7781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007783ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 restart_edit = 0;
7786 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007787 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007790/*
7791 * Execute normal mode command "cmd".
7792 * "remap" can be REMAP_NONE or REMAP_YES.
7793 */
7794 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007795exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007796{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007797 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007798 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007799 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007800}
Bram Moolenaar25281632016-01-21 23:32:32 +01007801
Bram Moolenaar25281632016-01-21 23:32:32 +01007802/*
7803 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007804 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007805 */
7806 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007807exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007808{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007809 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007810 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007811
Bram Moolenaar905dd902019-04-07 14:21:47 +02007812 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7813 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007814 clear_oparg(&oa);
7815 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007816 while ((!stuff_empty()
7817 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007818 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007819 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007820 {
7821 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007822#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007823 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007824 && oa.op_type == OP_NOP && oa.regname == NUL
7825 && !VIsual_active)
7826 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007827 // If terminal_loop() returns OK we got a key that is handled
7828 // in Normal model. With FAIL we first need to position the
7829 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007830 if (terminal_loop(TRUE) == OK)
7831 normal_cmd(&oa, TRUE);
7832 }
7833 else
7834#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007835 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007836 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007837 }
7838}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007839
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840#ifdef FEAT_FIND_ID
7841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007842ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843{
7844 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7845 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7846 (linenr_T)1, (linenr_T)MAXLNUM);
7847}
7848
Bram Moolenaar4033c552017-09-16 20:54:51 +02007849#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850/*
7851 * ":psearch"
7852 */
7853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007854ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 g_do_tagpreview = p_pvh;
7857 ex_findpat(eap);
7858 g_do_tagpreview = 0;
7859}
7860#endif
7861
7862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007863ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 int whole = TRUE;
7866 long n;
7867 char_u *p;
7868 int action;
7869
7870 switch (cmdnames[eap->cmdidx].cmd_name[2])
7871 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007872 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7874 action = ACTION_GOTO;
7875 else
7876 action = ACTION_SHOW;
7877 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007878 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 action = ACTION_SHOW_ALL;
7880 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007881 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 action = ACTION_GOTO;
7883 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007884 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 action = ACTION_SPLIT;
7886 break;
7887 }
7888
7889 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007890 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {
7892 n = getdigits(&eap->arg);
7893 eap->arg = skipwhite(eap->arg);
7894 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007895 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {
7897 whole = FALSE;
7898 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007899 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 if (*p)
7901 {
7902 *p++ = NUL;
7903 p = skipwhite(p);
7904
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007905 // Check for trailing illegal characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 if (!ends_excmd(*p))
7907 eap->errmsg = e_trailing;
7908 else
7909 eap->nextcmd = check_nextcmd(p);
7910 }
7911 }
7912 if (!eap->skip)
7913 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7914 whole, !eap->forceit,
7915 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7916 n, action, eap->line1, eap->line2);
7917}
7918#endif
7919
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920
Bram Moolenaar4033c552017-09-16 20:54:51 +02007921#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922/*
7923 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7924 */
7925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007926ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007928 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7930}
7931
7932/*
7933 * ":pedit"
7934 */
7935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007936ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 win_T *curwin_save = curwin;
7939
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007940 if (ERROR_IF_ANY_POPUP_WINDOW)
7941 return;
7942
Bram Moolenaar026587b2019-08-17 15:08:00 +02007943 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007945 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007946
Bram Moolenaar026587b2019-08-17 15:08:00 +02007947 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if (curwin != curwin_save && win_valid(curwin_save))
7951 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007952 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 validate_cursor();
7954 redraw_later(VALID);
7955 win_enter(curwin_save, TRUE);
7956 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007957# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007958 else if (WIN_IS_POPUP(curwin))
7959 {
7960 // can't keep focus in popup window
7961 win_enter(firstwin, TRUE);
7962 }
7963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 g_do_tagpreview = 0;
7965}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967
7968/*
7969 * ":stag", ":stselect" and ":stjump".
7970 */
7971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007972ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
7974 postponed_split = -1;
7975 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007976 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7978 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007979 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981
7982/*
7983 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7984 */
7985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007986ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7989}
7990
7991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007992ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
7994 int cmd;
7995
7996 switch (name[1])
7997 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007998 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008000 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008002 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008004 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008006 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008008 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008010 case 'f': // ":tfirst"
8011 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008013 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008015 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008017 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008019 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 return;
8021 }
8022#endif
8023 cmd = DT_TAG;
8024 break;
8025 }
8026
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008027 if (name[0] == 'l')
8028 {
8029#ifndef FEAT_QUICKFIX
8030 ex_ni(eap);
8031 return;
8032#else
8033 cmd = DT_LTAG;
8034#endif
8035 }
8036
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8038 eap->forceit, TRUE);
8039}
8040
8041/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008042 * Check "str" for starting with a special cmdline variable.
8043 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8044 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8045 */
8046 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008047find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008048{
8049 int len;
8050 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008051 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008052 "%",
8053#define SPEC_PERC 0
8054 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008055#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008056 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008057#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008058 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008059#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008060 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008061#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008062 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008063#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008064 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008065#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008067#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008068 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008069#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008070 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008071#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008072 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008073#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008074 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008075#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008076#ifdef FEAT_CLIENTSERVER
8077 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008078# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008079#endif
8080 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008081
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008082 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008083 {
8084 len = (int)STRLEN(spec_str[i]);
8085 if (STRNCMP(src, spec_str[i], len) == 0)
8086 {
8087 *usedlen = len;
8088 return i;
8089 }
8090 }
8091 return -1;
8092}
8093
8094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 * Evaluate cmdline variables.
8096 *
8097 * change '%' to curbuf->b_ffname
8098 * '#' to curwin->w_altfile
8099 * '<cword>' to word under the cursor
8100 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008101 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 * '<cfile>' to path name under the cursor
8103 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008104 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 * '<afile>' to file name for autocommand
8106 * '<abuf>' to buffer number for autocommand
8107 * '<amatch>' to matching name for autocommand
8108 *
8109 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8110 * "" for error without a message) and NULL is returned.
8111 * Returns an allocated string if a valid match was found.
8112 * Returns NULL if no match was found. "usedlen" then still contains the
8113 * number of characters to skip.
8114 */
8115 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008116eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008117 char_u *src, // pointer into commandline
8118 char_u *srcstart, // beginning of valid memory for src
8119 int *usedlen, // characters after src that are used
8120 linenr_T *lnump, // line number for :e command, or NULL
8121 char **errormsg, // pointer to error message
8122 int *escaped) // return value has escaped white space (can
8123 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
8125 int i;
8126 char_u *s;
8127 char_u *result;
8128 char_u *resultbuf = NULL;
8129 int resultlen;
8130 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008131 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008133 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
8137 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008138 if (escaped != NULL)
8139 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
8141 /*
8142 * Check if there is something to do.
8143 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008144 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008145 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {
8147 *usedlen = 1;
8148 return NULL;
8149 }
8150
8151 /*
8152 * Skip when preceded with a backslash "\%" and "\#".
8153 * Note: In "\\%" the % is also not recognized!
8154 */
8155 if (src > srcstart && src[-1] == '\\')
8156 {
8157 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008158 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 return NULL;
8160 }
8161
8162 /*
8163 * word or WORD under cursor
8164 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008165 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8166 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008168 resultlen = find_ident_under_cursor(&result,
8169 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8170 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8171 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 if (resultlen == 0)
8173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008174 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 return NULL;
8176 }
8177 }
8178
8179 /*
8180 * '#': Alternate file name
8181 * '%': Current file name
8182 * File name under the cursor
8183 * File name for autocommand
8184 * and following modifiers
8185 */
8186 else
8187 {
8188 switch (spec_idx)
8189 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008190 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 if (curbuf->b_fname == NULL)
8192 {
8193 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008194 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 }
8196 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008199 tilde_file = STRCMP(result, "~") == 0;
8200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 break;
8202
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008203 case SPEC_HASH: // '#' or "#99": alternate file
8204 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {
8206 result = arg_all();
8207 resultbuf = result;
8208 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008209 if (escaped != NULL)
8210 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 break;
8213 }
8214 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008215 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008216 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008218 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008219 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008220 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008221 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008223 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008225 if (*usedlen < 2)
8226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008227 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008228 *usedlen = 1;
8229 return NULL;
8230 }
8231#ifdef FEAT_EVAL
8232 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8233 (long)i);
8234 if (result == NULL)
8235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008236 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008237 return NULL;
8238 }
8239#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008240 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 }
8244 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008245 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008246 if (i == 0 && src[1] == '<' && *usedlen > 1)
8247 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008248 buf = buflist_findnr(i);
8249 if (buf == NULL)
8250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008251 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008252 return NULL;
8253 }
8254 if (lnump != NULL)
8255 *lnump = ECMD_LAST;
8256 if (buf->b_fname == NULL)
8257 {
8258 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008259 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008260 }
8261 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008262 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008263 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008264 tilde_file = STRCMP(result, "~") == 0;
8265 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 break;
8268
8269#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008271 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 if (result == NULL)
8273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008274 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 return NULL;
8276 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008277 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 break;
8279#endif
8280
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008281 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008283 if (result != NULL && !autocmd_fname_full)
8284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008285 // Still need to turn the fname into a full path. It is
8286 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008287 autocmd_fname_full = TRUE;
8288 result = FullName_save(autocmd_fname, FALSE);
8289 vim_free(autocmd_fname);
8290 autocmd_fname = result;
8291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 if (result == NULL)
8293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008294 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 return NULL;
8296 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008297 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 break;
8299
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008300 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 if (autocmd_bufnr <= 0)
8302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008303 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 return NULL;
8305 }
8306 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8307 result = strbuf;
8308 break;
8309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008310 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 result = autocmd_match;
8312 if (result == NULL)
8313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008314 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 return NULL;
8316 }
8317 break;
8318
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008319 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008320 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (result == NULL)
8322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008323 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 return NULL;
8325 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008326 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008329 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008330 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008332 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008333 return NULL;
8334 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008335 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008336 result = strbuf;
8337 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008338
8339#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008340 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008341 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008343 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008344 return NULL;
8345 }
8346 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008347 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008348 result = strbuf;
8349 break;
8350#endif
8351
8352#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008354 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8355 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 result = strbuf;
8357 break;
8358#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008359
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008360 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008362 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 }
8364
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008365 resultlen = (int)STRLEN(result); // length of new string
8366 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {
8368 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 resultlen = (int)(s - result);
8371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 else if (!skip_mod)
8373 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008374 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 &resultlen);
8376 if (result == NULL)
8377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008378 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 return NULL;
8380 }
8381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383
8384 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8385 {
8386 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008387 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008388 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008390 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 result = NULL;
8392 }
8393 else
8394 result = vim_strnsave(result, resultlen);
8395 vim_free(resultbuf);
8396 return result;
8397}
8398
8399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 * Expand the <sfile> string in "arg".
8401 *
8402 * Returns an allocated string, or NULL for any error.
8403 */
8404 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008405expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008407 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 int len;
8409 char_u *result;
8410 char_u *newres;
8411 char_u *repl;
8412 int srclen;
8413 char_u *p;
8414
8415 result = vim_strsave(arg);
8416 if (result == NULL)
8417 return NULL;
8418
8419 for (p = result; *p; )
8420 {
8421 if (STRNCMP(p, "<sfile>", 7) != 0)
8422 ++p;
8423 else
8424 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008425 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008426 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 if (errormsg != NULL)
8428 {
8429 if (*errormsg)
8430 emsg(errormsg);
8431 vim_free(result);
8432 return NULL;
8433 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008434 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {
8436 p += srclen;
8437 continue;
8438 }
8439 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8440 newres = alloc(len);
8441 if (newres == NULL)
8442 {
8443 vim_free(repl);
8444 vim_free(result);
8445 return NULL;
8446 }
8447 mch_memmove(newres, result, (size_t)(p - result));
8448 STRCPY(newres + (p - result), repl);
8449 len = (int)STRLEN(newres);
8450 STRCAT(newres, p + srclen);
8451 vim_free(repl);
8452 vim_free(result);
8453 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008454 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 }
8456 }
8457
8458 return result;
8459}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008462/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008463 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008464 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008467dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 if (fname == NULL)
8470 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008471 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472}
8473#endif
8474
8475/*
8476 * ":behave {mswin,xterm}"
8477 */
8478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008479ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480{
8481 if (STRCMP(eap->arg, "mswin") == 0)
8482 {
8483 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8484 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8485 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8486 set_option_value((char_u *)"keymodel", 0L,
8487 (char_u *)"startsel,stopsel", 0);
8488 }
8489 else if (STRCMP(eap->arg, "xterm") == 0)
8490 {
8491 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8492 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8493 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8494 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8495 }
8496 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008497 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498}
8499
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500static int filetype_detect = FALSE;
8501static int filetype_plugin = FALSE;
8502static int filetype_indent = FALSE;
8503
8504/*
8505 * ":filetype [plugin] [indent] {on,off,detect}"
8506 * on: Load the filetype.vim file to install autocommands for file types.
8507 * off: Load the ftoff.vim file to remove all autocommands for file types.
8508 * plugin on: load filetype.vim and ftplugin.vim
8509 * plugin off: load ftplugof.vim
8510 * indent on: load filetype.vim and indent.vim
8511 * indent off: load indoff.vim
8512 */
8513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008514ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
8516 char_u *arg = eap->arg;
8517 int plugin = FALSE;
8518 int indent = FALSE;
8519
8520 if (*eap->arg == NUL)
8521 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008522 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008523 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 filetype_detect ? "ON" : "OFF",
8525 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8526 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8527 return;
8528 }
8529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008530 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 for (;;)
8532 {
8533 if (STRNCMP(arg, "plugin", 6) == 0)
8534 {
8535 plugin = TRUE;
8536 arg = skipwhite(arg + 6);
8537 continue;
8538 }
8539 if (STRNCMP(arg, "indent", 6) == 0)
8540 {
8541 indent = TRUE;
8542 arg = skipwhite(arg + 6);
8543 continue;
8544 }
8545 break;
8546 }
8547 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8548 {
8549 if (*arg == 'o' || !filetype_detect)
8550 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008551 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 filetype_detect = TRUE;
8553 if (plugin)
8554 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008555 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 filetype_plugin = TRUE;
8557 }
8558 if (indent)
8559 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008560 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 filetype_indent = TRUE;
8562 }
8563 }
8564 if (*arg == 'd')
8565 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008566 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008567 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 }
8569 }
8570 else if (STRCMP(arg, "off") == 0)
8571 {
8572 if (plugin || indent)
8573 {
8574 if (plugin)
8575 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008576 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 filetype_plugin = FALSE;
8578 }
8579 if (indent)
8580 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008581 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 filetype_indent = FALSE;
8583 }
8584 }
8585 else
8586 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008587 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 filetype_detect = FALSE;
8589 }
8590 }
8591 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008592 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593}
8594
8595/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008596 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 */
8598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008599ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
8601 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008602 {
8603 char_u *arg = eap->arg;
8604
8605 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8606 arg += 9;
8607
8608 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8609 if (arg != eap->arg)
8610 did_filetype = FALSE;
8611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008615ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617#ifdef FEAT_DIGRAPHS
8618 if (*eap->arg != NUL)
8619 putdigraph(eap->arg);
8620 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008621 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008623 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624#endif
8625}
8626
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008627#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8628 void
8629set_no_hlsearch(int flag)
8630{
8631 no_hlsearch = flag;
8632# ifdef FEAT_EVAL
8633 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8634# endif
8635}
8636
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637/*
8638 * ":nohlsearch"
8639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008641ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008643 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008644 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646#endif
8647
8648#ifdef FEAT_CRYPT
8649/*
8650 * ":X": Get crypt key
8651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008653ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008655 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008656 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657}
8658#endif
8659
8660#ifdef FEAT_FOLDING
8661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008662ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663{
8664 if (foldManualAllowed(TRUE))
8665 foldCreate(eap->line1, eap->line2);
8666}
8667
8668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008669ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670{
8671 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8672 eap->forceit, FALSE);
8673}
8674
8675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008676ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677{
8678 linenr_T lnum;
8679
Bram Moolenaar4facea32019-10-12 20:17:40 +02008680# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008681 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008682# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008683
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008684 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8686 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8687 ml_setmarked(lnum);
8688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008689 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008691 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008692# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008693 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695}
8696#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008698#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008699/*
8700 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8701 * instead of a quickfix command.
8702 */
8703 int
8704is_loclist_cmd(int cmdidx)
8705{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008706 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008707 return FALSE;
8708 return cmdnames[cmdidx].cmd_name[0] == 'l';
8709}
8710#endif
8711
Bram Moolenaar4facea32019-10-12 20:17:40 +02008712#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008713 int
8714get_pressedreturn(void)
8715{
8716 return ex_pressedreturn;
8717}
8718
8719 void
8720set_pressedreturn(int val)
8721{
8722 ex_pressedreturn = val;
8723}
8724#endif