blob: 0024c99c637826aaff8c491f637268f47ffe5a19 [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 Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), 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 Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# define ex_cclose ex_ni
59# define ex_copen ex_ni
60# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020061# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000063#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
64# define ex_cexpr ex_ni
65#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020067static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020068static 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 Moolenaarc2af0af2020-08-23 21:06:02 +020069static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010070static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020071#if !defined(FEAT_PERL) \
72 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
73 || !defined(FEAT_TCL) \
74 || !defined(FEAT_RUBY) \
75 || !defined(FEAT_LUA) \
76 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000077# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010080static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000082#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
86static void ex_highlight(exarg_T *eap);
87static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void ex_cquit(exarg_T *eap);
89static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_close(exarg_T *eap);
91static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
92static void ex_only(exarg_T *eap);
93static void ex_resize(exarg_T *eap);
94static void ex_stag(exarg_T *eap);
95static void ex_tabclose(exarg_T *eap);
96static void ex_tabonly(exarg_T *eap);
97static void ex_tabnext(exarg_T *eap);
98static void ex_tabmove(exarg_T *eap);
99static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200100#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100101static void ex_pclose(exarg_T *eap);
102static void ex_ptag(exarg_T *eap);
103static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#else
105# define ex_pclose ex_ni
106# define ex_ptag ex_ni
107# define ex_pedit ex_ni
108#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_exit(exarg_T *eap);
111static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100113static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#else
115# define ex_goto ex_ni
116#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100117static void ex_shell(exarg_T *eap);
118static void ex_preserve(exarg_T *eap);
119static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_mode(exarg_T *eap);
121static void ex_wrongmodifier(exarg_T *eap);
122static void ex_find(exarg_T *eap);
123static void ex_open(exarg_T *eap);
124static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifndef FEAT_GUI
126# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100129#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#else
132# define ex_tearoff ex_ni
133#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100134#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
135 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100136static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#else
138# define ex_popup ex_ni
139#endif
140#ifndef FEAT_GUI_MSWIN
141# define ex_simalt ex_ni
142#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000143#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144# define gui_mch_find_dialog ex_ni
145# define gui_mch_replace_dialog ex_ni
146#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000147#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# define ex_helpfind ex_ni
149#endif
150#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100151# define ex_cscope ex_ni
152# define ex_scscope ex_ni
153# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#endif
155#ifndef FEAT_SYN_HL
156# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200157# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000158#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200159#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200160# define ex_syntime ex_ni
161#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000162#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000163# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000164# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000165# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000166# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000167# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200169#ifndef FEAT_PERSISTENT_UNDO
170# define ex_rundo ex_ni
171# define ex_wundo ex_ni
172#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200173#ifndef FEAT_LUA
174# define ex_lua ex_script_ni
175# define ex_luado ex_ni
176# define ex_luafile ex_ni
177#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000178#ifndef FEAT_MZSCHEME
179# define ex_mzscheme ex_script_ni
180# define ex_mzfile ex_ni
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#ifndef FEAT_PERL
183# define ex_perl ex_script_ni
184# define ex_perldo ex_ni
185#endif
186#ifndef FEAT_PYTHON
187# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200188# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189# define ex_pyfile ex_ni
190#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200191#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200192# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200193# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194# define ex_py3file ex_ni
195#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100196#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
197# define ex_pyx ex_script_ni
198# define ex_pyxdo ex_ni
199# define ex_pyxfile ex_ni
200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifndef FEAT_TCL
202# define ex_tcl ex_script_ni
203# define ex_tcldo ex_ni
204# define ex_tclfile ex_ni
205#endif
206#ifndef FEAT_RUBY
207# define ex_ruby ex_script_ni
208# define ex_rubydo ex_ni
209# define ex_rubyfile ex_ni
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#ifndef FEAT_KEYMAP
212# define ex_loadkeymap ex_ni
213#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100214static void ex_swapname(exarg_T *eap);
215static void ex_syncbind(exarg_T *eap);
216static void ex_read(exarg_T *eap);
217static void ex_pwd(exarg_T *eap);
218static void ex_equal(exarg_T *eap);
219static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100220static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000222#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#else
225# define ex_winpos ex_ni
226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ex_operators(exarg_T *eap);
228static void ex_put(exarg_T *eap);
229static void ex_copymove(exarg_T *eap);
230static void ex_submagic(exarg_T *eap);
231static void ex_join(exarg_T *eap);
232static void ex_at(exarg_T *eap);
233static void ex_bang(exarg_T *eap);
234static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200235#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ex_wundo(exarg_T *eap);
237static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_redo(exarg_T *eap);
240static void ex_later(exarg_T *eap);
241static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100243static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_startinsert(exarg_T *eap);
247static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_checkpath(exarg_T *eap);
250static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#else
252# define ex_findpat ex_ni
253# define ex_checkpath ex_ni
254#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200255#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#else
258# define ex_psearch ex_ni
259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static void ex_tag(exarg_T *eap);
261static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200263# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200264# define ex_break ex_ni
265# define ex_breakadd ex_ni
266# define ex_breakdel ex_ni
267# define ex_breaklist ex_ni
268# define ex_call ex_ni
269# define ex_catch ex_ni
270# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_continue ex_ni
272# define ex_debug ex_ni
273# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200274# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200275# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277# define ex_echo ex_ni
278# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200280# define ex_endblock 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
Bram Moolenaar340c59e2021-04-24 20:37:03 +0200287# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_finally ex_ni
289# define ex_finish ex_ni
290# define ex_function ex_ni
291# define ex_if ex_ni
292# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200293# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200294# define ex_lockvar ex_ni
295# define ex_oldfiles ex_ni
296# define ex_options ex_ni
297# define ex_packadd ex_ni
298# define ex_packloadall ex_ni
299# define ex_return ex_ni
300# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_throw ex_ni
302# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_unlet 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 Moolenaar05159a02005-02-26 23:04:13 +0000358#ifndef FEAT_PROFILE
359# define ex_profile ex_ni
360#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200361#ifndef FEAT_TERMINAL
362# define ex_terminal ex_ni
363#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200364#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
365# define ex_xrestore ex_ni
366#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100367#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200368# define ex_popupclear ex_ni
369#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000370
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371/*
372 * Declare cmdnames[].
373 */
374#define DO_DECLARE_EXCMD
375#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200376#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378static char_u dollar_command[2] = {'$', 0};
379
380
381#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100382// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383typedef struct
384{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385 char_u *line; // command line
386 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387} wcmd_T;
388
389/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000390 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000392 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000394struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100396 garray_T *lines_gap; // growarray with line info
397 int current_line; // last read line from growarray
398 int repeating; // TRUE when looping a second time
399 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200400 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 void *cookie;
402};
403
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200404static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100405static int store_loop_line(garray_T *gap, char_u *line);
406static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000407
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100408// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000409struct dbg_stuff
410{
411 int trylevel;
412 int force_abort;
413 except_T *caught_stack;
414 char_u *vv_exception;
415 char_u *vv_throwpoint;
416 int did_emsg;
417 int got_int;
418 int did_throw;
419 int need_rethrow;
420 int check_cstack;
421 except_T *current_exception;
422};
423
Bram Moolenaared203462004-06-16 11:19:22 +0000424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100425save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000426{
427 dsp->trylevel = trylevel; trylevel = 0;
428 dsp->force_abort = force_abort; force_abort = FALSE;
429 dsp->caught_stack = caught_stack; caught_stack = NULL;
430 dsp->vv_exception = v_exception(NULL);
431 dsp->vv_throwpoint = v_throwpoint(NULL);
432
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100433 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000434 dsp->did_emsg = did_emsg; did_emsg = FALSE;
435 dsp->got_int = got_int; got_int = FALSE;
436 dsp->did_throw = did_throw; did_throw = FALSE;
437 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
438 dsp->check_cstack = check_cstack; check_cstack = FALSE;
439 dsp->current_exception = current_exception; current_exception = NULL;
440}
441
442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100443restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000444{
445 suppress_errthrow = FALSE;
446 trylevel = dsp->trylevel;
447 force_abort = dsp->force_abort;
448 caught_stack = dsp->caught_stack;
449 (void)v_exception(dsp->vv_exception);
450 (void)v_throwpoint(dsp->vv_throwpoint);
451 did_emsg = dsp->did_emsg;
452 got_int = dsp->got_int;
453 did_throw = dsp->did_throw;
454 need_rethrow = dsp->need_rethrow;
455 check_cstack = dsp->check_cstack;
456 current_exception = dsp->current_exception;
457}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458#endif
459
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460/*
461 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
462 * command is given.
463 */
464 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100465do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100466 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
468 int save_msg_scroll;
469 int prev_msg_row;
470 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100471 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000472
473 if (improved)
474 exmode_active = EXMODE_VIM;
475 else
476 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100477 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100478 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000479
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100480 // When using ":global /pat/ visual" and then "Q" we return to continue
481 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482 if (global_busy)
483 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
485 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100486 ++RedrawingDisabled; // don't redisplay the window
487 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 ++hold_gui_events;
491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
Bram Moolenaar32526b32019-01-19 17:43:09 +0100493 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 while (exmode_active)
495 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000497 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
498 {
499 exmode_active = FALSE;
500 break;
501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 msg_scroll = TRUE;
503 need_wait_return = FALSE;
504 ex_pressedreturn = FALSE;
505 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100506 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 prev_msg_row = msg_row;
508 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (improved)
510 {
511 cmdline_row = msg_row;
512 do_cmdline(NULL, getexline, NULL, 0);
513 }
514 else
515 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
516 lines_left = Rows - 1;
517
Bram Moolenaardf177f62005-02-22 08:39:57 +0000518 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100519 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000521 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000522 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if (ex_pressedreturn)
526 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100527 // go up one line, to overwrite the ":<CR>" line, so the
528 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 msg_row = prev_msg_row;
530 if (prev_msg_row == Rows - 1)
531 msg_row--;
532 }
533 msg_col = 0;
534 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
535 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100538 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000539 {
540 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000541 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000543 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546
547#ifdef FEAT_GUI
548 --hold_gui_events;
549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 --RedrawingDisabled;
551 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100552 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 need_wait_return = FALSE;
554 msg_scroll = save_msg_scroll;
555}
556
557/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200558 * Print the executed command for when 'verbose' is set.
559 * When "lnum" is 0 only print the command.
560 */
561 static void
562msg_verbose_cmd(linenr_T lnum, char_u *cmd)
563{
564 ++no_wait_return;
565 verbose_enter_scroll();
566
567 if (lnum == 0)
568 smsg(_("Executing: %s"), cmd);
569 else
570 smsg(_("line %ld: %s"), (long)lnum, cmd);
571 if (msg_silent == 0)
572 msg_puts("\n"); // don't overwrite this
573
574 verbose_leave_scroll();
575 --no_wait_return;
576}
577
578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 * Execute a simple command line. Used for translated commands like "*".
580 */
581 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100582do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 return do_cmdline(cmd, NULL, NULL,
585 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
586}
587
588/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100589 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
590 * This allows for using a range without ":" in Vim9 script.
591 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200592 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100593do_cmd_argument(char_u *cmd)
594{
595 return do_cmdline(cmd, NULL, NULL,
596 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
597}
598
599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 * do_cmdline(): execute one Ex command line
601 *
602 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100603 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * 2. Split up in parts separated with '|'.
605 *
606 * This function can be called recursively!
607 *
608 * flags:
609 * DOCMD_VERBOSE - The command will be included in the error message.
610 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100611 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 * DOCMD_KEYTYPED - Don't reset KeyTyped.
613 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
614 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
615 *
616 * return FAIL if cmdline could not be executed, OK otherwise
617 */
618 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100619do_cmdline(
620 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200621 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100622 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100625 char_u *next_cmdline; // next cmd to execute
626 char_u *cmdline_copy = NULL; // copy of cmd line
627 int used_getline = FALSE; // used "fgetline" to obtain command
628 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100630 int count = 0; // line number count
631 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 int retval = OK;
633#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100634 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100635 garray_T lines_ga; // keep lines for ":while"/":for"
636 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200637 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100638 char_u *fname = NULL; // function or script name
639 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
640 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
641 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200643 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200644 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100646 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200647 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000649 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000651 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100653# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654# define cmd_cookie cookie
655#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100656 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200657#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100658 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
659 // location for storing error messages to be converted to an exception.
660 // This ensures that the do_errthrow() call in do_one_cmd() does not
661 // combine the messages stored by an earlier invocation of do_one_cmd()
662 // with the command name of the later one. This would happen when
663 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 saved_msg_list = msg_list;
665 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#endif
667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // It's possible to create an endless loop with ":execute", catch that
669 // here. The value of 200 allows nested function calls, ":source", etc.
670 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100671 if (call_depth >= 200
672#ifdef FEAT_EVAL
673 && call_depth >= p_mfd
674#endif
675 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000677 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // When converting to an exception, we do not include the command name
680 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100681 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 msg_list = saved_msg_list;
683#endif
684 return FAIL;
685 }
686 ++call_depth;
687
688#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100689 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000691 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100695 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++ex_nesting_level;
699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Get the function or script name and the address where the next breakpoint
701 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 fname = func_name(real_cookie);
705 breakpoint = func_breakpoint(real_cookie);
706 dbg_tick = func_dbg_tick(real_cookie);
707 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100708 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100710 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 breakpoint = source_breakpoint(real_cookie);
712 dbg_tick = source_dbg_tick(real_cookie);
713 }
714
715 /*
716 * Initialize "force_abort" and "suppress_errthrow" at the top level.
717 */
718 if (!recursive)
719 {
720 force_abort = FALSE;
721 suppress_errthrow = FALSE;
722 }
723
724 /*
725 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 * exception handling (used when debugging). Otherwise clear it to avoid
727 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000729 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000730 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 initial_trylevel = trylevel;
735
736 /*
737 * "did_throw" will be set to TRUE when an exception is being thrown.
738 */
739 did_throw = FALSE;
740#endif
741 /*
742 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * If force_abort is set, we cancel everything.
745 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100746#ifdef FEAT_EVAL
747 did_emsg_cumul += did_emsg;
748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 did_emsg = FALSE;
750
751 /*
752 * KeyTyped is only set when calling vgetc(). Reset it here when not
753 * calling vgetc() (sourced command lines).
754 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 if (!(flags & DOCMD_KEYTYPED)
756 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 KeyTyped = FALSE;
758
759 /*
760 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000761 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 * - for multiple commands on one line, separated with '|'
763 * - when repeating until there are no more lines (for ":source")
764 */
765 next_cmdline = cmdline;
766 do
767 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100769 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#endif
771
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100772 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (next_cmdline == NULL
774#ifdef FEAT_EVAL
775 && !force_abort
776 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#endif
779 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100780 {
781#ifdef FEAT_EVAL
782 did_emsg_cumul += did_emsg;
783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100789 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * 3. If a line is given: Make a copy, so we can mess with it.
791 */
792
793#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100794 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000795 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 // Each '|' separated command is stored separately in lines_ga, to
798 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100799 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100801 // Check if a function has returned or, unless it has an unclosed
802 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000806 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 func_line_end(real_cookie);
808# endif
809 if (func_has_ended(real_cookie))
810 {
811 retval = FAIL;
812 break;
813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000816 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100817 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818 script_line_end();
819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100821 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
824 retval = FAIL;
825 break;
826 }
827
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100828 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 if (breakpoint != NULL && dbg_tick != NULL
830 && *dbg_tick != debug_tick)
831 {
832 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100833 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100834 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 *dbg_tick = debug_tick;
836 }
837
838 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100839 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100846 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100848 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 *dbg_tick = debug_tick;
851 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000852# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000853 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854 {
855 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100856 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100857 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000858 script_line_start();
859 }
860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#endif
863
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100864 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (next_cmdline == NULL)
866 {
867 /*
868 * Need to set msg_didout for the first line after an ":if",
869 * otherwise the ":if" will be overwritten.
870 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874#ifdef FEAT_EVAL
875 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
876#else
877 0
878#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100879 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
880 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100882 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100883 // returned for the end of a sourced file or executed function
884 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (KeyTyped && !(flags & DOCMD_REPEAT))
886 need_wait_return = FALSE;
887 retval = FAIL;
888 break;
889 }
890 used_getline = TRUE;
891
892 /*
893 * Keep the first typed line. Clear it when more lines are typed.
894 */
895 if (flags & DOCMD_KEEPLINE)
896 {
897 vim_free(repeat_cmdline);
898 if (count == 0)
899 repeat_cmdline = vim_strsave(next_cmdline);
900 else
901 repeat_cmdline = NULL;
902 }
903 }
904
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100905 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 else if (cmdline_copy == NULL)
907 {
908 next_cmdline = vim_strsave(next_cmdline);
909 if (next_cmdline == NULL)
910 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200911 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 retval = FAIL;
913 break;
914 }
915 }
916 cmdline_copy = next_cmdline;
917
918#ifdef FEAT_EVAL
919 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200920 * Inside a while/for loop, and when the command looks like a ":while"
921 * or ":for", the line is stored, because we may need it later when
922 * looping.
923 *
924 * When there is a '|' and another command, it is stored separately,
925 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000926 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200927 *
928 * Pass a different "fgetline" function to do_one_cmd() below,
929 * that it stores lines in or reads them from "lines_ga". Makes it
930 * possible to define a function inside a while/for loop and handles
931 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200933 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200935 cmd_getline = get_loop_line;
936 cmd_cookie = (void *)&cmd_loop_cookie;
937 cmd_loop_cookie.lines_gap = &lines_ga;
938 cmd_loop_cookie.current_line = current_line;
939 cmd_loop_cookie.getline = fgetline;
940 cmd_loop_cookie.cookie = cookie;
941 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
942
943 // Save the current line when encountering it the first time.
944 if (current_line == lines_ga.ga_len
945 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 {
947 retval = FAIL;
948 break;
949 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200950 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200952 else
953 {
954 cmd_getline = fgetline;
955 cmd_cookie = cookie;
956 }
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 did_endif = FALSE;
959#endif
960
961 if (count++ == 0)
962 {
963 /*
964 * All output from the commands is put below each other, without
965 * waiting for a return. Don't do this when executing commands
966 * from a script or when being called recursive (e.g. for ":e
967 * +command file").
968 */
969 if (!(flags & DOCMD_NOWAIT) && !recursive)
970 {
971 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100972 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100974 msg_scroll = TRUE; // put messages below each other
975 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 ++RedrawingDisabled;
977 did_inc = TRUE;
978 }
979 }
980
Bram Moolenaar823654b2020-05-29 23:03:09 +0200981 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100982 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 /*
985 * 2. Execute one '|' separated command.
986 * do_one_cmd() will return NULL if there is no trailing '|'.
987 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
988 */
989 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100990 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_EVAL
992 &cstack,
993#endif
994 cmd_getline, cmd_cookie);
995 --recursive;
996
997#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000998 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100999 // Use "current_line" from "cmd_loop_cookie", it may have been
1000 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002#endif
1003
1004 if (next_cmdline == NULL)
1005 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001006 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 /*
1009 * If the command was typed, remember it for the ':' register.
1010 * Do this AFTER executing the command to make :@: work.
1011 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001012 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 && new_last_cmdline != NULL)
1014 {
1015 vim_free(last_cmdline);
1016 last_cmdline = new_last_cmdline;
1017 new_last_cmdline = NULL;
1018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 else
1021 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001022 // need to copy the command after the '|' to cmdline_copy, for the
1023 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001024 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 next_cmdline = cmdline_copy;
1026 }
1027
1028
1029#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001030 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001032 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001034 {
1035 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
1041 ++current_line;
1042
1043 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 * An ":endwhile", ":endfor" and ":continue" is handled here.
1045 * If we were executing commands, jump back to the ":while" or
1046 * ":for".
1047 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001053 // Jump back to the matching ":while" or ":for". Be careful
1054 // not to use a cs_line[] from an entry that isn't a ":while"
1055 // or ":for": It would make "current_line" invalid and can
1056 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (!did_emsg && !got_int && !did_throw
1058 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 && (cstack.cs_flags[cstack.cs_idx]
1060 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 && cstack.cs_line[cstack.cs_idx] >= 0
1062 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1063 {
1064 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001065 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 // Check for the next breakpoint at or after the ":while"
1070 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001071 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001074 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 fname,
1076 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1077 *dbg_tick = debug_tick;
1078 }
1079 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001082 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001084 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1085 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 }
1088
1089 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001095 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 }
1098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001099 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001100 if (breakpoint != NULL && has_watchexpr())
1101 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001102 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001103 *dbg_tick = debug_tick;
1104 }
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 /*
1107 * When not inside any ":while" loop, clear remembered lines.
1108 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 if (lines_ga.ga_len > 0)
1112 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001113 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1115 free_cmdlines(&lines_ga);
1116 }
1117 current_line = 0;
1118 }
1119
1120 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1122 * being restored at the ":endtry". Reset them here and set the
1123 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1124 * This includes the case where a missing ":endif", ":endwhile" or
1125 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 cstack.cs_lflags &= ~CSL_HAD_FINA;
1130 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1131 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 did_throw ? (void *)current_exception : NULL);
1133 did_emsg = got_int = did_throw = FALSE;
1134 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1135 }
1136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001137 // Update global "trylevel" for recursive calls to do_cmdline() from
1138 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 trylevel = initial_trylevel + cstack.cs_trylevel;
1140
1141 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001142 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 * files) is aborted because of an error, an interrupt, or an uncaught
1144 * exception, cancel everything. If it is left normally, reset
1145 * force_abort to get the non-EH compatible abortion behavior for
1146 * the rest of the script.
1147 */
1148 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1149 force_abort = FALSE;
1150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001151 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001153#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 }
1156 /*
1157 * Continue executing command lines when:
1158 * - no CTRL-C typed, no aborting error, no exception thrown or try
1159 * conditionals need to be checked for executing finally clauses or
1160 * catching an interrupt exception
1161 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 * looping for ":source" command or function call.
1164 */
1165 while (!((got_int
1166#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001167 || (did_emsg && (force_abort || in_vim9script()))
1168 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#endif
1170 )
1171#ifdef FEAT_EVAL
1172 && cstack.cs_trylevel == 0
1173#endif
1174 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001175 && !(did_emsg
1176#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001177 // Keep going when inside try/catch, so that the error can be
1178 // deal with, except when it is a syntax error, it may cause
1179 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001180 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1181#endif
1182 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001183 && (getline_equal(fgetline, cookie, getexmodeline)
1184 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 && (next_cmdline != NULL
1186#ifdef FEAT_EVAL
1187 || cstack.cs_idx >= 0
1188#endif
1189 || (flags & DOCMD_REPEAT)));
1190
1191 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001192 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#ifdef FEAT_EVAL
1194 free_cmdlines(&lines_ga);
1195 ga_clear(&lines_ga);
1196
1197 if (cstack.cs_idx >= 0)
1198 {
1199 /*
1200 * If a sourced file or executed function ran to its end, report the
1201 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001202 * In Vim9 script do not give a second error, executing aborts after
1203 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001205 if (!got_int && !did_throw && !aborting()
1206 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001207 && ((getline_equal(fgetline, cookie, getsourceline)
1208 && !source_finished(fgetline, cookie))
1209 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 && !func_has_ended(real_cookie))))
1211 {
1212 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001213 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001215 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001216 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001217 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001219 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221
1222 /*
1223 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1224 * ":endtry" in a sourced file or executed function. If the try
1225 * conditional is in its finally clause, ignore anything pending.
1226 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001227 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 */
1229 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001230 {
1231 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1232
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001233 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001234 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001235 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1236 &cstack.cs_looplevel);
1237 }
1238 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 trylevel = initial_trylevel;
1240 }
1241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001242 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1243 // lack was reported above and the error message is to be converted to an
1244 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001245 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 ? (char_u *)"endfunction" : (char_u *)NULL);
1247
1248 if (trylevel == 0)
1249 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001250 // Just in case did_throw got set but current_exception wasn't.
1251 if (current_exception == NULL)
1252 did_throw = FALSE;
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 /*
1255 * When an exception is being thrown out of the outermost try
1256 * conditional, discard the uncaught exception, disable the conversion
1257 * of interrupts or errors to exceptions, and ensure that no more
1258 * commands are executed.
1259 */
1260 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001261 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
1263 /*
1264 * On an interrupt or an aborting error not converted to an exception,
1265 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001266 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * when force_abort is set and did_emsg unset in case of an interrupt
1268 * from a finally clause after an error.
1269 */
1270 else if (got_int || (did_emsg && force_abort))
1271 suppress_errthrow = TRUE;
1272 }
1273
1274 /*
1275 * The current cstack will be freed when do_cmdline() returns. An uncaught
1276 * exception will have to be rethrown in the previous cstack. If a function
1277 * has just returned or a script file was just finished and the previous
1278 * cstack belongs to the same function or, respectively, script file, it
1279 * will have to be checked for finally clauses to be executed due to the
1280 * ":return" or ":finish". This is done in do_one_cmd().
1281 */
1282 if (did_throw)
1283 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001284 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001286 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 && ex_nesting_level > func_level(real_cookie) + 1))
1288 {
1289 if (!did_throw)
1290 check_cstack = TRUE;
1291 }
1292 else
1293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001294 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001295 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 --ex_nesting_level;
1297 /*
1298 * Go to debug mode when returning from a function in which we are
1299 * single-stepping.
1300 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001301 if ((getline_equal(fgetline, cookie, getsourceline)
1302 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001304 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ? (char_u *)_("End of sourced file")
1306 : (char_u *)_("End of function"));
1307 }
1308
1309 /*
1310 * Restore the exception environment (done after returning from the
1311 * debugger).
1312 */
1313 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001314 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
1316 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001317
1318 // Cleanup if "cs_emsg_silent_list" remains.
1319 if (cstack.cs_emsg_silent_list != NULL)
1320 {
1321 eslist_T *elem, *temp;
1322
1323 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1324 {
1325 temp = elem->next;
1326 vim_free(elem);
1327 }
1328 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001329#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331 /*
1332 * If there was too much output to fit on the command line, ask the user to
1333 * hit return before redrawing the screen. With the ":global" command we do
1334 * this only once after the command is finished.
1335 */
1336 if (did_inc)
1337 {
1338 --RedrawingDisabled;
1339 --no_wait_return;
1340 msg_scroll = FALSE;
1341
1342 /*
1343 * When just finished an ":if"-":else" which was typed, no need to
1344 * wait for hit-return. Also for an error situation.
1345 */
1346 if (retval == FAIL
1347#ifdef FEAT_EVAL
1348 || (did_endif && KeyTyped && !did_emsg)
1349#endif
1350 )
1351 {
1352 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001353 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355 else if (need_wait_return)
1356 {
1357 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001358 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 * here should not overwrite the command that may be shown before
1360 * doing that.
1361 */
1362 msg_didout |= msg_didout_before_start;
1363 wait_return(FALSE);
1364 }
1365 }
1366
Bram Moolenaar2a942252012-11-28 23:03:07 +01001367#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001368 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001369#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /*
1371 * Reset if_level, in case a sourced script file contains more ":if" than
1372 * ":endif" (could be ":if x | foo | endif").
1373 */
1374 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001375#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 --call_depth;
1378 return retval;
1379}
1380
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001381#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001382/*
1383 * Handle when "did_throw" is set after executing commands.
1384 */
1385 void
1386handle_did_throw()
1387{
1388 char *p = NULL;
1389 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001390 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001391
1392 /*
1393 * If the uncaught exception is a user exception, report it as an
1394 * error. If it is an error exception, display the saved error
1395 * message now. For an interrupt exception, do nothing; the
1396 * interrupt message is given elsewhere.
1397 */
1398 switch (current_exception->type)
1399 {
1400 case ET_USER:
1401 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001402 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001403 current_exception->value);
1404 p = (char *)vim_strsave(IObuff);
1405 break;
1406 case ET_ERROR:
1407 messages = current_exception->messages;
1408 current_exception->messages = NULL;
1409 break;
1410 case ET_INTERRUPT:
1411 break;
1412 }
1413
1414 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1415 current_exception->throw_lnum);
1416 ESTACK_CHECK_SETUP
1417 current_exception->throw_name = NULL;
1418
1419 discard_current_exception(); // uses IObuff if 'verbose'
1420 suppress_errthrow = TRUE;
1421 force_abort = TRUE;
1422
1423 if (messages != NULL)
1424 {
1425 do
1426 {
1427 msglist_T *next = messages->next;
1428 int save_compiling = estack_compiling;
1429
1430 estack_compiling = messages->msg_compiling;
1431 emsg(messages->msg);
1432 vim_free(messages->msg);
1433 vim_free(messages->sfile);
1434 vim_free(messages);
1435 messages = next;
1436 estack_compiling = save_compiling;
1437 }
1438 while (messages != NULL);
1439 }
1440 else if (p != NULL)
1441 {
1442 emsg(p);
1443 vim_free(p);
1444 }
1445 vim_free(SOURCING_NAME);
1446 ESTACK_CHECK_NOW
1447 estack_pop();
1448}
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001451 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 */
1453 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001454get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001456 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 wcmd_T *wp;
1458 char_u *line;
1459
1460 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1461 {
1462 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001463 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001465 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001467 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001469 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001470 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 ++cp->current_line;
1472
1473 return line;
1474 }
1475
1476 KeyTyped = FALSE;
1477 ++cp->current_line;
1478 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001479 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 return vim_strsave(wp->line);
1481}
1482
1483/*
1484 * Store a line in "gap" so that a ":while" loop can execute it again.
1485 */
1486 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001487store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 if (ga_grow(gap, 1) == FAIL)
1490 return FAIL;
1491 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001492 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 return OK;
1495}
1496
1497/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001498 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 */
1500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001501free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 while (gap->ga_len > 0)
1504 {
1505 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1506 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508}
1509#endif
1510
1511/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001512 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1513 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001516getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001517 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001519 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001522 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001525 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1526 // function that's originally used to obtain the lines. This may be
1527 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001528 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001529 cp = (struct loop_cookie *)cookie;
1530 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532 gp = cp->getline;
1533 cp = cp->cookie;
1534 }
1535 return gp == func;
1536#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001537 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538#endif
1539}
1540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001542 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 * getline function. Otherwise return "cookie".
1544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001546getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001547 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001548 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar13505972019-01-24 15:04:48 +01001550#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001551 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001552 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001554 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1555 // cookie that's originally used to obtain the lines. This may be nested
1556 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001557 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001558 cp = (struct loop_cookie *)cookie;
1559 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
1561 gp = cp->getline;
1562 cp = cp->cookie;
1563 }
1564 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001565#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaard5053d02020-06-28 15:51:16 +02001570#if defined(FEAT_EVAL) || defined(PROT)
1571/*
1572 * Get the next line source line without advancing.
1573 */
1574 char_u *
1575getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001576 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001577 void *cookie) // argument for fgetline()
1578{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001579 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001580 struct loop_cookie *cp;
1581 wcmd_T *wp;
1582
1583 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1584 // cookie that's originally used to obtain the lines. This may be nested
1585 // several levels.
1586 gp = fgetline;
1587 cp = (struct loop_cookie *)cookie;
1588 while (gp == get_loop_line)
1589 {
1590 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1591 {
1592 // executing lines a second time, use the stored copy
1593 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1594 return wp->line;
1595 }
1596 gp = cp->getline;
1597 cp = cp->cookie;
1598 }
1599 if (gp == getsourceline)
1600 return source_nextline(cp);
1601 return NULL;
1602}
1603#endif
1604
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605
1606/*
1607 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1608 * ":bwipeout", etc.
1609 * Returns the buffer number.
1610 */
1611 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001612compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613{
1614 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001615 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616 int count = offset;
1617
1618 buf = firstbuf;
1619 while (buf->b_next != NULL && buf->b_fnum < lnum)
1620 buf = buf->b_next;
1621 while (count != 0)
1622 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001623 count += (offset < 0) ? 1 : -1;
1624 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1625 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001627 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001629 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001630 while (buf->b_ml.ml_mfp == NULL)
1631 {
1632 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1633 if (nextbuf == NULL)
1634 break;
1635 buf = nextbuf;
1636 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001637 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001639 if (addr_type == ADDR_LOADED_BUFFERS)
1640 while (buf->b_ml.ml_mfp == NULL)
1641 {
1642 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1643 if (nextbuf == NULL)
1644 break;
1645 buf = nextbuf;
1646 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001647 return buf->b_fnum;
1648}
1649
Bram Moolenaarb7316892019-05-01 18:08:42 +02001650/*
1651 * Return the window number of "win".
1652 * When "win" is NULL return the number of windows.
1653 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001654 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001655current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001656{
1657 win_T *wp;
1658 int nr = 0;
1659
Bram Moolenaar29323592016-07-24 22:04:11 +02001660 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001661 {
1662 ++nr;
1663 if (wp == win)
1664 break;
1665 }
1666 return nr;
1667}
1668
1669 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001670current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001671{
1672 tabpage_T *tp;
1673 int nr = 0;
1674
Bram Moolenaar29323592016-07-24 22:04:11 +02001675 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001676 {
1677 ++nr;
1678 if (tp == tab)
1679 break;
1680 }
1681 return nr;
1682}
1683
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001684 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001685comment_start(char_u *p, int starts_with_colon UNUSED)
1686{
1687#ifdef FEAT_EVAL
1688 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001689 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001690#endif
1691 return *p == '"';
1692}
1693
Bram Moolenaarf240e182014-11-27 18:33:02 +01001694# define CURRENT_WIN_NR current_win_nr(curwin)
1695# define LAST_WIN_NR current_win_nr(NULL)
1696# define CURRENT_TAB_NR current_tab_nr(curtab)
1697# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001698
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699/*
1700 * Execute one Ex command.
1701 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001702 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1703 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 *
1705 * 1. skip comment lines and leading space
1706 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001707 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001709 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 6. parse arguments
1711 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001713 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 *
1715 * This function may be called recursively!
1716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001718do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001719 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001720 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001722 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001724 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001725 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 char_u *p;
1728 linenr_T lnum;
1729 long n;
1730 char *errormsg = NULL; // error message
1731 char_u *after_modifier = NULL;
1732 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001733 cmdmod_T save_cmdmod;
1734 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001735 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001736 int ni; // set when Not Implemented
1737 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001738 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001739#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001740 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001741 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001742 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001743#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001744 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001745 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaara80faa82020-04-12 19:37:17 +02001747 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 ea.line1 = 1;
1749 ea.line2 = 1;
1750#ifdef FEAT_EVAL
1751 ++ex_nesting_level;
1752#endif
1753
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001754 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (quitmore
1756#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001761 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 --quitmore;
1763
1764 /*
1765 * Reset browse, confirm, etc.. They are restored when returning, for
1766 * recursive calls.
1767 */
1768 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001771 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1772 goto doend;
1773
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001774/*
1775 * 1. Skip comment lines and leading white space and colons.
1776 * 2. Handle command modifiers.
1777 */
1778 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001780 ea.cmdlinep = cmdlinep;
1781 ea.getline = fgetline;
1782 ea.cookie = cookie;
1783#ifdef FEAT_EVAL
1784 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001785 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001787 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001788 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001789 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001790#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001791 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001792#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001793 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795#ifdef FEAT_EVAL
1796 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1797 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1798#else
1799 ea.skip = (if_level > 0);
1800#endif
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 * 3. Skip over the range to find the command. Let "p" point to after it.
1804 *
1805 * We need the command to know what kind of range it uses.
1806 */
1807 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001808#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001809 // In Vim9 script a colon is required before the range. This may also be
1810 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001811 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001812 {
1813 may_have_range = FALSE;
1814 for (p = ea.cmd; p >= *cmdlinep; --p)
1815 {
1816 if (*p == ':')
1817 may_have_range = TRUE;
1818 if (p < ea.cmd && !VIM_ISWHITE(*p))
1819 break;
1820 }
1821 }
1822 else
1823 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001824 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001825#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001826 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827
1828#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001829 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001830 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001831 if (ea.cmd == cmd + 1 && *cmd == '$')
1832 // should be "$VAR = val"
1833 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001834 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001835 if (ea.cmdidx == CMD_SIZE)
1836 {
1837 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1838
1839 // If a ':' before the range is missing, give a clearer error
1840 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001841 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001842 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001843 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001844 goto doend;
1845 }
1846 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 else
1849#endif
1850 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001851
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001852#ifdef FEAT_EVAL
1853# ifdef FEAT_PROFILE
1854 // Count this line for profiling if skip is TRUE.
1855 if (do_profiling == PROF_YES
1856 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1857 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1858 {
1859 int skip = did_emsg || got_int || did_throw;
1860
1861 if (ea.cmdidx == CMD_catch)
1862 skip = !skip && !(cstack->cs_idx >= 0
1863 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1864 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1865 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1866 skip = skip || !(cstack->cs_idx >= 0
1867 && !(cstack->cs_flags[cstack->cs_idx]
1868 & (CSF_ACTIVE | CSF_TRUE)));
1869 else if (ea.cmdidx == CMD_finally)
1870 skip = FALSE;
1871 else if (ea.cmdidx != CMD_endif
1872 && ea.cmdidx != CMD_endfor
1873 && ea.cmdidx != CMD_endtry
1874 && ea.cmdidx != CMD_endwhile)
1875 skip = ea.skip;
1876
1877 if (!skip)
1878 {
1879 if (getline_equal(fgetline, cookie, get_func_line))
1880 func_line_exec(getline_cookie(fgetline, cookie));
1881 else if (getline_equal(fgetline, cookie, getsourceline))
1882 script_line_exec();
1883 }
1884 }
1885# endif
1886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001887 // May go to debug mode. If this happens and the ">quit" debug command is
1888 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001889 dbg_check_breakpoint(&ea);
1890 if (!ea.skip && got_int)
1891 {
1892 ea.skip = TRUE;
1893 (void)do_intthrow(cstack);
1894 }
1895#endif
1896
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001897/*
1898 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 *
1900 * where 'addr' is:
1901 *
1902 * % (entire file)
1903 * $ [+-NUM]
1904 * 'x [+-NUM] (where x denotes a currently defined mark)
1905 * . [+-NUM]
1906 * [+-NUM]..
1907 * NUM
1908 *
1909 * The ea.cmd pointer is updated to point to the first character following the
1910 * range spec. If an initial address is found, but no second, the upper bound
1911 * is equal to the lower.
1912 */
1913
Bram Moolenaar25190db2019-05-04 15:05:28 +02001914 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001915 if (!IS_USER_CMDIDX(ea.cmdidx))
1916 {
1917 if (ea.cmdidx != CMD_SIZE)
1918 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1919 else
1920 ea.addr_type = ADDR_LINES;
1921
Bram Moolenaar25190db2019-05-04 15:05:28 +02001922 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001923 if (ea.cmdidx == CMD_wincmd && p != NULL)
1924 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001925#ifdef FEAT_QUICKFIX
1926 // :.cc in quickfix window uses line number
1927 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1928 ea.addr_type = ADDR_OTHER;
1929#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001930 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001931
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001932 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001933#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001934 if (!may_have_range)
1935 ea.line1 = ea.line2 = default_address(&ea);
1936 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001937#endif
1938 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1939 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001942 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
1944
1945 /*
1946 * Skip ':' and any white space
1947 */
1948 ea.cmd = skipwhite(ea.cmd);
1949 while (*ea.cmd == ':')
1950 ea.cmd = skipwhite(ea.cmd + 1);
1951
1952 /*
1953 * If we got a line, but no command, then go to the line.
1954 * If we find a '|' or '\n' we set ea.nextcmd.
1955 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001956 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1957 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 /*
1960 * strange vi behaviour:
1961 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001962 * ":3|..." prints line 3 (not in Vim9 script)
1963 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001965 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001967 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 goto doend;
1969 }
1970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 // If this looks like an undefined user command and there are CmdUndefined
1972 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001973 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1974 && ASCII_ISUPPER(*ea.cmd)
1975 && has_cmdundefined())
1976 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001977 int ret;
1978
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001979 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001980 while (ASCII_ISALNUM(*p))
1981 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001982 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001983 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1984 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // If the autocommands did something and didn't cause an error, try
1986 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001987 p = (ret
1988#ifdef FEAT_EVAL
1989 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001990#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001992 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (p == NULL)
1995 {
1996 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001997 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 goto doend;
1999 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002000 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002001 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2002 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
2004 errormsg = uc_fun_cmd();
2005 goto doend;
2006 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (ea.cmdidx == CMD_SIZE)
2009 {
2010 if (!ea.skip)
2011 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002012 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // If the modifier was parsed OK the error must be in the
2016 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002017 if (after_modifier != NULL)
2018 append_command(after_modifier);
2019 else
2020 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002021 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002022 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002023 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002024 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 goto doend;
2027 }
2028
Bram Moolenaar958636c2014-10-21 20:01:58 +02002029 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2030 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002031#ifdef HAVE_EX_SCRIPT_NI
2032 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2033#endif
2034 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036#ifndef FEAT_EVAL
2037 /*
2038 * When the expression evaluation is disabled, recognize the ":if" and
2039 * ":endif" commands and ignore everything in between it.
2040 */
2041 if (ea.cmdidx == CMD_if)
2042 ++if_level;
2043 if (if_level)
2044 {
2045 if (ea.cmdidx == CMD_endif)
2046 --if_level;
2047 goto doend;
2048 }
2049
2050#endif
2051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002052 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002053 if (*p == '!' && ea.cmdidx != CMD_substitute
2054 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 ++p;
2057 ea.forceit = TRUE;
2058 }
2059 else
2060 ea.forceit = FALSE;
2061
2062/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002063 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002065 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002066 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 if (!ea.skip)
2069 {
2070#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002071 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002073 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002074 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 goto doend;
2076 }
2077#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002078 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002079 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002080 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002081 goto doend;
2082 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002083 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002085 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002086 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 goto doend;
2088 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002089
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002090 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002092#ifdef FEAT_CMDWIN
2093 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2094 {
2095 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002096 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002097 goto doend;
2098 }
2099#endif
2100 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2101 {
2102 // Command not allowed when text is locked
2103 errormsg = _(get_text_locked_msg());
2104 goto doend;
2105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // Disallow editing another buffer when "curbuf_lock" is set.
2109 // Do allow ":checktime" (it is postponed).
2110 // Do allow ":edit" (check for an argument later).
2111 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002112 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002113 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002114 && ea.cmdidx != CMD_edit
2115 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002116 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002117 && curbuf_locked())
2118 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002120 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002122 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 goto doend;
2124 }
2125 }
2126
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002127 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002129 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 goto doend;
2131 }
2132
2133 /*
2134 * Don't complain about the range if it is not used
2135 * (could happen if line_count is accidentally set to 0).
2136 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002137 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
2139 /*
2140 * If the range is backwards, ask for confirmation and, if given, swap
2141 * ea.line1 & ea.line2 so it's forwards again.
2142 * When global command is busy, don't ask, will fail below.
2143 */
2144 if (!global_busy && ea.line1 > ea.line2)
2145 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002146 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002148 if (sourcing || exmode_active)
2149 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002150 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002151 goto doend;
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (ask_yesno((char_u *)
2154 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002155 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
2157 lnum = ea.line1;
2158 ea.line1 = ea.line2;
2159 ea.line2 = lnum;
2160 }
2161 if ((errormsg = invalid_range(&ea)) != NULL)
2162 goto doend;
2163 }
2164
Bram Moolenaarb7316892019-05-01 18:08:42 +02002165 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2166 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 ea.line2 = 1;
2168
2169 correct_range(&ea);
2170
2171#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002172 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002173 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002175 // Put the first line at the start of a closed fold, put the last line
2176 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 (void)hasFolding(ea.line1, &ea.line1, NULL);
2178 (void)hasFolding(ea.line2, NULL, &ea.line2);
2179 }
2180#endif
2181
2182#ifdef FEAT_QUICKFIX
2183 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002184 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 * option here, so things like % get expanded.
2186 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002187 p = replace_makeprg(&ea, p, cmdlinep);
2188 if (p == NULL)
2189 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190#endif
2191
2192 /*
2193 * Skip to start of argument.
2194 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2195 */
2196 if (ea.cmdidx == CMD_bang)
2197 ea.arg = p;
2198 else
2199 ea.arg = skipwhite(p);
2200
Bram Moolenaar379fb762018-08-30 15:58:28 +02002201 // ":file" cannot be run with an argument when "curbuf_lock" is set
2202 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2203 goto doend;
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 /*
2206 * Check for "++opt=val" argument.
2207 * Must be first, allow ":w ++enc=utf8 !cmd"
2208 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002209 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2211 if (getargopt(&ea) == FAIL && !ni)
2212 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002213 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 goto doend;
2215 }
2216
2217 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002219 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002221 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002223 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 goto doend;
2225 }
2226 ea.arg = skipwhite(ea.arg + 1);
2227 ea.append = TRUE;
2228 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002229 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
2231 ++ea.arg;
2232 ea.usefilter = TRUE;
2233 }
2234 }
2235
2236 if (ea.cmdidx == CMD_read)
2237 {
2238 if (ea.forceit)
2239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002240 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 ea.forceit = FALSE;
2242 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002243 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
2245 ++ea.arg;
2246 ea.usefilter = TRUE;
2247 }
2248 }
2249
2250 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2251 {
2252 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
2255 ++ea.arg;
2256 ++ea.amount;
2257 }
2258 ea.arg = skipwhite(ea.arg);
2259 }
2260
2261 /*
2262 * Check for "+command" argument, before checking for next command.
2263 * Don't do this for ":read !cmd" and ":write !cmd".
2264 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002265 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2267
2268 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002269 * For commands that do not use '|' inside their argument: Check for '|' to
2270 * separate commands and '"' or '#' to start comments.
2271 *
2272 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002273 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002274 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002275 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002277 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2278 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002279 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002280 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002281 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002282 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002283 || ea.cmdidx == CMD_global
2284 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002285 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002286#ifdef FEAT_EVAL
2287 || inside_block(&ea)
2288#endif
2289 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 for (p = ea.arg; *p; ++p)
2292 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002293 // Remove one backslash before a newline, so that it's possible to
2294 // pass a newline to the shell and also a newline that is preceded
2295 // with a backslash. This makes it impossible to end a shell
2296 // command in a backslash, but that doesn't appear useful.
2297 // Halving the number of backslashes is incompatible with previous
2298 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002300 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002301 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 ea.nextcmd = p + 1;
2304 *p = NUL;
2305 break;
2306 }
2307 }
2308 }
2309
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002310 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002311 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002313 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002316 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002317 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002318 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002320#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002321 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002322 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002324 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002325 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002328 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2329 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002330 {
2331 ea.regname = *ea.arg++;
2332#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002333 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002334 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2335 {
kuuote08d7b1c2021-10-04 22:17:36 +01002336 if (!ea.skip)
2337 {
2338 set_expr_line(vim_strsave(ea.arg), &ea);
2339 did_set_expr_line = TRUE;
2340 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002341 ea.arg += STRLEN(ea.arg);
2342 }
2343#endif
2344 ea.arg = skipwhite(ea.arg);
2345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
2347
2348 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002349 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 * count, it's a buffer name.
2351 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002352 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002353 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002354 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002356 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002358 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002360 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 goto doend;
2362 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002363 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
2365 ea.line2 = n;
2366 if (ea.addr_count == 0)
2367 ea.addr_count = 1;
2368 }
2369 else
2370 {
2371 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002372 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002373 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002374 else
2375 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 ++ea.addr_count;
2377 /*
2378 * Be vi compatible: no error message for out of range.
2379 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002380 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 ea.line2 = curbuf->b_ml.ml_line_count;
2382 }
2383 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002384
2385 /*
2386 * Check for flags: 'l', 'p' and '#'.
2387 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002388 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002389 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002390 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2391 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002393 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002394 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 goto doend;
2396 }
2397
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002400 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 goto doend;
2402 }
2403
2404#ifdef FEAT_EVAL
2405 /*
2406 * Skip the command when it's not going to be executed.
2407 * The commands like :if, :endif, etc. always need to be executed.
2408 * Also make an exception for commands that handle a trailing command
2409 * themselves.
2410 */
2411 if (ea.skip)
2412 {
2413 switch (ea.cmdidx)
2414 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002415 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 case CMD_while:
2417 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002418 case CMD_for:
2419 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 case CMD_if:
2421 case CMD_elseif:
2422 case CMD_else:
2423 case CMD_endif:
2424 case CMD_try:
2425 case CMD_catch:
2426 case CMD_finally:
2427 case CMD_endtry:
2428 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002429 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 break;
2431
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002432 // Commands that handle '|' themselves. Check: A command should
2433 // either have the EX_TRLBAR flag, appear in this list or appear in
2434 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CMD_aboveleft:
2436 case CMD_and:
2437 case CMD_belowright:
2438 case CMD_botright:
2439 case CMD_browse:
2440 case CMD_call:
2441 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002442 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_delfunction:
2444 case CMD_djump:
2445 case CMD_dlist:
2446 case CMD_dsearch:
2447 case CMD_dsplit:
2448 case CMD_echo:
2449 case CMD_echoerr:
2450 case CMD_echomsg:
2451 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002452 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002454 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002455 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 case CMD_help:
2457 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002458 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 case CMD_ijump:
2460 case CMD_ilist:
2461 case CMD_isearch:
2462 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002463 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_keepjumps:
2465 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002466 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 case CMD_leftabove:
2468 case CMD_let:
2469 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002470 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002471 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002473 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002474 case CMD_noautocmd:
2475 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 case CMD_perl:
2477 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002478 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002479 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002480 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 case CMD_return:
2482 case CMD_rightbelow:
2483 case CMD_ruby:
2484 case CMD_silent:
2485 case CMD_smagic:
2486 case CMD_snomagic:
2487 case CMD_substitute:
2488 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002489 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 case CMD_tcl:
2491 case CMD_throw:
2492 case CMD_tilde:
2493 case CMD_topleft:
2494 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002495 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002496 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 case CMD_verbose:
2498 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002499 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 break;
2501
2502 default: goto doend;
2503 }
2504 }
2505#endif
2506
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002507 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2510 goto doend;
2511 }
2512
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 /*
2514 * Accept buffer name. Cannot be used at the same time with a buffer
2515 * number. Don't do this for a user command.
2516 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002517 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002518 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
2520 /*
2521 * :bdelete, :bwipeout and :bunload take several arguments, separated
2522 * by spaces: find next space (skipping over escaped characters).
2523 * The others take one argument: ignore trailing spaces.
2524 */
2525 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2526 || ea.cmdidx == CMD_bunload)
2527 p = skiptowhite_esc(ea.arg);
2528 else
2529 {
2530 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002531 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 --p;
2533 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002534 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002535 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002536 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 goto doend;
2538 ea.addr_count = 1;
2539 ea.arg = skipwhite(p);
2540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002542 // The :try command saves the emsg_silent flag, reset it here when
2543 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002544 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002545 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002546 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002547 if (emsg_silent < 0)
2548 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002549 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002550 }
2551
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002553 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaar958636c2014-10-21 20:01:58 +02002556 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 /*
2559 * Execute a user-defined command.
2560 */
2561 do_ucmd(&ea);
2562 }
2563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002566 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
2568 ea.errmsg = NULL;
2569 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2570 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002571 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573
2574#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002576 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002577 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002578 && current_sctx.sc_sid > 0
2579 && ea.cmdidx != CMD_endif
2580 && (cstack->cs_idx < 0
2581 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002582 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 /*
2585 * If the command just executed called do_cmdline(), any throw or ":return"
2586 * or ":finish" encountered there must also check the cstack of the still
2587 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2588 * exception, or reanimate a returned function or finished script file and
2589 * return or finish it again.
2590 */
2591 if (need_rethrow)
2592 do_throw(cstack);
2593 else if (check_cstack)
2594 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002595 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002597 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 && current_func_returned())
2599 do_return(&ea, TRUE, FALSE, NULL);
2600 }
2601 need_rethrow = check_cstack = FALSE;
2602#endif
2603
2604doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002605 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002608 curwin->w_cursor.col = 0;
2609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
2611 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2612 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002613 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002615 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
2617 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002618 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002620 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
2622 emsg(errormsg);
2623 }
2624#ifdef FEAT_EVAL
2625 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002626 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2627 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002628
2629 if (did_set_expr_line)
2630 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631#endif
2632
Bram Moolenaare1004402020-10-24 20:49:43 +02002633 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002635 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002636 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002638 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 ea.nextcmd = NULL;
2640
2641#ifdef FEAT_EVAL
2642 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002643 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644#endif
2645
2646 return ea.nextcmd;
2647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002649static char ex_error_buf[MSG_BUF_LEN];
2650
2651/*
2652 * Return an error message with argument included.
2653 * Uses a static buffer, only the last error will be kept.
2654 * "msg" will be translated, caller should use N_().
2655 */
2656 char *
2657ex_errmsg(char *msg, char_u *arg)
2658{
2659 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2660 return ex_error_buf;
2661}
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002664 * Handle a range without a command.
2665 * Returns an error message on failure.
2666 */
2667 char *
2668ex_range_without_command(exarg_T *eap)
2669{
2670 char *errormsg = NULL;
2671
2672 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2673#ifdef FEAT_EVAL
2674 && !in_vim9script()
2675#endif
2676 )
2677 {
2678 eap->cmdidx = CMD_print;
2679 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2680 if ((errormsg = invalid_range(eap)) == NULL)
2681 {
2682 correct_range(eap);
2683 ex_print(eap);
2684 }
2685 }
2686 else if (eap->addr_count != 0)
2687 {
2688 if (eap->line2 > curbuf->b_ml.ml_line_count)
2689 {
2690 // With '-' in 'cpoptions' a line number past the file is an
2691 // error, otherwise put it at the end of the file.
2692 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2693 eap->line2 = -1;
2694 else
2695 eap->line2 = curbuf->b_ml.ml_line_count;
2696 }
2697
2698 if (eap->line2 < 0)
2699 errormsg = _(e_invalid_range);
2700 else
2701 {
2702 if (eap->line2 == 0)
2703 curwin->w_cursor.lnum = 1;
2704 else
2705 curwin->w_cursor.lnum = eap->line2;
2706 beginline(BL_SOL | BL_FIX);
2707 }
2708 }
2709 return errormsg;
2710}
2711
2712/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002713 * Check for an Ex command with optional tail.
2714 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002715 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002716 */
2717 static int
2718checkforcmd_opt(
2719 char_u **pp, // start of command
2720 char *cmd, // name of command
2721 int len, // required length
2722 int noparen)
2723{
2724 int i;
2725
2726 for (i = 0; cmd[i] != NUL; ++i)
2727 if (((char_u *)cmd)[i] != (*pp)[i])
2728 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002729 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2730 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002731 {
2732 *pp = skipwhite(*pp + i);
2733 return TRUE;
2734 }
2735 return FALSE;
2736}
2737
2738/*
2739 * Check for an Ex command with optional tail.
2740 * If there is a match advance "pp" to the argument and return TRUE.
2741 */
2742 int
2743checkforcmd(
2744 char_u **pp, // start of command
2745 char *cmd, // name of command
2746 int len) // required length
2747{
2748 return checkforcmd_opt(pp, cmd, len, FALSE);
2749}
2750
2751/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002752 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002753 * If there is a match advance "pp" to the argument and return TRUE.
2754 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002755 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002756checkforcmd_noparen(
2757 char_u **pp, // start of command
2758 char *cmd, // name of command
2759 int len) // required length
2760{
2761 return checkforcmd_opt(pp, cmd, len, TRUE);
2762}
2763
2764/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002765 * Parse and skip over command modifiers:
2766 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002767 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002768 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002769 * When "skip_only" is TRUE the global variables are not changed, except for
2770 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002771 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2772 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002773 * Call apply_cmdmod() to get the side effects of the modifiers:
2774 * - Increment "sandbox" for ":sandbox"
2775 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002776 * - set msg_silent for ":silent"
2777 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 * Return FAIL when the command is not to be executed.
2779 * May set "errormsg" to an error message.
2780 */
2781 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002782parse_command_modifiers(
2783 exarg_T *eap,
2784 char **errormsg,
2785 cmdmod_T *cmod,
2786 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002787{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002788 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002789 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002790 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002791 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002792 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002793 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002794
Bram Moolenaare1004402020-10-24 20:49:43 +02002795 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002796 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002797
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002798 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2799 {
2800 // The automatically inserted Visual area range is skipped, so that
2801 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002802 // range to after the modififiers. The command will be
2803 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2804 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002805 eap->cmd += 5;
2806 cmd_start = eap->cmd;
2807 has_visual_range = TRUE;
2808 }
2809
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002810 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 for (;;)
2812 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002813 char_u *p;
2814
Bram Moolenaareffed932018-08-14 13:38:17 +02002815 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002816 {
2817 if (*eap->cmd == ':')
2818 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002820 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002821
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002822 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002823 if (*eap->cmd == NUL && exmode_active
2824 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2825 || getline_equal(eap->getline, eap->cookie, getexline))
2826 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2827 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002828 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002829 if (!skip_only)
2830 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002831 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 }
2833
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002834 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002835 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002836 {
2837 // a comment ends at a NL
2838 if (eap->nextcmd == NULL)
2839 {
2840 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2841 if (eap->nextcmd != NULL)
2842 ++eap->nextcmd;
2843 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002844 if (vim9script)
2845 {
2846 if (has_cmdmod(cmod, FALSE))
2847 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002848#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002849 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2850 && eap->cmd[2] != '{')
2851 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002852#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002853 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002855 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002856 if (*eap->cmd == NUL)
2857 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002858 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002859 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002860 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002861 if (vim9script && has_cmdmod(cmod, FALSE))
2862 *errormsg = _(e_command_modifier_without_command);
2863 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 return FAIL;
2865 }
2866
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002867 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002868
2869 // In Vim9 script a variable can shadow a command modifier:
2870 // verbose = 123
2871 // verbose += 123
2872 // silent! verbose = func()
2873 // verbose.member = 2
2874 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002875 // But not:
2876 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002877 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002878 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002879 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002880
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002881 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002882 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002883 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002884 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2885 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002886 break;
2887 }
2888
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 switch (*p)
2890 {
Bram Moolenaar91324262022-09-09 13:27:59 +01002891 // When adding an entry, also modify cmdmods[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002892 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002894 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002895 continue;
2896
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002897 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002898 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002899 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 continue;
2901 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002902 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 {
2904#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002905 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002906#endif
2907 continue;
2908 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002909 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002910 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002911 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002912 continue;
2913
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002914 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002915 break;
2916#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002917 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002918#endif
2919 continue;
2920
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002921 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002923 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002924 continue;
2925 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002926 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002928 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002929 continue;
2930 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002931 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002932 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002933 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002934 continue;
2935 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002936 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002937 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002938 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002939 continue;
2940
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002941 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002942 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002943 char_u *reg_pat;
2944 char_u *nulp = NULL;
2945 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002946
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002947 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002948 || *p == NUL
2949 || (ends_excmd(*p)
2950#ifdef FEAT_EVAL
2951 // in ":filter #pat# cmd" # does not
2952 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002953 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002954#endif
2955 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 break;
2957 if (*p == '!')
2958 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002959 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 p = skipwhite(p + 1);
2961 if (*p == NUL || ends_excmd(*p))
2962 break;
2963 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002964#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002965 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002966 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002967 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002968#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002969 if (skip_only)
2970 p = skip_vimgrep_pat(p, NULL, NULL);
2971 else
2972 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002973 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2974 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 if (p == NULL || *p == NUL)
2976 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002977 if (!skip_only)
2978 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002979 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002980 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002981 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002982 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002983 // restore the character overwritten by NUL
2984 if (nulp != NULL)
2985 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002986 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 eap->cmd = p;
2988 continue;
2989 }
2990
Bram Moolenaar21c3a802022-08-31 17:49:14 +01002991 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
2992 {
2993 cmod->cmod_split |= WSP_HOR;
2994 continue;
2995 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002996 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01002997 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002998 || *p == NUL || ends_excmd(*p))
2999 break;
3000 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 continue;
3003
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003004 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003005 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003006 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 continue;
3008 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003009 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3010 {
3011 if (ends_excmd2(p, eap->cmd))
3012 {
3013 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003014 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003015 return FAIL;
3016 }
3017 cmod->cmod_flags |= CMOD_LEGACY;
3018 continue;
3019 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003020
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003021 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003022 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003023 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003024 continue;
3025
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003026 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003028 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003029 continue;
3030 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003031 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003032 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003033 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003034 continue;
3035
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003036 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003037 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003038 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003039 continue;
3040
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003041 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003043 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003044 continue;
3045 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003046 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003048 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003049 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3050 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003051 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003053 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003054 }
3055 continue;
3056
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003057 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003058 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003059 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003060 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003061 long tabnr = get_address(eap, &eap->cmd,
3062 ADDR_TABS, eap->skip,
3063 skip_only, FALSE, 1);
3064 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003065 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003066 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003067 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003068 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3069 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003070 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003071 return FAIL;
3072 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003073 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003074 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003075 }
3076 eap->cmd = p;
3077 continue;
3078 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003079 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003080 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003081 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003082 continue;
3083
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003084 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003085 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003086 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003087 continue;
3088
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003089 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003090 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003091 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003092 continue;
3093 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003094 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003095 {
3096 if (ends_excmd2(p, eap->cmd))
3097 {
3098 *errormsg =
3099 _(e_vim9cmd_must_be_followed_by_command);
3100 return FAIL;
3101 }
3102 cmod->cmod_flags |= CMOD_VIM9CMD;
3103 continue;
3104 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003105 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003106 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003107 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003108 {
zeertzjqcd749632022-06-14 13:30:35 +01003109 // zero means not set, one is verbose == 0, etc.
3110 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003111 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003112 else
zeertzjqcd749632022-06-14 13:30:35 +01003113 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003114 eap->cmd = p;
3115 continue;
3116 }
3117 break;
3118 }
3119
Bram Moolenaar1501b632022-03-27 16:56:21 +01003120 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003121 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003122 if (eap->cmd > cmd_start)
3123 {
3124 // Move the '<,'> range to after the modifiers and insert a colon.
3125 // Since the modifiers have been parsed put the colon on top of the
3126 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3127 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003128 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003129 {
3130 size_t len = STRLEN(cmd_start);
3131
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003132 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003133 // "'<,'>mods" -> "mods *+
3134 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003135 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003136 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003137 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003138 }
3139 else
3140 {
3141 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3142 eap->cmd -= 5;
3143 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3144 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003145 }
3146 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003147 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003148 // Special case: change empty command to "+".
3149 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003150 eap->cmd = (char_u *)"'<,'>+";
3151 else
3152 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003153 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003154 else if (use_plus_cmd)
3155 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003156
Bram Moolenaareffed932018-08-14 13:38:17 +02003157 return OK;
3158}
3159
3160/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003161 * Return TRUE if "cmod" has anything set.
3162 */
3163 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003164has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003165{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003166 return (cmod->cmod_flags != 0 && (!ignore_silent
3167 || (cmod->cmod_flags
3168 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003169 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003170 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003171 || cmod->cmod_tab != 0
3172 || cmod->cmod_filter_regmatch.regprog != NULL;
3173}
3174
Bram Moolenaar8991be22022-02-14 21:51:46 +00003175#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003176/*
3177 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3178 */
3179 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003180cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003181{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003182 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003183 {
3184 emsg(_(e_misplaced_command_modifier));
3185 return TRUE;
3186 }
3187 return FALSE;
3188}
Dominique Pelle748b3082022-01-08 12:41:16 +00003189#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003190
3191/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003192 * Apply the command modifiers. Saves current state in "cmdmod", call
3193 * undo_cmdmod() later.
3194 */
3195 void
3196apply_cmdmod(cmdmod_T *cmod)
3197{
3198#ifdef HAVE_SANDBOX
3199 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3200 {
3201 ++sandbox;
3202 cmod->cmod_did_sandbox = TRUE;
3203 }
3204#endif
zeertzjqcd749632022-06-14 13:30:35 +01003205 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003206 {
3207 if (cmod->cmod_verbose_save == 0)
3208 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003209 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003210 }
3211
3212 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3213 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003214 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003215 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003216 cmod->cmod_save_msg_scroll = msg_scroll;
3217 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003218 if (cmod->cmod_flags & CMOD_SILENT)
3219 ++msg_silent;
3220 if (cmod->cmod_flags & CMOD_UNSILENT)
3221 msg_silent = 0;
3222
3223 if (cmod->cmod_flags & CMOD_ERRSILENT)
3224 {
3225 ++emsg_silent;
3226 ++cmod->cmod_did_esilent;
3227 }
3228
Bram Moolenaare1004402020-10-24 20:49:43 +02003229 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003230 {
3231 // Set 'eventignore' to "all".
3232 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003233 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003234 set_string_option_direct((char_u *)"ei", -1,
3235 (char_u *)"all", OPT_FREE, SID_NONE);
3236 }
3237}
3238
3239/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003240 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003241 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003242 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003243undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003244{
Bram Moolenaare1004402020-10-24 20:49:43 +02003245 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003246 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003247 p_verbose = cmod->cmod_verbose_save - 1;
3248 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003249 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003250
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003251#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003252 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003253 {
3254 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003255 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003256 }
3257#endif
3258
Bram Moolenaare1004402020-10-24 20:49:43 +02003259 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003260 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003261 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003262 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3263 OPT_FREE, SID_NONE);
3264 free_string_option(cmod->cmod_save_ei);
3265 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003266 }
3267
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003268 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003269
Bram Moolenaare1004402020-10-24 20:49:43 +02003270 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003271 {
3272 // messages could be enabled for a serious error, need to check if the
3273 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003274 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3275 msg_silent = cmod->cmod_save_msg_silent - 1;
3276 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003277 if (emsg_silent < 0)
3278 emsg_silent = 0;
3279 // Restore msg_scroll, it's set by file I/O commands, even when no
3280 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003281 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003282
3283 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3284 // somewhere in the line. Put it back in the first column.
3285 if (redirecting())
3286 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003287
Bram Moolenaare1004402020-10-24 20:49:43 +02003288 cmod->cmod_save_msg_silent = 0;
3289 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003290 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003291}
3292
3293/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003294 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003295 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003296 * Return FAIL and set "errormsg" or return OK.
3297 */
3298 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003299parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003300{
3301 int address_count = 1;
3302 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003303 int need_check_cursor = FALSE;
3304 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003305
3306 // Repeat for all ',' or ';' separated addresses.
3307 for (;;)
3308 {
3309 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003310 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003311 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003312 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003313 eap->addr_count == 0, address_count++);
3314 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003315 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003316 if (lnum == MAXLNUM)
3317 {
3318 if (*eap->cmd == '%') // '%' - all lines
3319 {
3320 ++eap->cmd;
3321 switch (eap->addr_type)
3322 {
3323 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003324 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003325 eap->line1 = 1;
3326 eap->line2 = curbuf->b_ml.ml_line_count;
3327 break;
3328 case ADDR_LOADED_BUFFERS:
3329 {
3330 buf_T *buf = firstbuf;
3331
3332 while (buf->b_next != NULL
3333 && buf->b_ml.ml_mfp == NULL)
3334 buf = buf->b_next;
3335 eap->line1 = buf->b_fnum;
3336 buf = lastbuf;
3337 while (buf->b_prev != NULL
3338 && buf->b_ml.ml_mfp == NULL)
3339 buf = buf->b_prev;
3340 eap->line2 = buf->b_fnum;
3341 break;
3342 }
3343 case ADDR_BUFFERS:
3344 eap->line1 = firstbuf->b_fnum;
3345 eap->line2 = lastbuf->b_fnum;
3346 break;
3347 case ADDR_WINDOWS:
3348 case ADDR_TABS:
3349 if (IS_USER_CMDIDX(eap->cmdidx))
3350 {
3351 eap->line1 = 1;
3352 eap->line2 = eap->addr_type == ADDR_WINDOWS
3353 ? LAST_WIN_NR : LAST_TAB_NR;
3354 }
3355 else
3356 {
3357 // there is no Vim command which uses '%' and
3358 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003359 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003360 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003361 }
3362 break;
3363 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003364 case ADDR_UNSIGNED:
3365 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003366 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003367 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003368 case ADDR_ARGUMENTS:
3369 if (ARGCOUNT == 0)
3370 eap->line1 = eap->line2 = 0;
3371 else
3372 {
3373 eap->line1 = 1;
3374 eap->line2 = ARGCOUNT;
3375 }
3376 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003377 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003378#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003379 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003380 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003381 if (eap->line2 == 0)
3382 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003383#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003384 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003385 case ADDR_NONE:
3386 // Will give an error later if a range is found.
3387 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003388 }
3389 ++eap->addr_count;
3390 }
3391 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3392 {
3393 pos_T *fp;
3394
3395 // '*' - visual area
3396 if (eap->addr_type != ADDR_LINES)
3397 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003398 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003399 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003400 }
3401
3402 ++eap->cmd;
3403 if (!eap->skip)
3404 {
3405 fp = getmark('<', FALSE);
3406 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003407 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003408 eap->line1 = fp->lnum;
3409 fp = getmark('>', FALSE);
3410 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003411 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003412 eap->line2 = fp->lnum;
3413 ++eap->addr_count;
3414 }
3415 }
3416 }
3417 else
3418 eap->line2 = lnum;
3419 eap->addr_count++;
3420
3421 if (*eap->cmd == ';')
3422 {
3423 if (!eap->skip)
3424 {
3425 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003426
Bram Moolenaar0e717042020-04-27 19:29:01 +02003427 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003428 // accept zero as address, so 0;/PATTERN/ works correctly
3429 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003430 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003431 if (eap->line2 > 0)
3432 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003433 else
3434 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003435 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003436 }
3437 }
3438 else if (*eap->cmd != ',')
3439 break;
3440 ++eap->cmd;
3441 }
3442
3443 // One address given: set start and end lines.
3444 if (eap->addr_count == 1)
3445 {
3446 eap->line1 = eap->line2;
3447 // ... but only implicit: really no address given
3448 if (lnum == MAXLNUM)
3449 eap->addr_count = 0;
3450 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003451 ret = OK;
3452
3453theend:
3454 if (need_check_cursor)
3455 check_cursor();
3456 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003457}
3458
3459/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003460 * Append "cmd" to the error message in IObuff.
3461 * Takes care of limiting the length and handling 0xa0, which would be
3462 * invisible otherwise.
3463 */
3464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003465append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003466{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003467 size_t len = STRLEN(IObuff);
3468 char_u *s = cmd;
3469 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003470
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003471 if (len > IOSIZE - 100)
3472 {
3473 // Not enough space, truncate and put in "...".
3474 d = IObuff + IOSIZE - 100;
3475 d -= mb_head_off(IObuff, d);
3476 STRCPY(d, "...");
3477 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003478 STRCAT(IObuff, ": ");
3479 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003480 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003481 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003482 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003483 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003484 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003485 STRCPY(d, "<a0>");
3486 d += 4;
3487 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003488 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3489 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003490 else
3491 MB_COPY_CHAR(s, d);
3492 }
3493 *d = NUL;
3494}
3495
Dominique Pelle748b3082022-01-08 12:41:16 +00003496#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003497/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003498 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3499 * the name. Otherwise just return "start".
3500 */
3501 char_u *
3502skip_option_env_lead(char_u *start)
3503{
3504 char_u *name = start;
3505
3506 if (*start == '&')
3507 {
3508 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3509 name += 3;
3510 else
3511 name += 1;
3512 }
3513 else if (*start == '$')
3514 name += 1;
3515 return name;
3516}
Dominique Pelle748b3082022-01-08 12:41:16 +00003517#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003518
3519/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003520 * Return TRUE and set "*idx" if "p" points to a one letter command.
3521 * If not in Vim9 script:
3522 * - The 'k' command can directly be followed by any character.
3523 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3524 * but :sre[wind] is another command, as are :scr[iptnames],
3525 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3526 */
3527 static int
3528one_letter_cmd(char_u *p, cmdidx_T *idx)
3529{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003530 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003531 return FALSE;
3532 if (*p == 'k')
3533 {
3534 *idx = CMD_k;
3535 return TRUE;
3536 }
3537 if (p[0] == 's'
3538 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3539 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3540 || p[1] == 'g'
3541 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3542 || p[1] == 'I'
3543 || (p[1] == 'r' && p[2] != 'e')))
3544 {
3545 *idx = CMD_substitute;
3546 return TRUE;
3547 }
3548 return FALSE;
3549}
3550
3551/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003552 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3553 * call.
3554 */
3555 int
3556number_method(char_u *cmd)
3557{
3558 char_u *p = skipdigits(cmd);
3559
3560 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3561}
3562
3563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003565 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003566 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003567 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 *
3569 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3570 * assignment without "let". Sets eap->cmdidx to the command while returning
3571 * "eap->cmd".
3572 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 * Returns NULL for an ambiguous user command.
3574 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 char_u *
3576find_ex_command(
3577 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003578 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003579 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003580 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 int len;
3583 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003584 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003585#ifndef FEAT_EVAL
3586 int vim9 = FALSE;
3587#else
3588 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 /*
3591 * Recognize a Vim9 script function/method call and assignment:
3592 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3593 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003594 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003595 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003597 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003599 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003600 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3601 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003603 int oplen;
3604 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003605 char_u *swp;
3606
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003607 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003608 || (eap->cmd[0] == '$'
3609 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003610 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003611 && (valid_yank_reg(eap->cmd[1], FALSE)
3612 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003613 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003614 if (*eap->cmd == '&')
3615 {
3616 p = eap->cmd + 1;
3617 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3618 p += 2;
3619 p = to_name_end(p, FALSE);
3620 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003621 else if (*eap->cmd == '$')
3622 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003623 else
3624 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003625 if (ends_excmd(*skipwhite(p)))
3626 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003627 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3628 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003629 eap->cmdidx = CMD_eval;
3630 return eap->cmd;
3631 }
3632 // "&option" can be followed by "->" or "=", check below
3633 }
3634
3635 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003637 if (
3638 // "(..." is an expression.
3639 // "funcname(" is always a function call.
3640 *p == '('
3641 || (p == eap->cmd
3642 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003643 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003644 *eap->cmd == '{'
3645 // "'string'->func()" is an expression.
3646 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003647 // '"string"->func()' is an expression.
3648 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003649 // '$"string"->func()' is an expression.
3650 // "$'string'->func()" is an expression.
3651 || (eap->cmd[0] == '$'
3652 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3653 // '0z1234->func()' is an expression.
3654 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003655 // "g:varname" is an expression.
3656 || eap->cmd[1] == ':'
3657 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003658 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003659 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003660 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003661 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3662 {
3663 // "{" by itself is the start of a block.
3664 eap->cmdidx = CMD_block;
3665 return eap->cmd + 1;
3666 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003667 eap->cmdidx = CMD_eval;
3668 return eap->cmd;
3669 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003670
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003671 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003672 // "varname[]" is an expression.
3673 *p == '['
3674 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003675 || (*p == '.'
3676 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3677 // g:[key] is an expression
3678 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003679 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003680 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003681
3682 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003683 // Skip over the whole thing, it can be:
3684 // name.member = val
3685 // name[a : b] = val
3686 // name[idx] = val
3687 // name[idx].member = val
3688 // etc.
3689 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003690 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003691 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003692 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003693 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003694 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003695 || (after[0] == '.' && after[1] == '.'
3696 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003697 eap->cmdidx = CMD_var;
3698 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003699 --emsg_silent;
3700 return eap->cmd;
3701 }
3702
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003703 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3704 // an assignment.
3705 // If there is no line break inside the "[...]" then "p" is
3706 // advanced to after the "]" by to_name_const_end(): check if a "="
3707 // follows.
3708 // If "[...]" has a line break "p" still points at the "[" and it
3709 // can't be an assignment.
3710 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003711 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003712 char_u *eq;
3713
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003714 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003715 if (p == eap->cmd && *p == '[')
3716 {
3717 int count = 0;
3718 int semicolon = FALSE;
3719
3720 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3721 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003722 eq = p;
3723 if (eq != NULL)
3724 {
3725 eq = skipwhite(eq);
3726 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3727 ++eq;
3728 }
3729 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003730 {
3731 eap->cmdidx = CMD_eval;
3732 return eap->cmd;
3733 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003734 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003735 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003736 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003737 return eap->cmd;
3738 }
3739 }
3740
3741 // Recognize an assignment if we recognize the variable name:
3742 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003743 // "@r = expr"
3744 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003745 // "var = expr" where "var" is a variable name or we are skipping
3746 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003747 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003748 oplen = assignment_len(skipwhite(p), &heredoc);
3749 if (oplen > 0)
3750 {
3751 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3752 || *eap->cmd == '&'
3753 || *eap->cmd == '$'
3754 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003755 || (eap->skip && IS_WHITE_OR_NUL(
3756 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003757 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003758 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003759 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003760 return eap->cmd;
3761 }
3762 }
3763
3764 // Recognize using a type for a w:, b:, t: or g: variable:
3765 // "w:varname: number = 123".
3766 if (eap->cmd[1] == ':' && *p == ':')
3767 {
3768 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003769 return eap->cmd;
3770 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003771 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003772
Bram Moolenaar31d99482022-05-26 22:24:43 +01003773 // 1234->func() is a method call
3774 if (number_method(eap->cmd))
3775 {
3776 eap->cmdidx = CMD_eval;
3777 return eap->cmd;
3778 }
3779
Bram Moolenaar7b829262021-10-13 15:04:34 +01003780 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3781 // an expression. A global/substitute/list command needs to use a
3782 // longer name.
3783 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3784 {
3785 eap->cmdidx = CMD_eval;
3786 return eap->cmd;
3787 }
3788
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003789 // If it is an ID it might be a variable with an operator on the next
3790 // line, if the variable exists it can't be an Ex command.
3791 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003792 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003793 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3794 {
3795 eap->cmdidx = CMD_eval;
3796 return eap->cmd;
3797 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003798
3799 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003800 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3801 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003802 {
3803 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3804 return eap->cmd + 2;
3805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 }
3807#endif
3808
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 /*
3810 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 */
3812 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003813 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 ++p;
3816 }
3817 else
3818 {
3819 while (ASCII_ISALPHA(*p))
3820 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003821 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003822 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003824 while (ASCII_ISALNUM(*p))
3825 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 }
3827 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3828 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003829 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 ++p;
3831 while (ASCII_ISALPHA(*p))
3832 ++p;
3833 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003834
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003835 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003836 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 ++p;
3838 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003839 // The "d" command can directly be followed by 'l' or 'p' flag, when
3840 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003841 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003842 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003843 // Check for ":dl", ":dell", etc. to ":deletel": that's
3844 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003845 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003846 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003847 break;
3848 if (i == len - 1)
3849 {
3850 --len;
3851 if (p[-1] == 'l')
3852 eap->flags |= EXFLAG_LIST;
3853 else
3854 eap->flags |= EXFLAG_PRINT;
3855 }
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003858 if (ASCII_ISLOWER(eap->cmd[0]))
3859 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003860 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003861 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003862
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003863 if (command_count != (int)CMD_SIZE)
3864 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003865 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003866 getout(1);
3867 }
3868
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003869 // Use a precomputed index for fast look-up in cmdnames[]
3870 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003871 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3872 if (ASCII_ISLOWER(c2))
3873 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3874 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003875 else if (ASCII_ISUPPER(eap->cmd[0]))
3876 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003878 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3881 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3882 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3883 (size_t)len) == 0)
3884 {
3885#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003886 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 *full = TRUE;
3888#endif
3889 break;
3890 }
3891
Bram Moolenaar204852a2022-03-05 12:56:44 +00003892 // :Print and :mode are not supported in Vim9 script.
3893 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003894 if (vim9 && eap->cmdidx != CMD_SIZE)
3895 {
3896 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3897 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003898 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003899 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003900 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003901 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003902 eap->cmdidx = CMD_SIZE;
3903 }
3904 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003905
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003906 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003907 // 'cpoptions'.
3908 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3909 p = eap->cmd;
3910
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003911 // Look for a user defined command as a last resort. Let ":Print" be
3912 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003913 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3914 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003916 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 while (ASCII_ISALNUM(*p))
3918 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003919 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003921 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 eap->cmdidx = CMD_SIZE;
3923 }
3924
Bram Moolenaar204852a2022-03-05 12:56:44 +00003925 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3926 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003927 eap->cmdidx = CMD_finally;
3928
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003929#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003930 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003931 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003932 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003933 && (eap->cmdidx < 0 ||
3934 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003935 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003936 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3937
3938 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003939 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003940 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003941 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003942#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 return p;
3945}
3946
3947#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003948static struct cmdmod
3949{
3950 char *name;
3951 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003952 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003953} cmdmods[] = {
3954 {"aboveleft", 3, FALSE},
3955 {"belowright", 3, FALSE},
3956 {"botright", 2, FALSE},
3957 {"browse", 3, FALSE},
3958 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003959 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003960 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003961 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003962 {"keepalt", 5, FALSE},
3963 {"keepjumps", 5, FALSE},
3964 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003965 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003966 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003967 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003968 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003969 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003970 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003971 {"rightbelow", 6, FALSE},
3972 {"sandbox", 3, FALSE},
3973 {"silent", 3, FALSE},
3974 {"tab", 3, TRUE},
3975 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003976 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003977 {"verbose", 4, TRUE},
3978 {"vertical", 4, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003979 {"vim9cmd", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003980};
3981
3982/*
3983 * Return length of a command modifier (including optional count).
3984 * Return zero when it's not a modifier.
3985 */
3986 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003987modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003988{
3989 int i, j;
3990 char_u *p = cmd;
3991
3992 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003993 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003994 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003995 {
3996 for (j = 0; p[j] != NUL; ++j)
3997 if (p[j] != cmdmods[i].name[j])
3998 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003999 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00004000 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00004001 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004002 }
4003 return 0;
4004}
4005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006/*
4007 * Return > 0 if an Ex command "name" exists.
4008 * Return 2 if there is an exact match.
4009 * Return 3 if there is an ambiguous match.
4010 */
4011 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004012cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
4014 exarg_T ea;
4015 int full = FALSE;
4016 int i;
4017 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004018 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004020 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004021 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
4023 for (j = 0; name[j] != NUL; ++j)
4024 if (name[j] != cmdmods[i].name[j])
4025 break;
4026 if (name[j] == NUL && j >= cmdmods[i].minlen)
4027 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4028 }
4029
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004030 // Check built-in commands and user defined commands.
4031 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004032 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004034 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004036 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004038 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4039 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004040 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004041 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4043}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004044
4045/*
4046 * "fullcommand" function
4047 */
4048 void
4049f_fullcommand(typval_T *argvars, typval_T *rettv)
4050{
4051 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004052 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004053 char_u *p;
4054
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004055 rettv->v_type = VAR_STRING;
4056 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004057
4058 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
4059 return;
4060
4061 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004062 if (name == NULL)
4063 return;
4064
zeertzjqc024ed92022-01-04 16:22:52 +00004065 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004066 name++;
4067 name = skip_range(name, TRUE, NULL);
4068
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004069 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4070 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004071 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004072 p = find_ex_command(&ea, NULL, NULL, NULL);
4073 if (p == NULL || ea.cmdidx == CMD_SIZE)
4074 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004075 if (in_vim9script())
4076 {
4077 int res;
4078
4079 ++emsg_silent;
4080 res = not_in_vim9(&ea);
4081 --emsg_silent;
4082
4083 if (res == FAIL)
4084 return;
4085 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004086
4087 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004088 ? get_user_command_name(ea.useridx, ea.cmdidx)
4089 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091#endif
4092
Bram Moolenaard0190392019-08-23 21:17:35 +02004093 cmdidx_T
4094excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095{
Bram Moolenaard0190392019-08-23 21:17:35 +02004096 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004098 if (!one_letter_cmd(cmd, &idx))
4099 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4100 idx = (cmdidx_T)((int)idx + 1))
4101 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4102 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
Bram Moolenaard0190392019-08-23 21:17:35 +02004104 return idx;
4105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
Bram Moolenaard0190392019-08-23 21:17:35 +02004107 long
4108excmd_get_argt(cmdidx_T idx)
4109{
4110 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111}
4112
4113/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004114 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 *
4116 * Backslashed delimiters after / or ? will be skipped, and commands will
4117 * not be expanded between /'s and ?'s or after "'".
4118 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004119 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 * Returns the "cmd" pointer advanced to beyond the range.
4121 */
4122 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004123skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004124 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004125 int skip_star, // skip "*" used for Visual range
4126 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004128 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004129 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004131 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004133 if (*cmd == '\\')
4134 {
4135 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4136 ++cmd;
4137 else
4138 break;
4139 }
4140 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004142 char_u *p = cmd;
4143
4144 // a quote is only valid at the start or after a separator
4145 while (p > cmd_start)
4146 {
4147 --p;
4148 if (!VIM_ISWHITE(*p))
4149 break;
4150 }
4151 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4152 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 if (*++cmd == NUL && ctx != NULL)
4154 *ctx = EXPAND_NOTHING;
4155 }
4156 else if (*cmd == '/' || *cmd == '?')
4157 {
4158 delim = *cmd++;
4159 while (*cmd != NUL && *cmd != delim)
4160 if (*cmd++ == '\\' && *cmd != NUL)
4161 ++cmd;
4162 if (*cmd == NUL && ctx != NULL)
4163 *ctx = EXPAND_NOTHING;
4164 }
4165 if (*cmd != NUL)
4166 ++cmd;
4167 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004169 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004170 while (*cmd == ':')
4171 cmd = skipwhite(cmd + 1);
4172
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004173 // Skip "*" used for Visual range.
4174 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4175 cmd = skipwhite(cmd + 1);
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return cmd;
4178}
4179
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004180 static void
4181addr_error(cmd_addr_T addr_type)
4182{
4183 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004184 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004185 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004186 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004187}
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004190 * Return the default address for an address type.
4191 */
4192 static linenr_T
4193default_address(exarg_T *eap)
4194{
4195 linenr_T lnum = 0;
4196
4197 switch (eap->addr_type)
4198 {
4199 case ADDR_LINES:
4200 case ADDR_OTHER:
4201 // Default is the cursor line number. Avoid using an invalid
4202 // line number though.
4203 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4204 lnum = curbuf->b_ml.ml_line_count;
4205 else
4206 lnum = curwin->w_cursor.lnum;
4207 break;
4208 case ADDR_WINDOWS:
4209 lnum = CURRENT_WIN_NR;
4210 break;
4211 case ADDR_ARGUMENTS:
4212 lnum = curwin->w_arg_idx + 1;
4213 if (lnum > ARGCOUNT)
4214 lnum = ARGCOUNT;
4215 break;
4216 case ADDR_LOADED_BUFFERS:
4217 case ADDR_BUFFERS:
4218 lnum = curbuf->b_fnum;
4219 break;
4220 case ADDR_TABS:
4221 lnum = CURRENT_TAB_NR;
4222 break;
4223 case ADDR_TABS_RELATIVE:
4224 case ADDR_UNSIGNED:
4225 lnum = 1;
4226 break;
4227 case ADDR_QUICKFIX:
4228#ifdef FEAT_QUICKFIX
4229 lnum = qf_get_cur_idx(eap);
4230#endif
4231 break;
4232 case ADDR_QUICKFIX_VALID:
4233#ifdef FEAT_QUICKFIX
4234 lnum = qf_get_cur_valid_idx(eap);
4235#endif
4236 break;
4237 case ADDR_NONE:
4238 // Will give an error later if a range is found.
4239 break;
4240 }
4241 return lnum;
4242}
4243
4244/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004245 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 *
4247 * Set ptr to the next character after the part that was interpreted.
4248 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004249 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 *
4251 * Return MAXLNUM when no Ex address was found.
4252 */
4253 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004254get_address(
4255 exarg_T *eap UNUSED,
4256 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004257 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004258 int skip, // only skip the address, don't use it
4259 int silent, // no errors or side effects
4260 int to_other_file, // flag: may jump to other file
4261 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262{
4263 int c;
4264 int i;
4265 long n;
4266 char_u *cmd;
4267 pos_T pos;
4268 pos_T *fp;
4269 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004270 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 cmd = skipwhite(*ptr);
4273 lnum = MAXLNUM;
4274 do
4275 {
4276 switch (*cmd)
4277 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004278 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004279 ++cmd;
4280 switch (addr_type)
4281 {
4282 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004283 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 lnum = curwin->w_cursor.lnum;
4285 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004286 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004287 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004288 break;
4289 case ADDR_ARGUMENTS:
4290 lnum = curwin->w_arg_idx + 1;
4291 break;
4292 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004293 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004294 lnum = curbuf->b_fnum;
4295 break;
4296 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004297 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004298 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004299 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004300 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004301 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004302 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004303 cmd = NULL;
4304 goto error;
4305 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004306 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004307#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004308 lnum = qf_get_cur_idx(eap);
4309#endif
4310 break;
4311 case ADDR_QUICKFIX_VALID:
4312#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004313 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004314#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004315 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004316 }
4317 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004319 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 ++cmd;
4321 switch (addr_type)
4322 {
4323 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004324 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 lnum = curbuf->b_ml.ml_line_count;
4326 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004327 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004328 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004329 break;
4330 case ADDR_ARGUMENTS:
4331 lnum = ARGCOUNT;
4332 break;
4333 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004334 buf = lastbuf;
4335 while (buf->b_ml.ml_mfp == NULL)
4336 {
4337 if (buf->b_prev == NULL)
4338 break;
4339 buf = buf->b_prev;
4340 }
4341 lnum = buf->b_fnum;
4342 break;
4343 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004344 lnum = lastbuf->b_fnum;
4345 break;
4346 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004347 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004348 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004349 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004350 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004351 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004352 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004353 cmd = NULL;
4354 goto error;
4355 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004356 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004357#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004358 lnum = qf_get_size(eap);
4359 if (lnum == 0)
4360 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004361#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004362 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004363 case ADDR_QUICKFIX_VALID:
4364#ifdef FEAT_QUICKFIX
4365 lnum = qf_get_valid_size(eap);
4366 if (lnum == 0)
4367 lnum = 1;
4368#endif
4369 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 }
4371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004373 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004374 if (*++cmd == NUL)
4375 {
4376 cmd = NULL;
4377 goto error;
4378 }
4379 if (addr_type != ADDR_LINES)
4380 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004381 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004382 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004383 goto error;
4384 }
4385 if (skip)
4386 ++cmd;
4387 else
4388 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004389 // Only accept a mark in another file when it is
4390 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4392 ++cmd;
4393 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004394 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 lnum = curwin->w_cursor.lnum;
4396 else
4397 {
4398 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 cmd = NULL;
4401 goto error;
4402 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004403 lnum = fp->lnum;
4404 }
4405 }
4406 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
4408 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004409 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004410 c = *cmd++;
4411 if (addr_type != ADDR_LINES)
4412 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004413 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004414 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 goto error;
4416 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004417 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004418 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004419 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 if (*cmd == c)
4421 ++cmd;
4422 }
4423 else
4424 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004425 int flags;
4426
4427 pos = curwin->w_cursor; // save curwin->w_cursor
4428
4429 // When '/' or '?' follows another address, start from
4430 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004431 if (lnum > 0 && lnum != MAXLNUM)
4432 curwin->w_cursor.lnum =
4433 lnum > curbuf->b_ml.ml_line_count
4434 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004435
4436 // Start a forward search at the end of the line (unless
4437 // before the first line).
4438 // Start a backward search at the start of the line.
4439 // This makes sure we never match in the current
4440 // line, and can match anywhere in the
4441 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004442 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004443 curwin->w_cursor.col = MAXCOL;
4444 else
4445 curwin->w_cursor.col = 0;
4446 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004447 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004448 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 {
4450 curwin->w_cursor = pos;
4451 cmd = NULL;
4452 goto error;
4453 }
4454 lnum = curwin->w_cursor.lnum;
4455 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004456 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 cmd += searchcmdlen;
4458 }
4459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004462 ++cmd;
4463 if (addr_type != ADDR_LINES)
4464 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004465 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004466 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 goto error;
4468 }
4469 if (*cmd == '&')
4470 i = RE_SUBST;
4471 else if (*cmd == '?' || *cmd == '/')
4472 i = RE_SEARCH;
4473 else
4474 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004475 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 cmd = NULL;
4477 goto error;
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 if (!skip)
4481 {
4482 /*
4483 * When search follows another address, start from
4484 * there.
4485 */
4486 if (lnum != MAXLNUM)
4487 pos.lnum = lnum;
4488 else
4489 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 /*
4492 * Start the search just like for the above
4493 * do_search().
4494 */
4495 if (*cmd != '?')
4496 pos.col = MAXCOL;
4497 else
4498 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004499 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004500 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004502 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 lnum = pos.lnum;
4504 else
4505 {
4506 cmd = NULL;
4507 goto error;
4508 }
4509 }
4510 ++cmd;
4511 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
4513 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004514 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004515 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517
4518 for (;;)
4519 {
4520 cmd = skipwhite(cmd);
4521 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4522 break;
4523
4524 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004525 {
4526 switch (addr_type)
4527 {
4528 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004529 case ADDR_OTHER:
4530 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004531 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 break;
4533 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004534 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004535 break;
4536 case ADDR_ARGUMENTS:
4537 lnum = curwin->w_arg_idx + 1;
4538 break;
4539 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004540 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004541 lnum = curbuf->b_fnum;
4542 break;
4543 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004544 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004546 case ADDR_TABS_RELATIVE:
4547 lnum = 1;
4548 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004549 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004550#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004551 lnum = qf_get_cur_idx(eap);
4552#endif
4553 break;
4554 case ADDR_QUICKFIX_VALID:
4555#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004556 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004557#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004558 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004559 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004560 case ADDR_UNSIGNED:
4561 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004562 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 }
4564 }
4565
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004567 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 else
4569 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004570 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 n = 1;
4572 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004575 if (n == MAXLNUM)
4576 {
4577 emsg(_(e_line_number_out_of_range));
4578 goto error;
4579 }
4580 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004581
4582 if (addr_type == ADDR_TABS_RELATIVE)
4583 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004584 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004585 cmd = NULL;
4586 goto error;
4587 }
4588 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004589 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004590 lnum = compute_buffer_local_count(
4591 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004593 {
4594#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004595 // Relative line addressing, need to adjust for folded lines
4596 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004597 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004598 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004599 (void)hasFolding(lnum, NULL, &lnum);
4600#endif
4601 if (i == '-')
4602 lnum -= n;
4603 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004604 {
4605 if (n >= LONG_MAX - lnum)
4606 {
4607 emsg(_(e_line_number_out_of_range));
4608 goto error;
4609 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004610 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004611 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
4614 } while (*cmd == '/' || *cmd == '?');
4615
4616error:
4617 *ptr = cmd;
4618 return lnum;
4619}
4620
4621/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004622 * Set eap->line1 and eap->line2 to the whole range.
4623 * Used for commands with the EX_DFLALL flag and no range given.
4624 */
4625 static void
4626address_default_all(exarg_T *eap)
4627{
4628 eap->line1 = 1;
4629 switch (eap->addr_type)
4630 {
4631 case ADDR_LINES:
4632 case ADDR_OTHER:
4633 eap->line2 = curbuf->b_ml.ml_line_count;
4634 break;
4635 case ADDR_LOADED_BUFFERS:
4636 {
4637 buf_T *buf = firstbuf;
4638
4639 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4640 buf = buf->b_next;
4641 eap->line1 = buf->b_fnum;
4642 buf = lastbuf;
4643 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4644 buf = buf->b_prev;
4645 eap->line2 = buf->b_fnum;
4646 }
4647 break;
4648 case ADDR_BUFFERS:
4649 eap->line1 = firstbuf->b_fnum;
4650 eap->line2 = lastbuf->b_fnum;
4651 break;
4652 case ADDR_WINDOWS:
4653 eap->line2 = LAST_WIN_NR;
4654 break;
4655 case ADDR_TABS:
4656 eap->line2 = LAST_TAB_NR;
4657 break;
4658 case ADDR_TABS_RELATIVE:
4659 eap->line2 = 1;
4660 break;
4661 case ADDR_ARGUMENTS:
4662 if (ARGCOUNT == 0)
4663 eap->line1 = eap->line2 = 0;
4664 else
4665 eap->line2 = ARGCOUNT;
4666 break;
4667 case ADDR_QUICKFIX_VALID:
4668#ifdef FEAT_QUICKFIX
4669 eap->line2 = qf_get_valid_size(eap);
4670 if (eap->line2 == 0)
4671 eap->line2 = 1;
4672#endif
4673 break;
4674 case ADDR_NONE:
4675 case ADDR_UNSIGNED:
4676 case ADDR_QUICKFIX:
4677 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4678 break;
4679 }
4680}
4681
4682
4683/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004684 * Get flags from an Ex command argument.
4685 */
4686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004687get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004688{
4689 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4690 {
4691 if (*eap->arg == 'l')
4692 eap->flags |= EXFLAG_LIST;
4693 else if (*eap->arg == 'p')
4694 eap->flags |= EXFLAG_PRINT;
4695 else
4696 eap->flags |= EXFLAG_NR;
4697 eap->arg = skipwhite(eap->arg + 1);
4698 }
4699}
4700
4701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 * Function called for command which is Not Implemented. NI!
4703 */
4704 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004705ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
4707 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004708 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004709 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710}
4711
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004712#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713/*
4714 * Function called for script command which is Not Implemented. NI!
4715 * Skips over ":perl <<EOF" constructs.
4716 */
4717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004718ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 if (!eap->skip)
4721 ex_ni(eap);
4722 else
4723 vim_free(script_get(eap, eap->arg));
4724}
4725#endif
4726
4727/*
4728 * Check range in Ex command for validity.
4729 * Return NULL when valid, error message when invalid.
4730 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004731 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004734 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004735
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 if ( eap->line1 < 0
4737 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004738 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004739 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004740
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004741 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004742 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004743 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004744 {
4745 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004746 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004747#ifdef FEAT_DIFF
4748 + (eap->cmdidx == CMD_diffget)
4749#endif
4750 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004751 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004752 break;
4753 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004754 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004755 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004756 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004757 break;
4758 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004759 // Only a boundary check, not whether the buffers actually
4760 // exist.
4761 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004762 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004763 break;
4764 case ADDR_LOADED_BUFFERS:
4765 buf = firstbuf;
4766 while (buf->b_ml.ml_mfp == NULL)
4767 {
4768 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004769 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004770 buf = buf->b_next;
4771 }
4772 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004773 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004774 buf = lastbuf;
4775 while (buf->b_ml.ml_mfp == NULL)
4776 {
4777 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004778 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004779 buf = buf->b_prev;
4780 }
4781 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004782 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004783 break;
4784 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004785 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004786 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004787 break;
4788 case ADDR_TABS:
4789 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004790 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004791 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004792 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004793 case ADDR_OTHER:
4794 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004795 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004796 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004797#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004798 // No error for value that is too big, will use the last entry.
4799 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004800 {
4801 if (eap->addr_count == 0)
4802 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004803 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004804 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004805#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004806 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004807 case ADDR_QUICKFIX_VALID:
4808#ifdef FEAT_QUICKFIX
4809 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4810 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004811 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004812#endif
4813 break;
4814 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004815 case ADDR_NONE:
4816 // Will give an error elsewhere.
4817 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004818 }
4819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 return NULL;
4821}
4822
4823/*
4824 * Correct the range for zero line number, if required.
4825 */
4826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004827correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004829 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
4831 if (eap->line1 == 0)
4832 eap->line1 = 1;
4833 if (eap->line2 == 0)
4834 eap->line2 = 1;
4835 }
4836}
4837
Bram Moolenaar748bf032005-02-02 23:04:36 +00004838#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004839/*
4840 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4841 * pattern. Otherwise return eap->arg.
4842 */
4843 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004844skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004845{
4846 char_u *p = eap->arg;
4847
Bram Moolenaara37420f2006-02-04 22:37:47 +00004848 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4849 || eap->cmdidx == CMD_vimgrepadd
4850 || eap->cmdidx == CMD_lvimgrepadd
4851 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004852 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004853 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004854 if (p == NULL)
4855 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004856 }
4857 return p;
4858}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004859
4860/*
4861 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4862 * in the command line, so that things like % get expanded.
4863 */
4864 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004865replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004866{
4867 char_u *new_cmdline;
4868 char_u *program;
4869 char_u *pos;
4870 char_u *ptr;
4871 int len;
4872 int i;
4873
4874 /*
4875 * Don't do it when ":vimgrep" is used for ":grep".
4876 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004877 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4878 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4879 || eap->cmdidx == CMD_grepadd
4880 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004881 && !grep_internal(eap->cmdidx))
4882 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004883 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4884 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004885 {
4886 if (*curbuf->b_p_gp == NUL)
4887 program = p_gp;
4888 else
4889 program = curbuf->b_p_gp;
4890 }
4891 else
4892 {
4893 if (*curbuf->b_p_mp == NUL)
4894 program = p_mp;
4895 else
4896 program = curbuf->b_p_mp;
4897 }
4898
4899 p = skipwhite(p);
4900
4901 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4902 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004903 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004904 i = 1;
4905 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4906 ++i;
4907 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004908 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004909 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004910 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004911 ptr = new_cmdline;
4912 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4913 {
4914 i = (int)(pos - program);
4915 STRNCPY(ptr, program, i);
4916 STRCPY(ptr += i, p);
4917 ptr += len;
4918 program = pos + 2;
4919 }
4920 STRCPY(ptr, program);
4921 }
4922 else
4923 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004924 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004925 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004926 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004927 STRCPY(new_cmdline, program);
4928 STRCAT(new_cmdline, " ");
4929 STRCAT(new_cmdline, p);
4930 }
4931 msg_make(p);
4932
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004933 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004934 vim_free(*cmdlinep);
4935 *cmdlinep = new_cmdline;
4936 p = new_cmdline;
4937 }
4938 return p;
4939}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004940#endif
4941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942/*
4943 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004944 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 * Return FAIL for failure, OK otherwise.
4946 */
4947 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004948expand_filename(
4949 exarg_T *eap,
4950 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004951 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004953 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 char_u *repl;
4955 int srclen;
4956 char_u *p;
4957 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004958 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
Bram Moolenaar748bf032005-02-02 23:04:36 +00004960#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004961 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004962 p = skip_grep_pat(eap);
4963#else
4964 p = eap->arg;
4965#endif
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 /*
4968 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4969 * the file name contains a wildcard it should not cause expanding.
4970 * (it will be expanded anyway if there is a wildcard before replacing).
4971 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004972 has_wildcards = mch_has_wildcard(p);
4973 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004975#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004976 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004977 if (p[0] == '`' && p[1] == '=')
4978 {
4979 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004980 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004981 if (*p == '`')
4982 ++p;
4983 continue;
4984 }
4985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 /*
4987 * Quick check if this cannot be the start of a special string.
4988 * Also removes backslash before '%', '#' and '<'.
4989 */
4990 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4991 {
4992 ++p;
4993 continue;
4994 }
4995
4996 /*
4997 * Try to find a match at this position.
4998 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004999 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005000 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005001 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005003 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 p += srclen;
5006 continue;
5007 }
5008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005009 // Wildcards won't be expanded below, the replacement is taken
5010 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005011 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5012 {
5013 char_u *l = repl;
5014
5015 repl = expand_env_save(repl);
5016 vim_free(l);
5017 }
5018
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005019 // Need to escape white space et al. with a backslash.
5020 // Don't do this for:
5021 // - replacement that already has been escaped: "##"
5022 // - shell commands (may have to use quotes instead).
5023 // - non-unix systems when there is a single argument (spaces don't
5024 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005026 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 && eap->cmdidx != CMD_grep
5029 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005030 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005031 && eap->cmdidx != CMD_lgrep
5032 && eap->cmdidx != CMD_lgrepadd
5033 && eap->cmdidx != CMD_lmake
5034 && eap->cmdidx != CMD_make
5035 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005037 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#endif
5039 )
5040 {
5041 char_u *l;
5042#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005043 // Don't escape a backslash here, because rem_backslash() doesn't
5044 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 static char_u *nobslash = (char_u *)" \t\"|";
5046# define ESCAPE_CHARS nobslash
5047#else
5048# define ESCAPE_CHARS escape_chars
5049#endif
5050
5051 for (l = repl; *l; ++l)
5052 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5053 {
5054 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5055 if (l != NULL)
5056 {
5057 vim_free(repl);
5058 repl = l;
5059 }
5060 break;
5061 }
5062 }
5063
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005064 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005065 if ((eap->usefilter || eap->cmdidx == CMD_bang
5066 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005067 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
5069 char_u *l;
5070
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005071 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 if (l != NULL)
5073 {
5074 vim_free(repl);
5075 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 }
5078
5079 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5080 vim_free(repl);
5081 if (p == NULL)
5082 return FAIL;
5083 }
5084
5085 /*
5086 * One file argument: Expand wildcards.
5087 * Don't do this with ":r !command" or ":w !command".
5088 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005089 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 /*
5092 * May do this twice:
5093 * 1. Replace environment variables.
5094 * 2. Replace any other wildcards, remove backslashes.
5095 */
5096 for (n = 1; n <= 2; ++n)
5097 {
5098 if (n == 2)
5099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 /*
5101 * Halve the number of backslashes (this is Vi compatible).
5102 * For Unix and OS/2, when wildcards are expanded, this is
5103 * done by ExpandOne() below.
5104 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005105#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (!has_wildcards)
5107#endif
5108 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110
5111 if (has_wildcards)
5112 {
5113 if (n == 1)
5114 {
5115 /*
5116 * First loop: May expand environment variables. This
5117 * can be done much faster with expand_env() than with
5118 * something else (e.g., calling a shell).
5119 * After expanding environment variables, check again
5120 * if there are still wildcards present.
5121 */
5122 if (vim_strchr(eap->arg, '$') != NULL
5123 || vim_strchr(eap->arg, '~') != NULL)
5124 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005125 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005126 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 has_wildcards = mch_has_wildcard(NameBuff);
5128 p = NameBuff;
5129 }
5130 else
5131 p = NULL;
5132 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005133 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
5135 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005136 int options = WILD_LIST_NOTFOUND
5137 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 ExpandInit(&xpc);
5140 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005141 if (p_wic)
5142 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005144 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (p == NULL)
5146 return FAIL;
5147 }
5148 if (p != NULL)
5149 {
5150 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5151 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005152 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 vim_free(p);
5154 }
5155 }
5156 }
5157 }
5158 return OK;
5159}
5160
5161/*
5162 * Replace part of the command line, keeping eap->cmd, eap->arg and
5163 * eap->nextcmd correct.
5164 * "src" points to the part that is to be replaced, of length "srclen".
5165 * "repl" is the replacement string.
5166 * Returns a pointer to the character after the replaced string.
5167 * Returns NULL for failure.
5168 */
5169 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005170repl_cmdline(
5171 exarg_T *eap,
5172 char_u *src,
5173 int srclen,
5174 char_u *repl,
5175 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176{
5177 int len;
5178 int i;
5179 char_u *new_cmdline;
5180
5181 /*
5182 * The new command line is build in new_cmdline[].
5183 * First allocate it.
5184 * Careful: a "+cmd" argument may have been NUL terminated.
5185 */
5186 len = (int)STRLEN(repl);
5187 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005188 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005189 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005190 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005191 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
5193 /*
5194 * Copy the stuff before the expanded part.
5195 * Copy the expanded stuff.
5196 * Copy what came after the expanded part.
5197 * Copy the next commands, if there are any.
5198 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005199 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005203 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005205 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005207 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
5209 i = (int)STRLEN(new_cmdline) + 1;
5210 STRCPY(new_cmdline + i, eap->nextcmd);
5211 eap->nextcmd = new_cmdline + i;
5212 }
5213 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5214 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5215 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5216 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5217 vim_free(*cmdlinep);
5218 *cmdlinep = new_cmdline;
5219
5220 return src;
5221}
5222
5223/*
5224 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005225 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 */
5227 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005228separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
5230 char_u *p;
5231
Bram Moolenaar86b68352004-12-27 21:59:20 +00005232#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005233 p = skip_grep_pat(eap);
5234#else
5235 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005236#endif
5237
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005238 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
5240 if (*p == Ctrl_V)
5241 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005242 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005243 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005245 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005246 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005247 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 break;
5249 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005250
5251#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005252 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005253 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005254 {
5255 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005256 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005257 if (*p == NUL) // stop at NUL after CTRL-V
5258 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005259 }
5260#endif
5261
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005262 // Check for '"': start of comment or '|': next command
5263 // :@" and :*" do not start a comment!
5264 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005265 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005266#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005267 && !in_vim9script()
5268#endif
5269 && !(eap->argt & EX_NOTRLCOM)
5270 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5271 || p != eap->arg)
5272 && (eap->cmdidx != CMD_redir
5273 || p != eap->arg + 1 || p[-1] != '@'))
5274#ifdef FEAT_EVAL
5275 || (*p == '#'
5276 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005277 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005278 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 || *p == '|' || *p == '\n')
5281 {
5282 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005283 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 * AND 'b' is present in 'cpoptions'.
5285 */
5286 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005287 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005289 if (!keep_backslash)
5290 {
5291 STRMOVE(p - 1, p); // remove the '\'
5292 --p;
5293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
5295 else
5296 {
5297 eap->nextcmd = check_nextcmd(p);
5298 *p = NUL;
5299 break;
5300 }
5301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005304 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 del_trailing_spaces(eap->arg);
5306}
5307
5308/*
5309 * get + command from ex argument
5310 */
5311 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005312getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
5314 char_u *arg = *argp;
5315 char_u *command = NULL;
5316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005317 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005320 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 command = dollar_command;
5322 else
5323 {
5324 command = arg;
5325 arg = skip_cmd_arg(command, TRUE);
5326 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005327 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005330 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 *argp = arg;
5332 }
5333 return command;
5334}
5335
5336/*
5337 * Find end of "+command" argument. Skip over "\ " and "\\".
5338 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005339 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005340skip_cmd_arg(
5341 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005342 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
5344 while (*p && !vim_isspace(*p))
5345 {
5346 if (*p == '\\' && p[1] != NUL)
5347 {
5348 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005349 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 else
5351 ++p;
5352 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005353 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355 return p;
5356}
5357
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005358 int
5359get_bad_opt(char_u *p, exarg_T *eap)
5360{
5361 if (STRICMP(p, "keep") == 0)
5362 eap->bad_char = BAD_KEEP;
5363 else if (STRICMP(p, "drop") == 0)
5364 eap->bad_char = BAD_DROP;
5365 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5366 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005367 else
5368 return FAIL;
5369 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005370}
5371
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372/*
5373 * Get "++opt=arg" argument.
5374 * Return FAIL or OK.
5375 */
5376 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005377getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 char_u *arg = eap->arg + 2;
5380 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005381 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005384 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5386 {
5387 if (*arg == 'n')
5388 {
5389 arg += 2;
5390 eap->force_bin = FORCE_NOBIN;
5391 }
5392 else
5393 eap->force_bin = FORCE_BIN;
5394 if (!checkforcmd(&arg, "binary", 3))
5395 return FAIL;
5396 eap->arg = skipwhite(arg);
5397 return OK;
5398 }
5399
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005400 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005401 if (STRNCMP(arg, "edit", 4) == 0)
5402 {
5403 eap->read_edit = TRUE;
5404 eap->arg = skipwhite(arg + 4);
5405 return OK;
5406 }
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 if (STRNCMP(arg, "ff", 2) == 0)
5409 {
5410 arg += 2;
5411 pp = &eap->force_ff;
5412 }
5413 else if (STRNCMP(arg, "fileformat", 10) == 0)
5414 {
5415 arg += 10;
5416 pp = &eap->force_ff;
5417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 else if (STRNCMP(arg, "enc", 3) == 0)
5419 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005420 if (STRNCMP(arg, "encoding", 8) == 0)
5421 arg += 8;
5422 else
5423 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 pp = &eap->force_enc;
5425 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005426 else if (STRNCMP(arg, "bad", 3) == 0)
5427 {
5428 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005429 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
5432 if (pp == NULL || *arg != '=')
5433 return FAIL;
5434
5435 ++arg;
5436 *pp = (int)(arg - eap->cmd);
5437 arg = skip_cmd_arg(arg, FALSE);
5438 eap->arg = skipwhite(arg);
5439 *arg = NUL;
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (pp == &eap->force_ff)
5442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5444 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005445 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005447 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005449 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5451 *p = TOLOWER_ASC(*p);
5452 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005453 else
5454 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005455 // Check ++bad= argument. Must be a single-byte character, "keep" or
5456 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005457 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005458 return FAIL;
5459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
5461 return OK;
5462}
5463
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005465ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466{
5467 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005468 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 * directory for security reasons.
5470 */
5471 if (secure)
5472 {
5473 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005474 eap->errmsg =
5475 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 }
5477 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005478 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 else
5480 do_augroup(eap->arg, eap->forceit);
5481}
5482
5483/*
5484 * ":doautocmd": Apply the automatic commands to the current buffer.
5485 */
5486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005487ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005489 char_u *arg = eap->arg;
5490 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005491 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005492
Bram Moolenaar1610d052016-06-09 22:53:01 +02005493 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005494 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005495 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005496 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499/*
5500 * :[N]bunload[!] [N] [bufname] unload buffer
5501 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5502 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5503 */
5504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005507 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005508 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 eap->errmsg = do_bufdel(
5510 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5511 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5512 : DOBUF_UNLOAD, eap->arg,
5513 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5514}
5515
5516/*
5517 * :[N]buffer [N] to buffer N
5518 * :[N]sbuffer [N] to buffer N
5519 */
5520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005521ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005523 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005524 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005526 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 else
5528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005529 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5531 else
5532 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005533 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005534 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 }
5536}
5537
5538/*
5539 * :[N]bmodified [N] to next mod. buffer
5540 * :[N]sbmodified [N] to next mod. buffer
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005546 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005547 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548}
5549
5550/*
5551 * :[N]bnext [N] to next buffer
5552 * :[N]sbnext [N] split and to next buffer
5553 */
5554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005557 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005558 return;
5559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005561 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005562 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563}
5564
5565/*
5566 * :[N]bNext [N] to previous buffer
5567 * :[N]bprevious [N] to previous buffer
5568 * :[N]sbNext [N] split and to previous buffer
5569 * :[N]sbprevious [N] split and to previous buffer
5570 */
5571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005572ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005574 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005575 return;
5576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005578 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005579 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580}
5581
5582/*
5583 * :brewind to first buffer
5584 * :bfirst to first buffer
5585 * :sbrewind split and to first buffer
5586 * :sbfirst split and to first buffer
5587 */
5588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005591 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005592 return;
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005595 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005596 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597}
5598
5599/*
5600 * :blast to last buffer
5601 * :sblast split and to last buffer
5602 */
5603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005604ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005606 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005607 return;
5608
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005610 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005611 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaar7a092242020-04-16 22:10:49 +02005614/*
5615 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005616 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005619ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005621 int comment_char = '"';
5622
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005623#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005624 if (in_vim9script())
5625 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005626#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005627 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005628}
5629
5630/*
5631 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5632 * to "cmd_start" or has a white space character before it.
5633 */
5634 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005635ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005636{
5637 int c = *cmd;
5638
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005639 if (c == NUL || c == '|' || c == '\n')
5640 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005641#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005642 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005643 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5644 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005645 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005646#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005647 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648}
5649
5650#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5651 || defined(PROTO)
5652/*
5653 * Return the next command, after the first '|' or '\n'.
5654 * Return NULL if not found.
5655 */
5656 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005657find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
5659 while (*p != '|' && *p != '\n')
5660 {
5661 if (*p == NUL)
5662 return NULL;
5663 ++p;
5664 }
5665 return (p + 1);
5666}
5667#endif
5668
5669/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005670 * Check if *p is a separator between Ex commands, skipping over white space.
5671 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 */
5673 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005674check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005676 char_u *s = skipwhite(p);
5677
5678 if (*s == '|' || *s == '\n')
5679 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 else
5681 return NULL;
5682}
5683
5684/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005685 * If "eap->nextcmd" is not set, check for a next command at "p".
5686 */
5687 void
5688set_nextcmd(exarg_T *eap, char_u *arg)
5689{
5690 char_u *p = check_nextcmd(arg);
5691
5692 if (eap->nextcmd == NULL)
5693 eap->nextcmd = p;
5694 else if (p != NULL)
5695 // cannot use "| command" inside a {} block
5696 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5697}
5698
5699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 * - if there are more files to edit
5701 * - and this is the last window
5702 * - and forceit not used
5703 * - and not repeated twice on a row
5704 * return FAIL and give error message if 'message' TRUE
5705 * return OK otherwise
5706 */
5707 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005708check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005709 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005710 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5713
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005714 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005715 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717 if (message)
5718 {
5719#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005720 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5721 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005723 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005725 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5726 NGETTEXT("%d more file to edit. Quit anyway?",
5727 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5729 return OK;
5730 return FAIL;
5731 }
5732#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005733 semsg(NGETTEXT(e_nr_more_file_to_edit,
5734 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005735 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737 return FAIL;
5738 }
5739 return OK;
5740}
5741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742/*
5743 * Function given to ExpandGeneric() to obtain the list of command names.
5744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005746get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005749 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 return cmdnames[idx].cmd_name;
5751}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005754ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
Bram Moolenaare6850792010-05-14 15:28:44 +02005756 if (*eap->arg == NUL)
5757 {
5758#ifdef FEAT_EVAL
5759 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5760 char_u *p = NULL;
5761
5762 if (expr != NULL)
5763 {
5764 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005765 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005766 --emsg_off;
5767 vim_free(expr);
5768 }
5769 if (p != NULL)
5770 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005771 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005772 vim_free(p);
5773 }
5774 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005775 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005776#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005777 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005778#endif
5779 }
5780 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005781 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005782
5783#ifdef FEAT_VTP
5784 else if (has_vtp_working())
5785 {
5786 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005787 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005788 redrawcmd();
5789 }
5790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791}
5792
5793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005794ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005797 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 do_highlight(eap->arg, eap->forceit, FALSE);
5799}
5800
5801
5802/*
5803 * Call this function if we thought we were going to exit, but we won't
5804 * (because of an error). May need to restore the terminal mode.
5805 */
5806 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005807not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
5809 exiting = FALSE;
5810 settmode(TMODE_RAW);
5811}
5812
Bram Moolenaar3552e742021-05-29 12:21:58 +02005813 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005814before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5815{
5816 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5817
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005818 // Bail out when autocommands closed the window.
5819 // Refuse to quit when the buffer in the last window is being closed (can
5820 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005821 if (!win_valid(wp)
5822 || curbuf_locked()
5823 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5824 return TRUE;
5825
5826 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5827 {
5828 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005829 // Refuse to quit when locked or when the window was closed or the
5830 // buffer in the last window is being closed (can only happen in
5831 // autocommands).
5832 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005833 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5834 return TRUE;
5835 }
5836
5837 return FALSE;
5838}
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005841 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005842 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005843 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005845 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005846ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005848 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005849
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850#ifdef FEAT_CMDWIN
5851 if (cmdwin_type != 0)
5852 {
5853 cmdwin_result = Ctrl_C;
5854 return;
5855 }
5856#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005857 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005858 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005859 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005860 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005861 return;
5862 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005863 if (eap->addr_count > 0)
5864 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005865 int wnr = eap->line2;
5866
5867 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5868 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005869 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005870 }
5871 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005872 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005874 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005875 if (curbuf_locked())
5876 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005877
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005878 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005879 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005880 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
5882#ifdef FEAT_NETBEANS_INTG
5883 netbeansForcedQuit = eap->forceit;
5884#endif
5885
5886 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005887 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 */
5889 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5890 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005891 if ((!buf_hide(wp->w_buffer)
5892 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005893 | (eap->forceit ? CCGD_FORCEIT : 0)
5894 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005896 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 {
5898 not_exiting();
5899 }
5900 else
5901 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005902 // quit last window
5903 // Note: only_one_window() returns true, even so a help window is
5904 // still open. In that case only quit, if no address has been
5905 // specified. Example:
5906 // :h|wincmd w|1q - don't quit
5907 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005908 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005910 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005911#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005913#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005914 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005915 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917}
5918
5919/*
5920 * ":cquit".
5921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005923ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005925 // this does not always pass on the exit code to the Manx compiler. why?
5926 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927}
5928
5929/*
5930 * ":qall": try to quit all windows
5931 */
5932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005933ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935# ifdef FEAT_CMDWIN
5936 if (cmdwin_type != 0)
5937 {
5938 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005939 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 else
5941 cmdwin_result = K_XF2;
5942 return;
5943 }
5944# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005945
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005946 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005947 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005948 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005949 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005950 return;
5951 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005952
5953 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005954 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005957 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 getout(0);
5959 not_exiting();
5960}
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962/*
5963 * ":close": close current window, unless it is the last one
5964 */
5965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005966ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005968 win_T *win;
5969 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005970#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005972 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005974#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005975 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005976 {
5977 if (eap->addr_count == 0)
5978 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005979 else
5980 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005981 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005982 {
5983 winnr++;
5984 if (winnr == eap->line2)
5985 break;
5986 }
5987 if (win == NULL)
5988 win = lastwin;
5989 ex_win_close(eap->forceit, win, NULL);
5990 }
5991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992}
5993
Bram Moolenaar4033c552017-09-16 20:54:51 +02005994#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005995/*
5996 * ":pclose": Close any preview window.
5997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005999ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006000{
6001 win_T *win;
6002
Bram Moolenaar79648732019-07-18 21:43:07 +02006003 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006004 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006005 if (win->w_p_pvw)
6006 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006007 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006008 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006009 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006010# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006011 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006012 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006013# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006014}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006015#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006016
Bram Moolenaarf740b292006-02-16 22:11:02 +00006017/*
6018 * Close window "win" and take care of handling closing the last window for a
6019 * modified buffer.
6020 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006022ex_win_close(
6023 int forceit,
6024 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006025 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 int need_hide;
6028 buf_T *buf = win->w_buffer;
6029
Bram Moolenaarcf844172020-06-26 19:44:06 +02006030 // Never close the autocommand window.
6031 if (win == aucmd_win)
6032 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006033 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006034 return;
6035 }
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006038 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006040#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006041 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006043 bufref_T bufref;
6044
6045 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006047 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 return;
6049 need_hide = FALSE;
6050 }
6051 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006054 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 return;
6056 }
6057 }
6058
Bram Moolenaar4033c552017-09-16 20:54:51 +02006059#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006061#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006063 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006064 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006065 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006066 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006067 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068}
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006071 * Handle the argument for a tabpage related ex command.
6072 * Returns a tabpage number.
6073 * When an error is encountered then eap->errmsg is set.
6074 */
6075 static int
6076get_tabpage_arg(exarg_T *eap)
6077{
6078 int tab_number;
6079 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6080
6081 if (eap->arg && *eap->arg != NUL)
6082 {
6083 char_u *p = eap->arg;
6084 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006085 int relative = 0; // argument +N/-N means: go to N places to the
6086 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006087
6088 if (*p == '-')
6089 {
6090 relative = -1;
6091 p++;
6092 }
6093 else if (*p == '+')
6094 {
6095 relative = 1;
6096 p++;
6097 }
6098
6099 p_save = p;
6100 tab_number = getdigits(&p);
6101
6102 if (relative == 0)
6103 {
6104 if (STRCMP(p, "$") == 0)
6105 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006106 else if (STRCMP(p, "#") == 0)
6107 if (valid_tabpage(lastused_tabpage))
6108 tab_number = tabpage_index(lastused_tabpage);
6109 else
6110 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006111 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006112 tab_number = 0;
6113 goto theend;
6114 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006115 else if (p == p_save || *p_save == '-' || *p != NUL
6116 || tab_number > LAST_TAB_NR)
6117 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006118 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006119 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006120 goto theend;
6121 }
6122 }
6123 else
6124 {
6125 if (*p_save == NUL)
6126 tab_number = 1;
6127 else if (p == p_save || *p_save == '-' || *p != NUL
6128 || tab_number == 0)
6129 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006130 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006131 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006132 goto theend;
6133 }
6134 tab_number = tab_number * relative + tabpage_index(curtab);
6135 if (!unaccept_arg0 && relative == -1)
6136 --tab_number;
6137 }
6138 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006139 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006140 }
6141 else if (eap->addr_count > 0)
6142 {
6143 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006144 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006145 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006146 tab_number = 0;
6147 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006148 else
6149 {
6150 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006151 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006152 {
6153 --tab_number;
6154 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006155 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006156 }
6157 }
6158 }
6159 else
6160 {
6161 switch (eap->cmdidx)
6162 {
6163 case CMD_tabnext:
6164 tab_number = tabpage_index(curtab) + 1;
6165 if (tab_number > LAST_TAB_NR)
6166 tab_number = 1;
6167 break;
6168 case CMD_tabmove:
6169 tab_number = LAST_TAB_NR;
6170 break;
6171 default:
6172 tab_number = tabpage_index(curtab);
6173 }
6174 }
6175
6176theend:
6177 return tab_number;
6178}
6179
6180/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006181 * ":tabclose": close current tab page, unless it is the last one.
6182 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 */
6184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006185ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006187 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006188 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006189
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006190# ifdef FEAT_CMDWIN
6191 if (cmdwin_type != 0)
6192 cmdwin_result = K_IGNORE;
6193 else
6194# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006195 if (first_tabpage->tp_next == NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00006196 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006197 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006199 tab_number = get_tabpage_arg(eap);
6200 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006201 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006202 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006203 if (tp == NULL)
6204 {
6205 beep_flush();
6206 return;
6207 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006208 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006209 {
6210 tabpage_close_other(tp, eap->forceit);
6211 return;
6212 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006213 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006214 tabpage_close(eap->forceit);
6215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006217}
6218
6219/*
6220 * ":tabonly": close all tab pages except the current one
6221 */
6222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006223ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006224{
6225 tabpage_T *tp;
6226 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006227 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006228
6229# ifdef FEAT_CMDWIN
6230 if (cmdwin_type != 0)
6231 cmdwin_result = K_IGNORE;
6232 else
6233# endif
6234 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006235 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006236 else
6237 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006238 tab_number = get_tabpage_arg(eap);
6239 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006240 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006241 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006242 // Repeat this up to a 1000 times, because autocommands may
6243 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006244 for (done = 0; done < 1000; ++done)
6245 {
6246 FOR_ALL_TABPAGES(tp)
6247 if (tp->tp_topframe != topframe)
6248 {
6249 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006250 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006251 if (valid_tabpage(tp))
6252 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006253 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006254 break;
6255 }
6256 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006257 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006258 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006259 }
6260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
6263/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006264 * Close the current tab page.
6265 */
6266 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006267tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006268{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006269 // First close all the windows but the current one. If that worked then
6270 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006271 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006272 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006273 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006274 ex_win_close(forceit, curwin, NULL);
6275# ifdef FEAT_GUI
6276 need_mouse_correct = TRUE;
6277# endif
6278}
6279
6280/*
6281 * Close tab page "tp", which is not the current tab page.
6282 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006283 * Also takes care of the tab pages line disappearing when closing the
6284 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006285 */
6286 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006287tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006288{
6289 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006290 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006291 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006292
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006293 // Limit to 1000 windows, autocommands may add a window while we close
6294 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006295 while (++done < 1000)
6296 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006297 wp = tp->tp_firstwin;
6298 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006299
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006300 // Autocommands may delete the tab page under our fingers and we may
6301 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006302 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006303 break;
6304 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006305
Bram Moolenaar29323592016-07-24 22:04:11 +02006306 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006307
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006308 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006309 if (h != tabline_height())
6310 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006311}
6312
6313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 * ":only".
6315 */
6316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006317ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006319 win_T *wp;
6320 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321# ifdef FEAT_GUI
6322 need_mouse_correct = TRUE;
6323# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006324 if (eap->addr_count > 0)
6325 {
6326 wnr = eap->line2;
6327 for (wp = firstwin; --wnr > 0; )
6328 {
6329 if (wp->w_next == NULL)
6330 break;
6331 else
6332 wp = wp->w_next;
6333 }
6334 win_goto(wp);
6335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 close_others(TRUE, eap->forceit);
6337}
6338
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006340ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006342 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006343 if (!eap->skip)
6344 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006345#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006346 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006347#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006348 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006349 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006350 else
6351 {
6352 int winnr = 0;
6353 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006354
Bram Moolenaar2256c992016-11-15 21:17:07 +01006355 FOR_ALL_WINDOWS(win)
6356 {
6357 winnr++;
6358 if (winnr == eap->line2)
6359 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006360 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006361 if (win == NULL)
6362 win = lastwin;
6363 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 }
6366}
6367
6368/*
6369 * ":stop" and ":suspend": Suspend Vim.
6370 */
dbivolaruab16ad32021-12-29 19:41:47 +00006371 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006372ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
6374 /*
6375 * Disallow suspending for "rvim".
6376 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006377 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 {
6379 if (!eap->forceit)
6380 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006381 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 windgoto((int)Rows - 1, 0);
6383 out_char('\n');
6384 out_flush();
6385 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006386 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006387 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006388 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006390 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006392 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006394 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006395 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 }
6397}
6398
6399/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006400 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 */
6402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006403ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006405#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006406 if (not_in_vim9(eap) == FAIL)
6407 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409#ifdef FEAT_CMDWIN
6410 if (cmdwin_type != 0)
6411 {
6412 cmdwin_result = Ctrl_C;
6413 return;
6414 }
6415#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006416 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006417 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006418 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006419 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006420 return;
6421 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006422
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006424 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 */
6426 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6427 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006428
6429 // Write the buffer for ":wq" or when it was changed.
6430 // Trigger QuitPre and ExitPre.
6431 // Check if we can exit now, after autocommands have changed things.
6432 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6433 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006435 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
6437 not_exiting();
6438 }
6439 else
6440 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006441 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006443 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444# ifdef FEAT_GUI
6445 need_mouse_correct = TRUE;
6446# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006447 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006448 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 }
6450}
6451
6452/*
6453 * ":print", ":list", ":number".
6454 */
6455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006456ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006458 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006459 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006462 for ( ;!got_int; ui_breakcheck())
6463 {
6464 print_line(eap->line1,
6465 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6466 || (eap->flags & EXFLAG_NR)),
6467 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6468 if (++eap->line1 > eap->line2)
6469 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006470 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006471 }
6472 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006473 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006474 curwin->w_cursor.lnum = eap->line2;
6475 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 }
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479}
6480
6481#ifdef FEAT_BYTEOFF
6482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006483ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
6485 goto_byte(eap->line2);
6486}
6487#endif
6488
6489/*
6490 * ":shell".
6491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006493ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 do_shell(NULL, 0);
6496}
6497
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006498#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006500static int drop_busy = FALSE;
6501static int drop_filec;
6502static char_u **drop_filev = NULL;
6503static int drop_split;
6504static void (*drop_callback)(void *);
6505static void *drop_cookie;
6506
6507 static void
6508handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 exarg_T ea;
6511 int save_msg_scroll = msg_scroll;
6512
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006513 // Setting the argument list may cause screen updates and being called
6514 // recursively. Avoid that by setting drop_busy.
6515 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006517 // Check whether the current buffer is changed. If so, we will need
6518 // to split the current window or data could be lost.
6519 // We don't need to check if the 'hidden' option is set, as in this
6520 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006521 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 {
6523 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006524 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 --emsg_off;
6526 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006527 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (win_split(0, 0) == FAIL)
6530 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006531 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006533 // When splitting the window, create a new alist. Otherwise the
6534 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 alist_unlink(curwin->w_alist);
6536 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 }
6538
6539 /*
6540 * Set up the new argument list.
6541 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006542 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
6544 /*
6545 * Move to the first file.
6546 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006547 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006548 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 ea.cmd = (char_u *)"next";
6550 do_argfile(&ea, 0);
6551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006552 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6553 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 need_start_insertmode = FALSE;
6555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006556 // Restore msg_scroll, otherwise a following command may cause scrolling
6557 // unexpectedly. The screen will be redrawn by the caller, thus
6558 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006560
6561 if (drop_callback != NULL)
6562 drop_callback(drop_cookie);
6563
6564 drop_filev = NULL;
6565 drop_busy = FALSE;
6566}
6567
6568/*
6569 * Handle a file drop. The code is here because a drop is *nearly* like an
6570 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006571 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006572 * code is very similar to :args and hence needs access to a lot of the static
6573 * functions in this file.
6574 *
6575 * The "filev" list must have been allocated using alloc(), as should each item
6576 * in the list. This function takes over responsibility for freeing the "filev"
6577 * list.
6578 */
6579 void
6580handle_drop(
6581 int filec, // the number of files dropped
6582 char_u **filev, // the list of files dropped
6583 int split, // force splitting the window
6584 void (*callback)(void *), // to be called after setting the argument
6585 // list
6586 void *cookie) // argument for "callback" (allocated)
6587{
6588 // Cannot handle recursive drops, finish the pending one.
6589 if (drop_busy)
6590 {
6591 FreeWild(filec, filev);
6592 vim_free(cookie);
6593 return;
6594 }
6595
6596 // When calling handle_drop() more than once in a row we only use the last
6597 // one.
6598 if (drop_filev != NULL)
6599 {
6600 FreeWild(drop_filec, drop_filev);
6601 vim_free(drop_cookie);
6602 }
6603
6604 drop_filec = filec;
6605 drop_filev = filev;
6606 drop_split = split;
6607 drop_callback = callback;
6608 drop_cookie = cookie;
6609
6610 // Postpone this when:
6611 // - editing the command line
6612 // - not possible to change the current buffer
6613 // - updating the screen
6614 // As it may change buffers and window structures that are in use and cause
6615 // freed memory to be used.
6616 if (text_locked() || curbuf_locked() || updating_screen)
6617 return;
6618
6619 handle_drop_internal();
6620}
6621
6622/*
6623 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6624 * reset: Handle a postponed drop.
6625 */
6626 void
6627handle_any_postponed_drop(void)
6628{
6629 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006630 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006631 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632}
6633#endif
6634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 * ":preserve".
6637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006639ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006641 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 ml_preserve(curbuf, TRUE);
6643}
6644
6645/*
6646 * ":recover".
6647 */
6648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006649ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006651 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006653 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6654 | CCGD_MULTWIN
6655 | (eap->forceit ? CCGD_FORCEIT : 0)
6656 | CCGD_EXCMD)
6657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 && (*eap->arg == NUL
6659 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006660 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 recoverymode = FALSE;
6662}
6663
6664/*
6665 * Command modifier used in a wrong way.
6666 */
6667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006668ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006670 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671}
6672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673/*
6674 * :sview [+command] file split window with new file, read-only
6675 * :split [[+command] file] split window with current or new file
6676 * :vsplit [[+command] file] split window vertically with current or new file
6677 * :new [[+command] file] split window with no or new file
6678 * :vnew [[+command] file] split vertically window with no or new file
6679 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006680 *
6681 * :tabedit open new Tab page with empty window
6682 * :tabedit [+command] file open new Tab page and edit "file"
6683 * :tabnew [[+command] file] just like :tabedit
6684 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 */
6686 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006687ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006689 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006691#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006692 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006693 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006694#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006695 int use_tab = eap->cmdidx == CMD_tabedit
6696 || eap->cmdidx == CMD_tabfind
6697 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006699 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006700 return;
6701
Bram Moolenaar4033c552017-09-16 20:54:51 +02006702#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
Bram Moolenaar4033c552017-09-16 20:54:51 +02006706#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006707 // A ":split" in the quickfix window works like ":new". Don't want two
6708 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006709 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
6711 if (eap->cmdidx == CMD_split)
6712 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (eap->cmdidx == CMD_vsplit)
6714 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006718 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
6720 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6721 FNAME_MESS, TRUE, curbuf->b_ffname);
6722 if (fname == NULL)
6723 goto theend;
6724 eap->arg = fname;
6725 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006726# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006727 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 && eap->cmdidx != CMD_new)
6730 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006731 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006732# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006733 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006734# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006735 au_has_group((char_u *)"FileExplorer"))
6736 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006737 // No browsing supported but we do have the file explorer:
6738 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006739 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006740 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006741 }
6742 else
6743 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006744 fname = do_browse(0, (char_u *)(use_tab
6745 ? _("Edit File in new tab page")
6746 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006748 if (fname == NULL)
6749 goto theend;
6750 eap->arg = fname;
6751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006753 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006756 /*
6757 * Either open new tab page or split the window.
6758 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006759 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006760 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006761 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006762 : eap->addr_count == 0 ? 0
6763 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006764 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006765 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006767 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006768 if (curwin != old_curwin
6769 && win_valid(old_curwin)
6770 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006771 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006772 old_curwin->w_alt_fnum = curbuf->b_fnum;
6773 }
6774 }
6775 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6777 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006778 // Reset 'scrollbind' when editing another file, but keep it when
6779 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006780 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006781 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 else
6783 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 do_exedit(eap, old_curwin);
6785 }
6786
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006787# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006788 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791theend:
6792 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006794
6795/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006796 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006797 */
6798 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006799tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006800{
6801 exarg_T ea;
6802
Bram Moolenaara80faa82020-04-12 19:37:17 +02006803 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006804 ea.cmdidx = CMD_tabnew;
6805 ea.cmd = (char_u *)"tabn";
6806 ea.arg = (char_u *)"";
6807 ex_splitview(&ea);
6808}
6809
6810/*
6811 * :tabnext command
6812 */
6813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006814ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006815{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006816 int tab_number;
6817
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006818 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006819 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006820 switch (eap->cmdidx)
6821 {
6822 case CMD_tabfirst:
6823 case CMD_tabrewind:
6824 goto_tabpage(1);
6825 break;
6826 case CMD_tablast:
6827 goto_tabpage(9999);
6828 break;
6829 case CMD_tabprevious:
6830 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006831 if (eap->arg && *eap->arg != NUL)
6832 {
6833 char_u *p = eap->arg;
6834 char_u *p_save = p;
6835
6836 tab_number = getdigits(&p);
6837 if (p == p_save || *p_save == '-' || *p != NUL
6838 || tab_number == 0)
6839 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006840 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006841 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006842 return;
6843 }
6844 }
6845 else
6846 {
6847 if (eap->addr_count == 0)
6848 tab_number = 1;
6849 else
6850 {
6851 tab_number = eap->line2;
6852 if (tab_number < 1)
6853 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006854 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006855 return;
6856 }
6857 }
6858 }
6859 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006860 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006861 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006862 tab_number = get_tabpage_arg(eap);
6863 if (eap->errmsg == NULL)
6864 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006865 break;
6866 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006867}
6868
6869/*
6870 * :tabmove command
6871 */
6872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006873ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006874{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006875 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006876
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006877 tab_number = get_tabpage_arg(eap);
6878 if (eap->errmsg == NULL)
6879 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006880}
6881
6882/*
6883 * :tabs command: List tabs and their contents.
6884 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006886ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006887{
6888 tabpage_T *tp;
6889 win_T *wp;
6890 int tabcount = 1;
6891
6892 msg_start();
6893 msg_scroll = TRUE;
6894 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6895 {
6896 msg_putchar('\n');
6897 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006898 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006899 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006900 ui_breakcheck();
6901
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006902 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006903 wp = firstwin;
6904 else
6905 wp = tp->tp_firstwin;
6906 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6907 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006908 msg_putchar('\n');
6909 msg_putchar(wp == curwin ? '>' : ' ');
6910 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006911 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6912 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006913 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006914 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006915 else
6916 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6917 IObuff, IOSIZE, TRUE);
6918 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006919 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006920 ui_breakcheck();
6921 }
6922 }
6923}
6924
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925/*
6926 * ":mode": Set screen mode.
6927 * If no argument given, just get the screen size and redraw.
6928 */
6929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006930ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
6932 if (*eap->arg == NUL)
6933 shell_resized();
6934 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006935 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936}
6937
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938/*
6939 * ":resize".
6940 * set, increment or decrement current window height
6941 */
6942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006943ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 int n;
6946 win_T *wp = curwin;
6947
6948 if (eap->addr_count > 0)
6949 {
6950 n = eap->line2;
6951 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6952 ;
6953 }
6954
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006955# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006959 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {
6961 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006962 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006963 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006965 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 }
6967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {
6969 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006970 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006971 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006973 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 }
6975}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
6977/*
6978 * ":find [+command] <file>" command.
6979 */
6980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 char_u *fname;
6984 int count;
6985
6986 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6987 TRUE, curbuf->b_ffname);
6988 if (eap->addr_count > 0)
6989 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006990 // Repeat finding the file "count" times. This matters when it
6991 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 count = eap->line2;
6993 while (fname != NULL && --count > 0)
6994 {
6995 vim_free(fname);
6996 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6997 FALSE, curbuf->b_ffname);
6998 }
6999 }
7000
7001 if (fname != NULL)
7002 {
7003 eap->arg = fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 do_exedit(eap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 vim_free(fname);
7006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007}
7008
7009/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007010 * ":open" simulation: for now just work like ":visual".
7011 */
7012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007014{
7015 regmatch_T regmatch;
7016 char_u *p;
7017
Bram Moolenaar65088802021-03-13 21:07:21 +01007018#ifdef FEAT_EVAL
7019 if (not_in_vim9(eap) == FAIL)
7020 return;
7021#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007022 curwin->w_cursor.lnum = eap->line2;
7023 beginline(BL_SOL | BL_FIX);
7024 if (*eap->arg == '/')
7025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007026 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007027 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007028 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007029 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007030 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007031 if (regmatch.regprog != NULL)
7032 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007033 // make a copy of the line, when searching for a mark it might be
7034 // flushed
7035 char_u *line = vim_strsave(ml_get_curline());
7036
Bram Moolenaardf177f62005-02-22 08:39:57 +00007037 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007038 if (vim_regexec(&regmatch, line, (colnr_T)0))
7039 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007040 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007041 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007042 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007043 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007044 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007045 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007046 eap->arg += STRLEN(eap->arg);
7047 }
7048 check_cursor();
7049
7050 eap->cmdidx = CMD_visual;
7051 do_exedit(eap, NULL);
7052}
7053
7054/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007055 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 */
7057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007058ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 do_exedit(eap, NULL);
7061}
7062
7063/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007064 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007067do_exedit(
7068 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007069 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007073 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007075 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7076 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007077 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 /*
7079 * ":vi" command ends Ex mode.
7080 */
7081 if (exmode_active && (eap->cmdidx == CMD_visual
7082 || eap->cmdidx == CMD_view))
7083 {
7084 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007085 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007088 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007089 if (global_busy)
7090 {
7091 int rd = RedrawingDisabled;
7092 int nwr = no_wait_return;
7093 int ms = msg_scroll;
7094#ifdef FEAT_GUI
7095 int he = hold_gui_events;
7096#endif
7097
7098 if (eap->nextcmd != NULL)
7099 {
7100 stuffReadbuff(eap->nextcmd);
7101 eap->nextcmd = NULL;
7102 }
7103
7104 if (exmode_was != EXMODE_VIM)
7105 settmode(TMODE_RAW);
7106 RedrawingDisabled = 0;
7107 no_wait_return = 0;
7108 need_wait_return = FALSE;
7109 msg_scroll = 0;
7110#ifdef FEAT_GUI
7111 hold_gui_events = 0;
7112#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007113 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007114 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007115
7116 main_loop(FALSE, TRUE);
7117
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007118 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007119 RedrawingDisabled = rd;
7120 no_wait_return = nwr;
7121 msg_scroll = ms;
7122#ifdef FEAT_GUI
7123 hold_gui_events = he;
7124#endif
7125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 }
7129
7130 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007131 || eap->cmdidx == CMD_tabnew
7132 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007133 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007135 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 setpcmark();
7137 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007138 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7139 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007141 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 || *eap->arg != NUL
7143#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007144 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145#endif
7146 )
7147 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007148 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7149 // Only ":edit" or ":script" can bring us here, others are stopped
7150 // earlier.
7151 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007152 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007153
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 n = readonlymode;
7155 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7156 readonlymode = TRUE;
7157 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007158 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7159 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007160 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7161 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7163 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007164 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7166 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7167 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007168 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007170 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007171 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007172 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7173 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007174 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007176 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (old_curwin != NULL)
7178 {
7179 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007180 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007182#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007183 cleanup_T cs;
7184
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007185 // Reset the error/interrupt/exception state here so that
7186 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007187 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007188#endif
7189#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007191#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007192 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007193
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007194#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007195 // Restore the error/interrupt/exception state if not
7196 // discarded by a new aborting error, interrupt, or
7197 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007198 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
7201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 }
7203 else if (readonlymode && curbuf->b_nwindows == 1)
7204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007205 // When editing an already visited buffer, 'readonly' won't be set
7206 // but the previous value is kept. With ":view" and ":sview" we
7207 // want the file to be readonly, except when another window is
7208 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 curbuf->b_p_ro = TRUE;
7210 }
7211 readonlymode = n;
7212 }
7213 else
7214 {
7215 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007216 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (n != curwin->w_arg_idx_invalid)
7220 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 /*
7224 * if ":split file" worked, set alternate file name in old window to new
7225 * file
7226 */
7227 if (old_curwin != NULL
7228 && *eap->arg != NUL
7229 && curwin != old_curwin
7230 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007231 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007232 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235 ex_no_reprint = TRUE;
7236}
7237
7238#ifndef FEAT_GUI
7239/*
7240 * ":gui" and ":gvim" when there is no GUI.
7241 */
7242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007243ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007245 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246}
7247#endif
7248
Bram Moolenaar4f974752019-02-17 17:44:42 +01007249#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007251ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252{
7253 gui_make_tearoff(eap->arg);
7254}
7255#endif
7256
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007257#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7258 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007260ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007262# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7263 if (gui.in_use)
7264 gui_make_popup(eap->arg, eap->forceit);
7265# ifdef FEAT_TERM_POPUP_MENU
7266 else
7267# endif
7268# endif
7269# ifdef FEAT_TERM_POPUP_MENU
7270 pum_make_popup(eap->arg, eap->forceit);
7271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272}
7273#endif
7274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007276ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
7278 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007279 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007281 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282}
7283
7284/*
7285 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7286 * offset.
7287 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007290ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007293 win_T *save_curwin = curwin;
7294 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 long topline;
7296 long y;
7297 linenr_T old_linenr = curwin->w_cursor.lnum;
7298
7299 setpcmark();
7300
7301 /*
7302 * determine max topline
7303 */
7304 if (curwin->w_p_scb)
7305 {
7306 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007307 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
7309 if (wp->w_p_scb && wp->w_buffer)
7310 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007311 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 if (topline > y)
7313 topline = y;
7314 }
7315 }
7316 if (topline < 1)
7317 topline = 1;
7318 }
7319 else
7320 {
7321 topline = 1;
7322 }
7323
7324
7325 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007326 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007328 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 {
7330 if (curwin->w_p_scb)
7331 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007332 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 y = topline - curwin->w_topline;
7334 if (y > 0)
7335 scrollup(y, TRUE);
7336 else
7337 scrolldown(-y, TRUE);
7338 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007339 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
7343 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007344 curwin = save_curwin;
7345 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 if (curwin->w_p_scb)
7347 {
7348 did_syncbind = TRUE;
7349 checkpcmark();
7350 if (old_linenr != curwin->w_cursor.lnum)
7351 {
7352 char_u ctrl_o[2];
7353
7354 ctrl_o[0] = Ctrl_O;
7355 ctrl_o[1] = 0;
7356 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7357 }
7358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359}
7360
7361
7362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007363ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007365 int i;
7366 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7367 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007369 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 do_bang(1, eap, FALSE, FALSE, TRUE);
7371 else
7372 {
7373 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7374 return;
7375
7376#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007377 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
7379 char_u *browseFile;
7380
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007381 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 NULL, NULL, NULL, curbuf);
7383 if (browseFile != NULL)
7384 {
7385 i = readfile(browseFile, NULL,
7386 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7387 vim_free(browseFile);
7388 }
7389 else
7390 i = OK;
7391 }
7392 else
7393#endif
7394 if (*eap->arg == NUL)
7395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007396 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 return;
7398 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7399 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7400 }
7401 else
7402 {
7403 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7404 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7405 i = readfile(eap->arg, NULL,
7406 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7407
7408 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007409 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007411#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 if (!aborting())
7413#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007414 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
7416 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007417 {
7418 if (empty && exmode_active)
7419 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007420 // Delete the empty line that remains. Historically ex does
7421 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007422 if (eap->line2 == 0)
7423 lnum = curbuf->b_ml.ml_line_count;
7424 else
7425 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007426 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007427 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007428 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007429 if (curwin->w_cursor.lnum > 1
7430 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007431 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007432 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007433 }
7434 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007435 redraw_curbuf_later(UPD_VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 }
7438}
7439
Bram Moolenaarea408852005-06-25 22:49:46 +00007440static char_u *prev_dir = NULL;
7441
7442#if defined(EXITFREE) || defined(PROTO)
7443 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007444free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007445{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007446 VIM_CLEAR(prev_dir);
7447 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007448}
7449#endif
7450
Bram Moolenaarf4258302013-06-02 18:20:17 +02007451/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007452 * Get the previous directory for the given chdir scope.
7453 */
7454 static char_u *
7455get_prevdir(cdscope_T scope)
7456{
7457 if (scope == CDSCOPE_WINDOW)
7458 return curwin->w_prevdir;
7459 else if (scope == CDSCOPE_TABPAGE)
7460 return curtab->tp_prevdir;
7461 return prev_dir;
7462}
7463
7464/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007465 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007466 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7467 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007468 */
7469 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007470post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007471{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007472 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007473 // Clear tab local directory for both :cd and :tcd
7474 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007475 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007476 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007477 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007478 char_u *pdir = get_prevdir(scope);
7479
7480 // If still in the global directory, need to remember current
7481 // directory as the global directory.
7482 if (globaldir == NULL && pdir != NULL)
7483 globaldir = vim_strsave(pdir);
7484
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007485 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007486 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007487 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007488 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007489 curtab->tp_localdir = vim_strsave(NameBuff);
7490 else
7491 curwin->w_localdir = vim_strsave(NameBuff);
7492 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007493 }
7494 else
7495 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007496 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007497 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007498 }
7499
zeertzjq64be6aa2021-11-19 11:59:08 +00007500 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007501 shorten_fnames(TRUE);
7502}
7503
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007504/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007505 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7506 */
7507 void
7508trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7509{
7510#ifdef FEAT_EVAL
7511 dict_T *v_event;
7512 save_v_event_T save_v_event;
7513
7514 v_event = get_v_event(&save_v_event);
7515 (void)dict_add_string(v_event, "directory", new_dir);
7516 dict_set_items_ro(v_event);
7517#endif
7518 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7519#ifdef FEAT_EVAL
7520 restore_v_event(v_event, &save_v_event);
7521#endif
7522}
7523
7524/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007525 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007526 * chdir() function.
7527 * scope == CDSCOPE_WINDOW: changes the window-local directory
7528 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7529 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007530 * Returns TRUE if the directory is successfully changed.
7531 */
7532 int
7533changedir_func(
7534 char_u *new_dir,
7535 int forceit,
7536 cdscope_T scope)
7537{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007538 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007539 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007540 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007541 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007542 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007543
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007544 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007545 return FALSE;
7546
7547 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7548 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007549 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007550 return FALSE;
7551 }
7552
7553 // ":cd -": Change to previous directory
7554 if (STRCMP(new_dir, "-") == 0)
7555 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007556 pdir = get_prevdir(scope);
7557 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007558 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007559 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007560 return FALSE;
7561 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007562 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007563 }
7564
7565 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007566 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007567 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007568 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007569 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007570
Bakudankun29f3a452021-12-11 12:28:08 +00007571 // For UNIX ":cd" means: go to home directory.
7572 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007573#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007574 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007575#else
7576 if (*new_dir == NUL && p_cdh)
7577#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007578 {
7579 // use NameBuff for home directory name
7580# ifdef VMS
7581 char_u *p;
7582
7583 p = mch_getenv((char_u *)"SYS$LOGIN");
7584 if (p == NULL || *p == NUL) // empty is the same as not set
7585 NameBuff[0] = NUL;
7586 else
7587 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7588# else
7589 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7590# endif
7591 new_dir = NameBuff;
7592 }
zeertzjqf38aad82021-12-30 13:45:57 +00007593 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007594 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7595 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007596 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007597 if (scope == CDSCOPE_WINDOW)
7598 acmd_fname = (char_u *)"window";
7599 else if (scope == CDSCOPE_TABPAGE)
7600 acmd_fname = (char_u *)"tabpage";
7601 else
7602 acmd_fname = (char_u *)"global";
7603 trigger_DirChangedPre(acmd_fname, new_dir);
7604
7605 if (vim_chdir(new_dir))
7606 {
7607 emsg(_(e_command_failed));
7608 vim_free(pdir);
7609 return FALSE;
7610 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007611 }
zeertzjq73257142022-02-02 13:16:37 +00007612
7613 if (scope == CDSCOPE_WINDOW)
7614 pp = &curwin->w_prevdir;
7615 else if (scope == CDSCOPE_TABPAGE)
7616 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007617 else
zeertzjq73257142022-02-02 13:16:37 +00007618 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007619 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007620 *pp = pdir;
7621
7622 post_chdir(scope);
7623
7624 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007625 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007626 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007627 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007628}
Bram Moolenaarea408852005-06-25 22:49:46 +00007629
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007631 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007633 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007634ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007636 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
7638 new_dir = eap->arg;
7639#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007640 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7641 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 ex_pwd(NULL);
7643 else
7644#endif
7645 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007646 cdscope_T scope = CDSCOPE_GLOBAL;
7647
7648 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7649 scope = CDSCOPE_WINDOW;
7650 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7651 scope = CDSCOPE_TABPAGE;
7652
7653 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007654 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007655 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007656 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 ex_pwd(eap);
7658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 }
7660}
7661
7662/*
7663 * ":pwd".
7664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007666ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
7668 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7669 {
7670#ifdef BACKSLASH_IN_FILENAME
7671 slash_adjust(NameBuff);
7672#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007673 if (p_verbose > 0)
7674 {
7675 char *context = "global";
7676
Bram Moolenaar05268152021-11-18 18:53:45 +00007677 if (last_chdir_reason != NULL)
7678 context = last_chdir_reason;
7679 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007680 context = "window";
7681 else if (curtab->tp_localdir != NULL)
7682 context = "tabpage";
7683 smsg("[%s] %s", context, (char *)NameBuff);
7684 }
7685 else
7686 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007689 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690}
7691
7692/*
7693 * ":=".
7694 */
7695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007696ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007698 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007699 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700}
7701
7702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007703ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007705 int n;
7706 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707
7708 if (cursor_valid())
7709 {
7710 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7711 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007712 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007714
7715 len = eap->line2;
7716 switch (*eap->arg)
7717 {
7718 case 'm': break;
7719 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007720 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007721 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007722
7723 // Hide the cursor if invoked with !
7724 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725}
7726
7727/*
7728 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007729 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 */
7731 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007732do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
Bram Moolenaar52953192019-08-16 10:27:13 +02007734 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007735 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007736# ifdef ELAPSED_FUNC
7737 elapsed_T start_tv;
7738
7739 // Remember at what time we started, so that we know how much longer we
7740 // should wait after waiting for a bit.
7741 ELAPSED_INIT(start_tv);
7742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007744 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007745 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007746 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007747 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007748
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007749 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007750 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007752 wait_now = msec - done > 1000L ? 1000L : msec - done;
7753#ifdef FEAT_TIMERS
7754 {
7755 long due_time = check_due_timer();
7756
7757 if (due_time > 0 && due_time < wait_now)
7758 wait_now = due_time;
7759 }
7760#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007761#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007762 if (has_any_channel() && wait_now > 20L)
7763 wait_now = 20L;
7764#endif
7765#ifdef FEAT_SOUND
7766 if (has_any_sound_callback() && wait_now > 20L)
7767 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007768#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007769 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007770
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007771#ifdef FEAT_JOB_CHANNEL
7772 if (has_any_channel())
7773 ui_breakcheck_force(TRUE);
7774 else
7775#endif
7776 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007777#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007778 // Process the netbeans and clientserver messages that may have been
7779 // received in the call to ui_breakcheck() when the GUI is in use. This
7780 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007781 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007782#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007783
7784# ifdef ELAPSED_FUNC
7785 // actual time passed
7786 done = ELAPSED_FUNC(start_tv);
7787# else
7788 // guestimate time passed (will actually be more)
7789 done += wait_now;
7790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007792
7793 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7794 // input buffer, otherwise a following call to input() fails.
7795 if (got_int)
7796 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007797
7798 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007799 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800}
7801
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802/*
7803 * ":winsize" command (obsolete).
7804 */
7805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007806ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 int w, h;
7809 char_u *arg = eap->arg;
7810 char_u *p;
7811
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007812 if (!isdigit(*arg))
7813 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007814 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007815 return;
7816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 w = getdigits(&arg);
7818 arg = skipwhite(arg);
7819 p = arg;
7820 h = getdigits(&arg);
7821 if (*p != NUL && *arg == NUL)
7822 set_shellsize(w, h, TRUE);
7823 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007824 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825}
7826
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007828ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
7830 int xchar = NUL;
7831 char_u *p;
7832
7833 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007835 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (eap->arg[1] == NUL)
7837 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007838 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 return;
7840 }
7841 xchar = eap->arg[1];
7842 p = eap->arg + 2;
7843 }
7844 else
7845 p = eap->arg + 1;
7846
Bram Moolenaar63b91732021-08-05 20:40:03 +02007847 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007849 if (*p != NUL && *p != (
7850#ifdef FEAT_EVAL
7851 in_vim9script() ? '#' :
7852#endif
7853 '"')
7854 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007855 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007856 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007858 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007859 postponed_split_flags = cmdmod.cmod_split;
7860 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7862 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007863 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866
Bram Moolenaar843ee412004-06-30 16:16:41 +00007867#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868/*
7869 * ":winpos".
7870 */
7871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007872ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 int x, y;
7875 char_u *arg = eap->arg;
7876 char_u *p;
7877
7878 if (*arg == NUL)
7879 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007880# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007881# ifdef VIMDLL
7882 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7883 mch_get_winpos(&x, &y) != FAIL)
7884# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007886# else
7887 if (mch_get_winpos(&x, &y) != FAIL)
7888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {
7890 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007891 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 }
7893 else
7894# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007895 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 }
7897 else
7898 {
7899 x = getdigits(&arg);
7900 arg = skipwhite(arg);
7901 p = arg;
7902 y = getdigits(&arg);
7903 if (*p == NUL || *arg != NUL)
7904 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007905 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 return;
7907 }
7908# ifdef FEAT_GUI
7909 if (gui.in_use)
7910 gui_mch_set_winpos(x, y);
7911 else if (gui.starting)
7912 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007913 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 gui_win_x = x;
7915 gui_win_y = y;
7916 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007917# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 else
7919# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007920# endif
7921# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007922 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923# endif
7924# ifdef HAVE_TGETENT
7925 if (*T_CWP)
7926 term_set_winpos(x, y);
7927# endif
7928 }
7929}
7930#endif
7931
7932/*
7933 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7934 */
7935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007936ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 oparg_T oa;
7939
7940 clear_oparg(&oa);
7941 oa.regname = eap->regname;
7942 oa.start.lnum = eap->line1;
7943 oa.end.lnum = eap->line2;
7944 oa.line_count = eap->line2 - eap->line1 + 1;
7945 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007947 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
7949 setpcmark();
7950 curwin->w_cursor.lnum = eap->line1;
7951 beginline(BL_SOL | BL_FIX);
7952 }
7953
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007954 if (VIsual_active)
7955 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007956
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 switch (eap->cmdidx)
7958 {
7959 case CMD_delete:
7960 oa.op_type = OP_DELETE;
7961 op_delete(&oa);
7962 break;
7963
7964 case CMD_yank:
7965 oa.op_type = OP_YANK;
7966 (void)op_yank(&oa, FALSE, TRUE);
7967 break;
7968
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007969 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007970 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007972 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7973#else
7974 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007976 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 oa.op_type = OP_RSHIFT;
7978 else
7979 oa.op_type = OP_LSHIFT;
7980 op_shift(&oa, FALSE, eap->amount);
7981 break;
7982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007984 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985}
7986
7987/*
7988 * ":put".
7989 */
7990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007991ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007993 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (eap->line2 == 0)
7995 {
7996 eap->line2 = 1;
7997 eap->forceit = TRUE;
7998 }
7999 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008000 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008001 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008002 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003}
8004
8005/*
8006 * Handle ":copy" and ":move".
8007 */
8008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008009ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010{
8011 long n;
8012
Bram Moolenaar491799b2020-08-01 19:23:43 +02008013#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008014 if (not_in_vim9(eap) == FAIL)
8015 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008016#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008017 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008018 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 {
8020 eap->nextcmd = NULL;
8021 return;
8022 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008023 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024
8025 /*
8026 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8027 */
8028 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8029 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008030 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 return;
8032 }
8033
8034 if (eap->cmdidx == CMD_move)
8035 {
8036 if (do_move(eap->line1, eap->line2, n) == FAIL)
8037 return;
8038 }
8039 else
8040 ex_copy(eap->line1, eap->line2, n);
8041 u_clearline();
8042 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008043 ex_may_print(eap);
8044}
8045
8046/*
8047 * Print the current line if flags were given to the Ex command.
8048 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008050ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008051{
8052 if (eap->flags != 0)
8053 {
8054 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8055 (eap->flags & EXFLAG_LIST));
8056 ex_no_reprint = TRUE;
8057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058}
8059
8060/*
8061 * ":smagic" and ":snomagic".
8062 */
8063 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008064ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008066 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008068 magic_overruled = eap->cmdidx == CMD_smagic
8069 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008070 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008071 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072}
8073
8074/*
8075 * ":join".
8076 */
8077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008078ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
8080 curwin->w_cursor.lnum = eap->line1;
8081 if (eap->line1 == eap->line2)
8082 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008083 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 return;
8085 if (eap->line2 == curbuf->b_ml.ml_line_count)
8086 {
8087 beep_flush();
8088 return;
8089 }
8090 ++eap->line2;
8091 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008092 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008094 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095}
8096
8097/*
8098 * ":[addr]@r" or ":[addr]*r": execute register
8099 */
8100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008101ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008104 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105
8106 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008107 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108
8109#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111#endif
8112
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008113 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 c = *eap->arg;
8115 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8116 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008117 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008118 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8119 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 else
8124 {
8125 int save_efr = exec_from_reg;
8126
8127 exec_from_reg = TRUE;
8128
8129 /*
8130 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008131 * Continue until the stuff buffer is empty and all added characters
8132 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008134 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8136
8137 exec_from_reg = save_efr;
8138 }
8139}
8140
8141/*
8142 * ":!".
8143 */
8144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008145ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
8147 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8148}
8149
8150/*
8151 * ":undo".
8152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008154ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008156 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008157 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008158 else
8159 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160}
8161
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008162#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008164ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008165{
8166 char_u hash[UNDO_HASH_SIZE];
8167
8168 u_compute_hash(hash);
8169 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8170}
8171
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008173ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008174{
8175 char_u hash[UNDO_HASH_SIZE];
8176
8177 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008178 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008179}
8180#endif
8181
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182/*
8183 * ":redo".
8184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008186ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187{
8188 u_redo(1);
8189}
8190
8191/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008192 * ":earlier" and ":later".
8193 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008194 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008195ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008196{
8197 long count = 0;
8198 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008199 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008200 char_u *p = eap->arg;
8201
8202 if (*p == NUL)
8203 count = 1;
8204 else if (isdigit(*p))
8205 {
8206 count = getdigits(&p);
8207 switch (*p)
8208 {
8209 case 's': ++p; sec = TRUE; break;
8210 case 'm': ++p; sec = TRUE; count *= 60; break;
8211 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008212 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8213 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008214 }
8215 }
8216
8217 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008218 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008219 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008220 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8221 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008222}
8223
8224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 * ":redir": start/stop redirection.
8226 */
8227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008228ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 char *mode;
8231 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008232 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
Bram Moolenaarba768492016-07-08 15:32:54 +02008234#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008235 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008236 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008237 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008238 return;
8239 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008240#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008241
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 if (STRICMP(eap->arg, "END") == 0)
8243 close_redir();
8244 else
8245 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008246 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008248 ++arg;
8249 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008251 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 mode = "a";
8253 }
8254 else
8255 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008256 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257
8258 close_redir();
8259
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008260 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008261 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 if (fname == NULL)
8263 return;
8264#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008265 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {
8267 char_u *browseFile;
8268
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008269 browseFile = do_browse(BROWSE_SAVE,
8270 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008271 fname, NULL, NULL,
8272 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 vim_free(fname);
8276 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008277 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 }
8279#endif
8280
8281 redir_fd = open_exfile(fname, eap->forceit, mode);
8282 vim_free(fname);
8283 }
8284#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008285 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008287 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008289 ++arg;
8290 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008292 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008293 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008295 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008297 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008298 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008299 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008302 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008303 if (*arg == '>')
8304 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008305 // Make register empty when not using @A-@Z and the
8306 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008307 if (*arg == NUL && !isupper(redir_reg))
8308 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008310 }
8311 if (*arg != NUL)
8312 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008313 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008314 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008315 }
8316 }
8317 else if (*arg == '=' && arg[1] == '>')
8318 {
8319 int append;
8320
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008321 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008322 close_redir();
8323 arg += 2;
8324
8325 if (*arg == '>')
8326 {
8327 ++arg;
8328 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 }
8330 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008331 append = FALSE;
8332
8333 if (var_redir_start(skipwhite(arg), append) == OK)
8334 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
8336#endif
8337
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008338 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008341 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008343
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008344 // Make sure redirection is not off. Can happen for cmdline completion
8345 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008346 if (redir_fd != NULL
8347#ifdef FEAT_EVAL
8348 || redir_reg || redir_vname
8349#endif
8350 )
8351 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352}
8353
8354/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008355 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008357 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008358ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008360 redraw_cmd(eap->forceit);
8361}
8362
8363/*
8364 * ":redraw": force redraw, with clear if "clear" is TRUE.
8365 */
8366 void
8367redraw_cmd(int clear)
8368{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 int r = RedrawingDisabled;
8370 int p = p_lz;
8371
8372 RedrawingDisabled = 0;
8373 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008374 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008376 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 if (need_maketitle)
8378 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008379#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8380# ifdef VIMDLL
8381 if (!gui.in_use)
8382# endif
8383 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 RedrawingDisabled = r;
8386 p_lz = p;
8387
Bram Moolenaar5017c662022-04-07 18:06:08 +01008388 // After drawing the statusline screen_attr may still be set.
8389 screen_stop_highlight();
8390
Bram Moolenaara2a89732022-08-31 14:46:18 +01008391 // Reset msg_didout, so that a message that's there is overwritten.
8392 msg_didout = FALSE;
8393 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008395 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008396 need_wait_return = FALSE;
8397
Bram Moolenaara653e532022-04-19 11:38:24 +01008398 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008399 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008400 redrawcmdline();
8401
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 out_flush();
8403}
8404
8405/*
8406 * ":redrawstatus": force redraw of status line(s)
8407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008409ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 int r = RedrawingDisabled;
8412 int p = p_lz;
8413
8414 RedrawingDisabled = 0;
8415 p_lz = FALSE;
8416 if (eap->forceit)
8417 status_redraw_all();
8418 else
8419 status_redraw_curbuf();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01008420 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 RedrawingDisabled = r;
8422 p_lz = p;
8423 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424}
8425
Bram Moolenaare12bab32019-01-08 22:02:56 +01008426/*
8427 * ":redrawtabline": force redraw of the tabline
8428 */
8429 static void
8430ex_redrawtabline(exarg_T *eap UNUSED)
8431{
8432 int r = RedrawingDisabled;
8433 int p = p_lz;
8434
8435 RedrawingDisabled = 0;
8436 p_lz = FALSE;
8437
8438 draw_tabline();
8439
8440 RedrawingDisabled = r;
8441 p_lz = p;
8442 out_flush();
8443}
8444
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008446close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 if (redir_fd != NULL)
8449 {
8450 fclose(redir_fd);
8451 redir_fd = NULL;
8452 }
8453#ifdef FEAT_EVAL
8454 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008455 if (redir_vname)
8456 {
8457 var_redir_stop();
8458 redir_vname = 0;
8459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460#endif
8461}
8462
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008463#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008464 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008465vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008466{
8467 if (vim_mkdir(name, prot) != 0)
8468 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008469 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008470 return FAIL;
8471 }
8472 return OK;
8473}
8474#endif
8475
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476/*
8477 * Open a file for writing for an Ex command, with some checks.
8478 * Return file descriptor, or NULL on failure.
8479 */
8480 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008481open_exfile(
8482 char_u *fname,
8483 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008484 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
8486 FILE *fd;
8487
8488#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008489 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 if (mch_isdir(fname))
8491 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008492 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 return NULL;
8494 }
8495#endif
8496 if (!forceit && *mode != 'a' && vim_fexists(fname))
8497 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008498 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 return NULL;
8500 }
8501
8502 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008503 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504
8505 return fd;
8506}
8507
8508/*
8509 * ":mark" and ":k".
8510 */
8511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008512ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513{
8514 pos_T pos;
8515
Bram Moolenaar10b94212021-02-19 21:42:57 +01008516#ifdef FEAT_EVAL
8517 if (not_in_vim9(eap) == FAIL)
8518 return;
8519#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008520 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008521 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008522 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008523 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 else
8525 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008526 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 curwin->w_cursor.lnum = eap->line2;
8528 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008530 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008531 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 }
8533}
8534
8535/*
8536 * Update w_topline, w_leftcol and the cursor position.
8537 */
8538 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008539update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008541 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 update_topline();
8543 if (!curwin->w_p_wrap)
8544 validate_cursor();
8545 update_curswant();
8546}
8547
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008549 * Save the current State and go to Normal mode.
8550 * Return TRUE if the typeahead could be saved.
8551 */
8552 int
8553save_current_state(save_state_T *sst)
8554{
8555 sst->save_msg_scroll = msg_scroll;
8556 sst->save_restart_edit = restart_edit;
8557 sst->save_msg_didout = msg_didout;
8558 sst->save_State = State;
8559 sst->save_insertmode = p_im;
8560 sst->save_finish_op = finish_op;
8561 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008562 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008563 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008564
Bram Moolenaar4324d872020-12-01 20:12:24 +01008565 msg_scroll = FALSE; // no msg scrolling in Normal mode
8566 restart_edit = 0; // don't go to Insert mode
8567 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008568
Bram Moolenaara452b802020-12-01 21:47:59 +01008569 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008570 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008571
8572 /*
8573 * Save the current typeahead. This is required to allow using ":normal"
8574 * from an event handler and makes sure we don't hang when the argument
8575 * ends with half a command.
8576 */
8577 save_typeahead(&sst->tabuf);
8578 return sst->tabuf.typebuf_valid;
8579}
8580
8581 void
8582restore_current_state(save_state_T *sst)
8583{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008584 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008585 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008586
8587 msg_scroll = sst->save_msg_scroll;
8588 restart_edit = sst->save_restart_edit;
8589 p_im = sst->save_insertmode;
8590 finish_op = sst->save_finish_op;
8591 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008592 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008593 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008594 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008595 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008596
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008597 // Restore the state (needed when called from a function executed for
8598 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008599 State = sst->save_State;
8600#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008601 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008602#endif
8603}
8604
8605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 * ":normal[!] {commands}": Execute normal mode commands.
8607 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008608 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008609ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008611 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 char_u *arg = NULL;
8613 int l;
8614 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008616 if (ex_normal_lock > 0)
8617 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008618 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008619 return;
8620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 if (ex_normal_busy >= p_mmd)
8622 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008623 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 return;
8625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 /*
8628 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8629 * this for the K_SPECIAL leading byte, otherwise special keys will not
8630 * work.
8631 */
8632 if (has_mbyte)
8633 {
8634 int len = 0;
8635
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008636 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 for (p = eap->arg; *p != NUL; ++p)
8638 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008639#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008640 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008642#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008643 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008644 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008645#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 )
8649 len += 2;
8650 }
8651 if (len > 0)
8652 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008653 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 if (arg != NULL)
8655 {
8656 len = 0;
8657 for (p = eap->arg; *p != NUL; ++p)
8658 {
8659 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008660#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 if (*p == CSI)
8662 {
8663 arg[len++] = KS_EXTRA;
8664 arg[len++] = (int)KE_CSI;
8665 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008666#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008667 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {
8669 arg[len++] = *++p;
8670 if (*p == K_SPECIAL)
8671 {
8672 arg[len++] = KS_SPECIAL;
8673 arg[len++] = KE_FILLER;
8674 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008675#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 else if (*p == CSI)
8677 {
8678 arg[len++] = KS_EXTRA;
8679 arg[len++] = (int)KE_CSI;
8680 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 }
8683 arg[len] = NUL;
8684 }
8685 }
8686 }
8687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008689 ++ex_normal_busy;
8690 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {
8692 /*
8693 * Repeat the :normal command for each line in the range. When no
8694 * range given, execute it just once, without positioning the cursor
8695 * first.
8696 */
8697 do
8698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 if (eap->addr_count != 0)
8700 {
8701 curwin->w_cursor.lnum = eap->line1++;
8702 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008703 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 }
8705
Bram Moolenaar13505972019-01-24 15:04:48 +01008706 exec_normal_cmd(arg != NULL
8707 ? arg
8708 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 }
8710 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8711 }
8712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008713 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 update_topline_cursor();
8715
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008716 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008718 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008719#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008720 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008721#endif
8722
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724}
8725
8726/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008727 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 */
8729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008730ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008732 if (eap->forceit)
8733 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008734 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008735 if (!curwin->w_cursor.lnum)
8736 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008737 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008738 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008739#ifdef FEAT_TERMINAL
8740 // Ignore this when running in an active terminal.
8741 if (term_job_running(curbuf->b_term))
8742 return;
8743#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008744
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008745 // Ignore the command when already in Insert mode. Inserting an
8746 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008747 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008748 return;
8749
Bram Moolenaar64969662005-12-14 21:59:55 +00008750 if (eap->cmdidx == CMD_startinsert)
8751 restart_edit = 'a';
8752 else if (eap->cmdidx == CMD_startreplace)
8753 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008755 restart_edit = 'V';
8756
8757 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008759 if (eap->cmdidx == CMD_startinsert)
8760 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008761 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008763
8764 if (VIsual_active)
8765 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766}
8767
8768/*
8769 * ":stopinsert"
8770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008772ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 restart_edit = 0;
8775 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008776 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008779/*
8780 * Execute normal mode command "cmd".
8781 * "remap" can be REMAP_NONE or REMAP_YES.
8782 */
8783 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008784exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008785{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008786 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008787 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008788 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008789}
Bram Moolenaar25281632016-01-21 23:32:32 +01008790
Bram Moolenaar25281632016-01-21 23:32:32 +01008791/*
8792 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008793 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008794 */
8795 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008796exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008797{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008798 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008799 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008800
Bram Moolenaar905dd902019-04-07 14:21:47 +02008801 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8802 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008803 clear_oparg(&oa);
8804 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008805 while ((!stuff_empty()
8806 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008807 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008808 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008809 {
8810 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008811#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008812 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008813 && oa.op_type == OP_NOP && oa.regname == NUL
8814 && !VIsual_active)
8815 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008816 // If terminal_loop() returns OK we got a key that is handled
8817 // in Normal model. With FAIL we first need to position the
8818 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008819 if (terminal_loop(TRUE) == OK)
8820 normal_cmd(&oa, TRUE);
8821 }
8822 else
8823#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008824 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008825 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008826 }
8827}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008828
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829#ifdef FEAT_FIND_ID
8830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008831ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
8833 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8834 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8835 (linenr_T)1, (linenr_T)MAXLNUM);
8836}
8837
Bram Moolenaar4033c552017-09-16 20:54:51 +02008838#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839/*
8840 * ":psearch"
8841 */
8842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008843ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
8845 g_do_tagpreview = p_pvh;
8846 ex_findpat(eap);
8847 g_do_tagpreview = 0;
8848}
8849#endif
8850
8851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008852ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
8854 int whole = TRUE;
8855 long n;
8856 char_u *p;
8857 int action;
8858
8859 switch (cmdnames[eap->cmdidx].cmd_name[2])
8860 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008861 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8863 action = ACTION_GOTO;
8864 else
8865 action = ACTION_SHOW;
8866 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008867 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 action = ACTION_SHOW_ALL;
8869 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008870 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 action = ACTION_GOTO;
8872 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008873 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 action = ACTION_SPLIT;
8875 break;
8876 }
8877
8878 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008879 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 n = getdigits(&eap->arg);
8882 eap->arg = skipwhite(eap->arg);
8883 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008884 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
8886 whole = FALSE;
8887 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008888 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 if (*p)
8890 {
8891 *p++ = NUL;
8892 p = skipwhite(p);
8893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008894 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008895 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008896 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008898 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
8900 }
8901 if (!eap->skip)
8902 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8903 whole, !eap->forceit,
8904 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8905 n, action, eap->line1, eap->line2);
8906}
8907#endif
8908
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
Bram Moolenaar4033c552017-09-16 20:54:51 +02008910#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911/*
8912 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8913 */
8914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008915ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008917 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8919}
8920
8921/*
8922 * ":pedit"
8923 */
8924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008925ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 win_T *curwin_save = curwin;
8928
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008929 if (ERROR_IF_ANY_POPUP_WINDOW)
8930 return;
8931
Bram Moolenaar026587b2019-08-17 15:08:00 +02008932 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008934 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008935
Bram Moolenaar026587b2019-08-17 15:08:00 +02008936 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 if (curwin != curwin_save && win_valid(curwin_save))
8940 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008941 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01008943 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 win_enter(curwin_save, TRUE);
8945 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008946# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008947 else if (WIN_IS_POPUP(curwin))
8948 {
8949 // can't keep focus in popup window
8950 win_enter(firstwin, TRUE);
8951 }
8952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 g_do_tagpreview = 0;
8954}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
8957/*
8958 * ":stag", ":stselect" and ":stjump".
8959 */
8960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008961ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008964 postponed_split_flags = cmdmod.cmod_split;
8965 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8967 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008968 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970
8971/*
8972 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8973 */
8974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008975ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
8977 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8978}
8979
8980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008981ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
8983 int cmd;
8984
8985 switch (name[1])
8986 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008987 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008989 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008991 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008993 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008995 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008997 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008999 case 'f': // ":tfirst"
9000 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009002 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009004 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009006 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009008 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 return;
9010 }
9011#endif
9012 cmd = DT_TAG;
9013 break;
9014 }
9015
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009016 if (name[0] == 'l')
9017 {
9018#ifndef FEAT_QUICKFIX
9019 ex_ni(eap);
9020 return;
9021#else
9022 cmd = DT_LTAG;
9023#endif
9024 }
9025
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9027 eap->forceit, TRUE);
9028}
9029
9030/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009031 * Check "str" for starting with a special cmdline variable.
9032 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9033 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9034 */
9035 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009036find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009037{
9038 int len;
9039 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009040 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009041 "%",
9042#define SPEC_PERC 0
9043 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009044#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009045 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009046#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009047 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009048#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009049 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009050#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009051 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009052#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009053 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009054#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009055 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009056#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009057 "<stack>", // call stack
9058#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009059 "<script>", // script file name
9060#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009061 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009062#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009063 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009064#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009065 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009066#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009067 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009068#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009069 "<SID>", // script ID: <SNR>123_
9070#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009071#ifdef FEAT_CLIENTSERVER
9072 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009073# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009074#endif
9075 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009076
K.Takataeeec2542021-06-02 13:28:16 +02009077 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009078 {
9079 len = (int)STRLEN(spec_str[i]);
9080 if (STRNCMP(src, spec_str[i], len) == 0)
9081 {
9082 *usedlen = len;
9083 return i;
9084 }
9085 }
9086 return -1;
9087}
9088
9089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 * Evaluate cmdline variables.
9091 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009092 * change "%" to curbuf->b_ffname
9093 * "#" to curwin->w_alt_fnum
9094 * "%%" to curwin->w_alt_fnum in Vim9 script
9095 * "<cword>" to word under the cursor
9096 * "<cWORD>" to WORD under the cursor
9097 * "<cexpr>" to C-expression under the cursor
9098 * "<cfile>" to path name under the cursor
9099 * "<sfile>" to sourced file name
9100 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009101 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009102 * "<slnum>" to sourced file line number
9103 * "<afile>" to file name for autocommand
9104 * "<abuf>" to buffer number for autocommand
9105 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 *
9107 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9108 * "" for error without a message) and NULL is returned.
9109 * Returns an allocated string if a valid match was found.
9110 * Returns NULL if no match was found. "usedlen" then still contains the
9111 * number of characters to skip.
9112 */
9113 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009114eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009115 char_u *src, // pointer into commandline
9116 char_u *srcstart, // beginning of valid memory for src
9117 int *usedlen, // characters after src that are used
9118 linenr_T *lnump, // line number for :e command, or NULL
9119 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009120 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009121 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009122 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 int i;
9125 char_u *s;
9126 char_u *result;
9127 char_u *resultbuf = NULL;
9128 int resultlen;
9129 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009130 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009132 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135
9136 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009137 if (escaped != NULL)
9138 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139
9140 /*
9141 * Check if there is something to do.
9142 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009143 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009144 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 {
9146 *usedlen = 1;
9147 return NULL;
9148 }
9149
9150 /*
9151 * Skip when preceded with a backslash "\%" and "\#".
9152 * Note: In "\\%" the % is also not recognized!
9153 */
9154 if (src > srcstart && src[-1] == '\\')
9155 {
9156 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009157 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 return NULL;
9159 }
9160
9161 /*
9162 * word or WORD under cursor
9163 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009164 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9165 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009167 resultlen = find_ident_under_cursor(&result,
9168 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9169 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9170 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (resultlen == 0)
9172 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009173 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 return NULL;
9175 }
9176 }
9177
9178 /*
9179 * '#': Alternate file name
9180 * '%': Current file name
9181 * File name under the cursor
9182 * File name for autocommand
9183 * and following modifiers
9184 */
9185 else
9186 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009187 int off = 0;
9188
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 switch (spec_idx)
9190 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009191 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009192#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009193 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009196 // '%': current file
9197 if (curbuf->b_fname == NULL)
9198 {
9199 result = (char_u *)"";
9200 valid = 0; // Must have ":p:h" to be valid
9201 }
9202 else
9203 {
9204 result = curbuf->b_fname;
9205 tilde_file = STRCMP(result, "~") == 0;
9206 }
9207 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009209#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009210 // "%%" alternate file
9211 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009212#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009213 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009214 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009215 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009217 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 result = arg_all();
9219 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009220 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009221 if (escaped != NULL)
9222 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 break;
9225 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009226 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009227 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009228 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009230 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009231 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009232 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009233 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009235 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009237 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009239 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009240 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009241 return NULL;
9242 }
9243#ifdef FEAT_EVAL
9244 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9245 (long)i);
9246 if (result == NULL)
9247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009248 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009249 return NULL;
9250 }
9251#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009252 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 }
9256 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009257 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009258 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9259 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009260 buf = buflist_findnr(i);
9261 if (buf == NULL)
9262 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009263 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009264 return NULL;
9265 }
9266 if (lnump != NULL)
9267 *lnump = ECMD_LAST;
9268 if (buf->b_fname == NULL)
9269 {
9270 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009271 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009272 }
9273 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009274 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009275 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009276 tilde_file = STRCMP(result, "~") == 0;
9277 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 break;
9280
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009281 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009282 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 if (result == NULL)
9284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009285 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 return NULL;
9287 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009288 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009291 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009293 if (result != NULL && !autocmd_fname_full)
9294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009295 // Still need to turn the fname into a full path. It is
9296 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009297 autocmd_fname_full = TRUE;
9298 result = FullName_save(autocmd_fname, FALSE);
9299 vim_free(autocmd_fname);
9300 autocmd_fname = result;
9301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 if (result == NULL)
9303 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009304 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 return NULL;
9306 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009307 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 break;
9309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009310 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 if (autocmd_bufnr <= 0)
9312 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009313 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 return NULL;
9315 }
9316 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9317 result = strbuf;
9318 break;
9319
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009320 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 result = autocmd_match;
9322 if (result == NULL)
9323 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009324 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 return NULL;
9326 }
9327 break;
9328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009329 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009330 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 if (result == NULL)
9332 {
LemonBoy6013d002022-04-09 21:42:10 +01009333 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9334 return NULL;
9335 }
9336 resultbuf = result; // remember allocated string
9337 break;
9338 case SPEC_STACK: // call stack
9339 result = estack_sfile(ESTACK_STACK);
9340 if (result == NULL)
9341 {
9342 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9343 return NULL;
9344 }
9345 resultbuf = result; // remember allocated string
9346 break;
9347 case SPEC_SCRIPT: // script file name
9348 result = estack_sfile(ESTACK_SCRIPT);
9349 if (result == NULL)
9350 {
9351 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 return NULL;
9353 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009354 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009356
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009357 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009358 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009359 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009360 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009361 return NULL;
9362 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009363 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009364 result = strbuf;
9365 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009366
9367#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009368 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009369 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009370 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009371 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009372 return NULL;
9373 }
9374 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009375 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009376 result = strbuf;
9377 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009378
Bram Moolenaar90944302020-08-01 20:45:11 +02009379 case SPEC_SID:
9380 if (current_sctx.sc_sid <= 0)
9381 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009382 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009383 return NULL;
9384 }
9385 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9386 result = strbuf;
9387 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009388#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009389
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009390#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009391 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009392 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9393 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 result = strbuf;
9395 break;
9396#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009397
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009398 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009399 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 }
9402
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009403 resultlen = (int)STRLEN(result); // length of new string
9404 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 {
9406 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 resultlen = (int)(s - result);
9409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 else if (!skip_mod)
9411 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009412 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 &resultlen);
9414 if (result == NULL)
9415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009416 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 return NULL;
9418 }
9419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 }
9421
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009422 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009424 if (empty_is_error)
9425 {
9426 if (valid != VALID_HEAD + VALID_PATH)
9427 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9428 else
9429 *errormsg = _(e_evaluates_to_an_empty_string);
9430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 result = NULL;
9432 }
9433 else
9434 result = vim_strnsave(result, resultlen);
9435 vim_free(resultbuf);
9436 return result;
9437}
9438
9439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 * Expand the <sfile> string in "arg".
9441 *
9442 * Returns an allocated string, or NULL for any error.
9443 */
9444 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009445expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009447 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 int len;
9449 char_u *result;
9450 char_u *newres;
9451 char_u *repl;
9452 int srclen;
9453 char_u *p;
9454
9455 result = vim_strsave(arg);
9456 if (result == NULL)
9457 return NULL;
9458
9459 for (p = result; *p; )
9460 {
9461 if (STRNCMP(p, "<sfile>", 7) != 0)
9462 ++p;
9463 else
9464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009465 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009466 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 if (errormsg != NULL)
9468 {
9469 if (*errormsg)
9470 emsg(errormsg);
9471 vim_free(result);
9472 return NULL;
9473 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009474 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 {
9476 p += srclen;
9477 continue;
9478 }
9479 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9480 newres = alloc(len);
9481 if (newres == NULL)
9482 {
9483 vim_free(repl);
9484 vim_free(result);
9485 return NULL;
9486 }
9487 mch_memmove(newres, result, (size_t)(p - result));
9488 STRCPY(newres + (p - result), repl);
9489 len = (int)STRLEN(newres);
9490 STRCAT(newres, p + srclen);
9491 vim_free(repl);
9492 vim_free(result);
9493 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009494 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 }
9496 }
9497
9498 return result;
9499}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009502/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009503 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009504 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009507dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 if (fname == NULL)
9510 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009511 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512}
9513#endif
9514
9515/*
9516 * ":behave {mswin,xterm}"
9517 */
9518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009519ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 if (STRCMP(eap->arg, "mswin") == 0)
9522 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009523 set_option_value_give_err((char_u *)"selection",
9524 0L, (char_u *)"exclusive", 0);
9525 set_option_value_give_err((char_u *)"selectmode",
9526 0L, (char_u *)"mouse,key", 0);
9527 set_option_value_give_err((char_u *)"mousemodel",
9528 0L, (char_u *)"popup", 0);
9529 set_option_value_give_err((char_u *)"keymodel",
9530 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 }
9532 else if (STRCMP(eap->arg, "xterm") == 0)
9533 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009534 set_option_value_give_err((char_u *)"selection",
9535 0L, (char_u *)"inclusive", 0);
9536 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9537 set_option_value_give_err((char_u *)"mousemodel",
9538 0L, (char_u *)"extend", 0);
9539 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 }
9541 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009542 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543}
9544
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545static int filetype_detect = FALSE;
9546static int filetype_plugin = FALSE;
9547static int filetype_indent = FALSE;
9548
9549/*
9550 * ":filetype [plugin] [indent] {on,off,detect}"
9551 * on: Load the filetype.vim file to install autocommands for file types.
9552 * off: Load the ftoff.vim file to remove all autocommands for file types.
9553 * plugin on: load filetype.vim and ftplugin.vim
9554 * plugin off: load ftplugof.vim
9555 * indent on: load filetype.vim and indent.vim
9556 * indent off: load indoff.vim
9557 */
9558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009559ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560{
9561 char_u *arg = eap->arg;
9562 int plugin = FALSE;
9563 int indent = FALSE;
9564
9565 if (*eap->arg == NUL)
9566 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009567 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009568 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 filetype_detect ? "ON" : "OFF",
9570 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9571 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9572 return;
9573 }
9574
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009575 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 for (;;)
9577 {
9578 if (STRNCMP(arg, "plugin", 6) == 0)
9579 {
9580 plugin = TRUE;
9581 arg = skipwhite(arg + 6);
9582 continue;
9583 }
9584 if (STRNCMP(arg, "indent", 6) == 0)
9585 {
9586 indent = TRUE;
9587 arg = skipwhite(arg + 6);
9588 continue;
9589 }
9590 break;
9591 }
9592 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9593 {
9594 if (*arg == 'o' || !filetype_detect)
9595 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009596 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 filetype_detect = TRUE;
9598 if (plugin)
9599 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009600 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 filetype_plugin = TRUE;
9602 }
9603 if (indent)
9604 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009605 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 filetype_indent = TRUE;
9607 }
9608 }
9609 if (*arg == 'd')
9610 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009611 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009612 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 }
9614 }
9615 else if (STRCMP(arg, "off") == 0)
9616 {
9617 if (plugin || indent)
9618 {
9619 if (plugin)
9620 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009621 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 filetype_plugin = FALSE;
9623 }
9624 if (indent)
9625 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009626 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 filetype_indent = FALSE;
9628 }
9629 }
9630 else
9631 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009632 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 filetype_detect = FALSE;
9634 }
9635 }
9636 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009637 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639
9640/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009641 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 */
9643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009644ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
9646 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009647 {
9648 char_u *arg = eap->arg;
9649
9650 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9651 arg += 9;
9652
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009653 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
Bram Moolenaar3e545692017-06-04 19:00:32 +02009654 if (arg != eap->arg)
9655 did_filetype = FALSE;
9656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009660ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662#ifdef FEAT_DIGRAPHS
9663 if (*eap->arg != NUL)
9664 putdigraph(eap->arg);
9665 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009666 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667#else
mityu61065042021-07-19 20:07:21 +02009668 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669#endif
9670}
9671
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009672#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9673 void
9674set_no_hlsearch(int flag)
9675{
9676 no_hlsearch = flag;
9677# ifdef FEAT_EVAL
9678 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9679# endif
9680}
9681
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682/*
9683 * ":nohlsearch"
9684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009686ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009688 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009689 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691#endif
9692
9693#ifdef FEAT_CRYPT
9694/*
9695 * ":X": Get crypt key
9696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009698ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009700 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009701 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702}
9703#endif
9704
9705#ifdef FEAT_FOLDING
9706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009707ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
9709 if (foldManualAllowed(TRUE))
9710 foldCreate(eap->line1, eap->line2);
9711}
9712
9713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009714ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9717 eap->forceit, FALSE);
9718}
9719
9720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009721ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722{
9723 linenr_T lnum;
9724
Bram Moolenaar4facea32019-10-12 20:17:40 +02009725# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009726 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009727# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009728
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009729 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9731 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9732 ml_setmarked(lnum);
9733
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009734 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009736 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009737# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009738 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740}
9741#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009743#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009744/*
9745 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9746 * instead of a quickfix command.
9747 */
9748 int
9749is_loclist_cmd(int cmdidx)
9750{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009751 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009752 return FALSE;
9753 return cmdnames[cmdidx].cmd_name[0] == 'l';
9754}
9755#endif
9756
Bram Moolenaar4facea32019-10-12 20:17:40 +02009757#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009758 int
9759get_pressedreturn(void)
9760{
9761 return ex_pressedreturn;
9762}
9763
9764 void
9765set_pressedreturn(int val)
9766{
9767 ex_pressedreturn = val;
9768}
9769#endif