blob: 9a507c90fa38d219b0517c5ad3b48aa0ba0afe46 [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
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000270# define ex_class ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200272# define ex_continue ex_ni
273# define ex_debug ex_ni
274# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200275# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200281# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200282# define ex_endfunction ex_ni
283# define ex_endif ex_ni
284# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_endwhile ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000286# define ex_enum ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000290# define ex_incdec ex_ni
291# define ex_interface ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200292# define ex_finish ex_ni
293# define ex_function ex_ni
294# define ex_if ex_ni
295# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200296# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_lockvar ex_ni
298# define ex_oldfiles ex_ni
299# define ex_options ex_ni
300# define ex_packadd ex_ni
301# define ex_packloadall ex_ni
302# define ex_return ex_ni
303# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_throw ex_ni
305# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000306# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200308# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309# define ex_import ex_ni
310# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200312#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# define ex_loadview ex_ni
314#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200315#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_viminfo ex_ni
317#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_filetype(exarg_T *eap);
320static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000322# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323# define ex_diffpatch ex_ni
324# define ex_diffgetput ex_ni
325# define ex_diffsplit ex_ni
326# define ex_diffthis ex_ni
327# define ex_diffupdate ex_ni
328#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#else
333# define ex_nohlsearch ex_ni
334# define ex_match ex_ni
335#endif
336#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#else
339# define ex_X ex_ni
340#endif
341#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_fold(exarg_T *eap);
343static void ex_foldopen(exarg_T *eap);
344static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#else
346# define ex_fold ex_ni
347# define ex_foldopen ex_ni
348# define ex_folddo ex_ni
349#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100350#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351# define ex_language ex_ni
352#endif
353#ifndef FEAT_SIGNS
354# define ex_sign ex_ni
355#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200359# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar05159a02005-02-26 23:04:13 +0000362#ifndef FEAT_PROFILE
363# define ex_profile ex_ni
364#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200365#ifndef FEAT_TERMINAL
366# define ex_terminal ex_ni
367#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200368#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
369# define ex_xrestore ex_ni
370#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100371#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200372# define ex_popupclear ex_ni
373#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000374
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375/*
376 * Declare cmdnames[].
377 */
378#define DO_DECLARE_EXCMD
379#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200380#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static char_u dollar_command[2] = {'$', 0};
383
384
385#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100386// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387typedef struct
388{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100389 char_u *line; // command line
390 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391} wcmd_T;
392
393/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000394 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100400 garray_T *lines_gap; // growarray with line info
401 int current_line; // last read line from growarray
402 int repeating; // TRUE when looping a second time
403 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200404 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 void *cookie;
406};
407
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200408static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100409static int store_loop_line(garray_T *gap, char_u *line);
410static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000411
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100412// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000413struct dbg_stuff
414{
415 int trylevel;
416 int force_abort;
417 except_T *caught_stack;
418 char_u *vv_exception;
419 char_u *vv_throwpoint;
420 int did_emsg;
421 int got_int;
422 int did_throw;
423 int need_rethrow;
424 int check_cstack;
425 except_T *current_exception;
426};
427
Bram Moolenaared203462004-06-16 11:19:22 +0000428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100429save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000430{
431 dsp->trylevel = trylevel; trylevel = 0;
432 dsp->force_abort = force_abort; force_abort = FALSE;
433 dsp->caught_stack = caught_stack; caught_stack = NULL;
434 dsp->vv_exception = v_exception(NULL);
435 dsp->vv_throwpoint = v_throwpoint(NULL);
436
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100437 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000438 dsp->did_emsg = did_emsg; did_emsg = FALSE;
439 dsp->got_int = got_int; got_int = FALSE;
440 dsp->did_throw = did_throw; did_throw = FALSE;
441 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
442 dsp->check_cstack = check_cstack; check_cstack = FALSE;
443 dsp->current_exception = current_exception; current_exception = NULL;
444}
445
446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100447restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000448{
449 suppress_errthrow = FALSE;
450 trylevel = dsp->trylevel;
451 force_abort = dsp->force_abort;
452 caught_stack = dsp->caught_stack;
453 (void)v_exception(dsp->vv_exception);
454 (void)v_throwpoint(dsp->vv_throwpoint);
455 did_emsg = dsp->did_emsg;
456 got_int = dsp->got_int;
457 did_throw = dsp->did_throw;
458 need_rethrow = dsp->need_rethrow;
459 check_cstack = dsp->check_cstack;
460 current_exception = dsp->current_exception;
461}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462#endif
463
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464/*
465 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
466 * command is given.
467 */
468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100469do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100470 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471{
472 int save_msg_scroll;
473 int prev_msg_row;
474 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100475 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000476
477 if (improved)
478 exmode_active = EXMODE_VIM;
479 else
480 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100481 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100482 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000483
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100484 // When using ":global /pat/ visual" and then "Q" we return to continue
485 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000486 if (global_busy)
487 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
489 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 ++RedrawingDisabled; // don't redisplay the window
491 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100493 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 ++hold_gui_events;
495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496
Bram Moolenaar32526b32019-01-19 17:43:09 +0100497 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 while (exmode_active)
499 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100500 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000501 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
502 {
503 exmode_active = FALSE;
504 break;
505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 msg_scroll = TRUE;
507 need_wait_return = FALSE;
508 ex_pressedreturn = FALSE;
509 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100510 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 prev_msg_row = msg_row;
512 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (improved)
514 {
515 cmdline_row = msg_row;
516 do_cmdline(NULL, getexline, NULL, 0);
517 }
518 else
519 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
520 lines_left = Rows - 1;
521
Bram Moolenaardf177f62005-02-22 08:39:57 +0000522 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100523 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000526 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 if (ex_pressedreturn)
530 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 // go up one line, to overwrite the ":<CR>" line, so the
532 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 msg_row = prev_msg_row;
534 if (prev_msg_row == Rows - 1)
535 msg_row--;
536 }
537 msg_col = 0;
538 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
539 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100542 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000543 {
544 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000545 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000547 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 }
550
551#ifdef FEAT_GUI
552 --hold_gui_events;
553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 --RedrawingDisabled;
555 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100556 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 need_wait_return = FALSE;
558 msg_scroll = save_msg_scroll;
559}
560
561/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200562 * Print the executed command for when 'verbose' is set.
563 * When "lnum" is 0 only print the command.
564 */
565 static void
566msg_verbose_cmd(linenr_T lnum, char_u *cmd)
567{
568 ++no_wait_return;
569 verbose_enter_scroll();
570
571 if (lnum == 0)
572 smsg(_("Executing: %s"), cmd);
573 else
574 smsg(_("line %ld: %s"), (long)lnum, cmd);
575 if (msg_silent == 0)
576 msg_puts("\n"); // don't overwrite this
577
578 verbose_leave_scroll();
579 --no_wait_return;
580}
581
582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 * Execute a simple command line. Used for translated commands like "*".
584 */
585 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100586do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 return do_cmdline(cmd, NULL, NULL,
589 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
590}
591
592/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100593 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
594 * This allows for using a range without ":" in Vim9 script.
595 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200596 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100597do_cmd_argument(char_u *cmd)
598{
599 return do_cmdline(cmd, NULL, NULL,
600 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
601}
602
603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * do_cmdline(): execute one Ex command line
605 *
606 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100607 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * 2. Split up in parts separated with '|'.
609 *
610 * This function can be called recursively!
611 *
612 * flags:
613 * DOCMD_VERBOSE - The command will be included in the error message.
614 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100615 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 * DOCMD_KEYTYPED - Don't reset KeyTyped.
617 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
618 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
619 *
620 * return FAIL if cmdline could not be executed, OK otherwise
621 */
622 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623do_cmdline(
624 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200625 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100626 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100627 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 char_u *next_cmdline; // next cmd to execute
630 char_u *cmdline_copy = NULL; // copy of cmd line
631 int used_getline = FALSE; // used "fgetline" to obtain command
632 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 int count = 0; // line number count
635 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int retval = OK;
637#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100638 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 garray_T lines_ga; // keep lines for ":while"/":for"
640 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200641 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100642 char_u *fname = NULL; // function or script name
643 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
644 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
645 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200647 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200648 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200651 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000653 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000655 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100657# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658# define cmd_cookie cookie
659#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200661#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100662 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
663 // location for storing error messages to be converted to an exception.
664 // This ensures that the do_errthrow() call in do_one_cmd() does not
665 // combine the messages stored by an earlier invocation of do_one_cmd()
666 // with the command name of the later one. This would happen when
667 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 saved_msg_list = msg_list;
669 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#endif
671
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100672 // It's possible to create an endless loop with ":execute", catch that
673 // here. The value of 200 allows nested function calls, ":source", etc.
674 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100675 if (call_depth >= 200
676#ifdef FEAT_EVAL
677 && call_depth >= p_mfd
678#endif
679 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000681 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100683 // When converting to an exception, we do not include the command name
684 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100685 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 msg_list = saved_msg_list;
687#endif
688 return FAIL;
689 }
690 ++call_depth;
691
692#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100693 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000695 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100700 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ++ex_nesting_level;
703
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // Get the function or script name and the address where the next breakpoint
705 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 fname = func_name(real_cookie);
709 breakpoint = func_breakpoint(real_cookie);
710 dbg_tick = func_dbg_tick(real_cookie);
711 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100712 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100714 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 breakpoint = source_breakpoint(real_cookie);
716 dbg_tick = source_dbg_tick(real_cookie);
717 }
718
719 /*
720 * Initialize "force_abort" and "suppress_errthrow" at the top level.
721 */
722 if (!recursive)
723 {
724 force_abort = FALSE;
725 suppress_errthrow = FALSE;
726 }
727
728 /*
729 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 * exception handling (used when debugging). Otherwise clear it to avoid
731 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000733 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000734 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000735 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200736 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 initial_trylevel = trylevel;
739
740 /*
741 * "did_throw" will be set to TRUE when an exception is being thrown.
742 */
743 did_throw = FALSE;
744#endif
745 /*
746 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000747 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * If force_abort is set, we cancel everything.
749 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100750#ifdef FEAT_EVAL
751 did_emsg_cumul += did_emsg;
752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 did_emsg = FALSE;
754
755 /*
756 * KeyTyped is only set when calling vgetc(). Reset it here when not
757 * calling vgetc() (sourced command lines).
758 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 if (!(flags & DOCMD_KEYTYPED)
760 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 KeyTyped = FALSE;
762
763 /*
764 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000765 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 * - for multiple commands on one line, separated with '|'
767 * - when repeating until there are no more lines (for ":source")
768 */
769 next_cmdline = cmdline;
770 do
771 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100773 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774#endif
775
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100776 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (next_cmdline == NULL
778#ifdef FEAT_EVAL
779 && !force_abort
780 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100784 {
785#ifdef FEAT_EVAL
786 did_emsg_cumul += did_emsg;
787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000792 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100793 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * 3. If a line is given: Make a copy, so we can mess with it.
795 */
796
797#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100798 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100801 // Each '|' separated command is stored separately in lines_ga, to
802 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100803 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 // Check if a function has returned or, unless it has an unclosed
806 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000810 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811 func_line_end(real_cookie);
812# endif
813 if (func_has_ended(real_cookie))
814 {
815 retval = FAIL;
816 break;
817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000820 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822 script_line_end();
823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100825 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
828 retval = FAIL;
829 break;
830 }
831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (breakpoint != NULL && dbg_tick != NULL
834 && *dbg_tick != debug_tick)
835 {
836 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *dbg_tick = debug_tick;
840 }
841
842 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100845 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100847 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100850 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 *dbg_tick = debug_tick;
855 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000857 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000858 {
859 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100860 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862 script_line_start();
863 }
864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#endif
867
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100868 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (next_cmdline == NULL)
870 {
871 /*
872 * Need to set msg_didout for the first line after an ":if",
873 * otherwise the ":if" will be overwritten.
874 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100877 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#ifdef FEAT_EVAL
879 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
880#else
881 0
882#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100883 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
884 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100886 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100887 // returned for the end of a sourced file or executed function
888 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (KeyTyped && !(flags & DOCMD_REPEAT))
890 need_wait_return = FALSE;
891 retval = FAIL;
892 break;
893 }
894 used_getline = TRUE;
895
896 /*
897 * Keep the first typed line. Clear it when more lines are typed.
898 */
899 if (flags & DOCMD_KEEPLINE)
900 {
901 vim_free(repeat_cmdline);
902 if (count == 0)
903 repeat_cmdline = vim_strsave(next_cmdline);
904 else
905 repeat_cmdline = NULL;
906 }
907 }
908
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100909 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 else if (cmdline_copy == NULL)
911 {
912 next_cmdline = vim_strsave(next_cmdline);
913 if (next_cmdline == NULL)
914 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200915 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 retval = FAIL;
917 break;
918 }
919 }
920 cmdline_copy = next_cmdline;
921
922#ifdef FEAT_EVAL
923 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200924 * Inside a while/for loop, and when the command looks like a ":while"
925 * or ":for", the line is stored, because we may need it later when
926 * looping.
927 *
928 * When there is a '|' and another command, it is stored separately,
929 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000930 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200931 *
932 * Pass a different "fgetline" function to do_one_cmd() below,
933 * that it stores lines in or reads them from "lines_ga". Makes it
934 * possible to define a function inside a while/for loop and handles
935 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200939 cmd_getline = get_loop_line;
940 cmd_cookie = (void *)&cmd_loop_cookie;
941 cmd_loop_cookie.lines_gap = &lines_ga;
942 cmd_loop_cookie.current_line = current_line;
943 cmd_loop_cookie.getline = fgetline;
944 cmd_loop_cookie.cookie = cookie;
945 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
946
947 // Save the current line when encountering it the first time.
948 if (current_line == lines_ga.ga_len
949 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 {
951 retval = FAIL;
952 break;
953 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200954 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200956 else
957 {
958 cmd_getline = fgetline;
959 cmd_cookie = cookie;
960 }
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 did_endif = FALSE;
963#endif
964
965 if (count++ == 0)
966 {
967 /*
968 * All output from the commands is put below each other, without
969 * waiting for a return. Don't do this when executing commands
970 * from a script or when being called recursive (e.g. for ":e
971 * +command file").
972 */
973 if (!(flags & DOCMD_NOWAIT) && !recursive)
974 {
975 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100976 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100978 msg_scroll = TRUE; // put messages below each other
979 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 ++RedrawingDisabled;
981 did_inc = TRUE;
982 }
983 }
984
Bram Moolenaar823654b2020-05-29 23:03:09 +0200985 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100986 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 /*
989 * 2. Execute one '|' separated command.
990 * do_one_cmd() will return NULL if there is no trailing '|'.
991 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
992 */
993 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100994 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#ifdef FEAT_EVAL
996 &cstack,
997#endif
998 cmd_getline, cmd_cookie);
999 --recursive;
1000
1001#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001002 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001003 // Use "current_line" from "cmd_loop_cookie", it may have been
1004 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001005 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#endif
1007
1008 if (next_cmdline == NULL)
1009 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001010 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 /*
1013 * If the command was typed, remember it for the ':' register.
1014 * Do this AFTER executing the command to make :@: work.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 && new_last_cmdline != NULL)
1018 {
1019 vim_free(last_cmdline);
1020 last_cmdline = new_last_cmdline;
1021 new_last_cmdline = NULL;
1022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 else
1025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001026 // need to copy the command after the '|' to cmdline_copy, for the
1027 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001028 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 next_cmdline = cmdline_copy;
1030 }
1031
1032
1033#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001034 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001036 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001038 {
1039 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
1045 ++current_line;
1046
1047 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 * An ":endwhile", ":endfor" and ":continue" is handled here.
1049 * If we were executing commands, jump back to the ":while" or
1050 * ":for".
1051 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Jump back to the matching ":while" or ":for". Be careful
1058 // not to use a cs_line[] from an entry that isn't a ":while"
1059 // or ":for": It would make "current_line" invalid and can
1060 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (!did_emsg && !got_int && !did_throw
1062 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001063 && (cstack.cs_flags[cstack.cs_idx]
1064 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 && cstack.cs_line[cstack.cs_idx] >= 0
1066 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1067 {
1068 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001073 // Check for the next breakpoint at or after the ":while"
1074 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001075 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
1077 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001078 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 fname,
1080 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1081 *dbg_tick = debug_tick;
1082 }
1083 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001086 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001088 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1089 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091 }
1092
1093 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001098 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001099 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
1101 }
1102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001103 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001104 if (breakpoint != NULL && has_watchexpr())
1105 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001106 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001107 *dbg_tick = debug_tick;
1108 }
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 /*
1111 * When not inside any ":while" loop, clear remembered lines.
1112 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
1115 if (lines_ga.ga_len > 0)
1116 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001117 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1119 free_cmdlines(&lines_ga);
1120 }
1121 current_line = 0;
1122 }
1123
1124 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001125 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1126 * being restored at the ":endtry". Reset them here and set the
1127 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1128 * This includes the case where a missing ":endif", ":endwhile" or
1129 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 cstack.cs_lflags &= ~CSL_HAD_FINA;
1134 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1135 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 did_throw ? (void *)current_exception : NULL);
1137 did_emsg = got_int = did_throw = FALSE;
1138 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1139 }
1140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 // Update global "trylevel" for recursive calls to do_cmdline() from
1142 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 trylevel = initial_trylevel + cstack.cs_trylevel;
1144
1145 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001146 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 * files) is aborted because of an error, an interrupt, or an uncaught
1148 * exception, cancel everything. If it is left normally, reset
1149 * force_abort to get the non-EH compatible abortion behavior for
1150 * the rest of the script.
1151 */
1152 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1153 force_abort = FALSE;
1154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001155 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001157#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
1159 }
1160 /*
1161 * Continue executing command lines when:
1162 * - no CTRL-C typed, no aborting error, no exception thrown or try
1163 * conditionals need to be checked for executing finally clauses or
1164 * catching an interrupt exception
1165 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 * looping for ":source" command or function call.
1168 */
1169 while (!((got_int
1170#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001171 || (did_emsg && (force_abort || in_vim9script()))
1172 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174 )
1175#ifdef FEAT_EVAL
1176 && cstack.cs_trylevel == 0
1177#endif
1178 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 && !(did_emsg
1180#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001181 // Keep going when inside try/catch, so that the error can be
1182 // deal with, except when it is a syntax error, it may cause
1183 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001184 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1185#endif
1186 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001187 && (getline_equal(fgetline, cookie, getexmodeline)
1188 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 && (next_cmdline != NULL
1190#ifdef FEAT_EVAL
1191 || cstack.cs_idx >= 0
1192#endif
1193 || (flags & DOCMD_REPEAT)));
1194
1195 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001196 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#ifdef FEAT_EVAL
1198 free_cmdlines(&lines_ga);
1199 ga_clear(&lines_ga);
1200
1201 if (cstack.cs_idx >= 0)
1202 {
1203 /*
1204 * If a sourced file or executed function ran to its end, report the
1205 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001206 * In Vim9 script do not give a second error, executing aborts after
1207 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001209 if (!got_int && !did_throw && !aborting()
1210 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001211 && ((getline_equal(fgetline, cookie, getsourceline)
1212 && !source_finished(fgetline, cookie))
1213 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 && !func_has_ended(real_cookie))))
1215 {
1216 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001217 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001219 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001221 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001223 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225
1226 /*
1227 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1228 * ":endtry" in a sourced file or executed function. If the try
1229 * conditional is in its finally clause, ignore anything pending.
1230 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001231 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 */
1233 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001234 {
1235 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1236
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001237 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001238 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001239 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1240 &cstack.cs_looplevel);
1241 }
1242 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 trylevel = initial_trylevel;
1244 }
1245
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001246 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1247 // lack was reported above and the error message is to be converted to an
1248 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001249 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 ? (char_u *)"endfunction" : (char_u *)NULL);
1251
1252 if (trylevel == 0)
1253 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001254 // Just in case did_throw got set but current_exception wasn't.
1255 if (current_exception == NULL)
1256 did_throw = FALSE;
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 /*
1259 * When an exception is being thrown out of the outermost try
1260 * conditional, discard the uncaught exception, disable the conversion
1261 * of interrupts or errors to exceptions, and ensure that no more
1262 * commands are executed.
1263 */
1264 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001265 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267 /*
1268 * On an interrupt or an aborting error not converted to an exception,
1269 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001270 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 * when force_abort is set and did_emsg unset in case of an interrupt
1272 * from a finally clause after an error.
1273 */
1274 else if (got_int || (did_emsg && force_abort))
1275 suppress_errthrow = TRUE;
1276 }
1277
1278 /*
1279 * The current cstack will be freed when do_cmdline() returns. An uncaught
1280 * exception will have to be rethrown in the previous cstack. If a function
1281 * has just returned or a script file was just finished and the previous
1282 * cstack belongs to the same function or, respectively, script file, it
1283 * will have to be checked for finally clauses to be executed due to the
1284 * ":return" or ":finish". This is done in do_one_cmd().
1285 */
1286 if (did_throw)
1287 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001288 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001290 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 && ex_nesting_level > func_level(real_cookie) + 1))
1292 {
1293 if (!did_throw)
1294 check_cstack = TRUE;
1295 }
1296 else
1297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001298 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001299 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 --ex_nesting_level;
1301 /*
1302 * Go to debug mode when returning from a function in which we are
1303 * single-stepping.
1304 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001305 if ((getline_equal(fgetline, cookie, getsourceline)
1306 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001308 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 ? (char_u *)_("End of sourced file")
1310 : (char_u *)_("End of function"));
1311 }
1312
1313 /*
1314 * Restore the exception environment (done after returning from the
1315 * debugger).
1316 */
1317 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001318 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001321
1322 // Cleanup if "cs_emsg_silent_list" remains.
1323 if (cstack.cs_emsg_silent_list != NULL)
1324 {
1325 eslist_T *elem, *temp;
1326
1327 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1328 {
1329 temp = elem->next;
1330 vim_free(elem);
1331 }
1332 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001333#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
1335 /*
1336 * If there was too much output to fit on the command line, ask the user to
1337 * hit return before redrawing the screen. With the ":global" command we do
1338 * this only once after the command is finished.
1339 */
1340 if (did_inc)
1341 {
1342 --RedrawingDisabled;
1343 --no_wait_return;
1344 msg_scroll = FALSE;
1345
1346 /*
1347 * When just finished an ":if"-":else" which was typed, no need to
1348 * wait for hit-return. Also for an error situation.
1349 */
1350 if (retval == FAIL
1351#ifdef FEAT_EVAL
1352 || (did_endif && KeyTyped && !did_emsg)
1353#endif
1354 )
1355 {
1356 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001357 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 else if (need_wait_return)
1360 {
1361 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001362 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 * here should not overwrite the command that may be shown before
1364 * doing that.
1365 */
1366 msg_didout |= msg_didout_before_start;
1367 wait_return(FALSE);
1368 }
1369 }
1370
Bram Moolenaar2a942252012-11-28 23:03:07 +01001371#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001372 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001373#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 /*
1375 * Reset if_level, in case a sourced script file contains more ":if" than
1376 * ":endif" (could be ":if x | foo | endif").
1377 */
1378 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001379#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 --call_depth;
1382 return retval;
1383}
1384
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001385#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001386/*
1387 * Handle when "did_throw" is set after executing commands.
1388 */
1389 void
1390handle_did_throw()
1391{
1392 char *p = NULL;
1393 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001394 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001395
1396 /*
1397 * If the uncaught exception is a user exception, report it as an
1398 * error. If it is an error exception, display the saved error
1399 * message now. For an interrupt exception, do nothing; the
1400 * interrupt message is given elsewhere.
1401 */
1402 switch (current_exception->type)
1403 {
1404 case ET_USER:
1405 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001406 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001407 current_exception->value);
1408 p = (char *)vim_strsave(IObuff);
1409 break;
1410 case ET_ERROR:
1411 messages = current_exception->messages;
1412 current_exception->messages = NULL;
1413 break;
1414 case ET_INTERRUPT:
1415 break;
1416 }
1417
1418 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1419 current_exception->throw_lnum);
1420 ESTACK_CHECK_SETUP
1421 current_exception->throw_name = NULL;
1422
1423 discard_current_exception(); // uses IObuff if 'verbose'
1424 suppress_errthrow = TRUE;
1425 force_abort = TRUE;
1426
1427 if (messages != NULL)
1428 {
1429 do
1430 {
1431 msglist_T *next = messages->next;
1432 int save_compiling = estack_compiling;
1433
1434 estack_compiling = messages->msg_compiling;
1435 emsg(messages->msg);
1436 vim_free(messages->msg);
1437 vim_free(messages->sfile);
1438 vim_free(messages);
1439 messages = next;
1440 estack_compiling = save_compiling;
1441 }
1442 while (messages != NULL);
1443 }
1444 else if (p != NULL)
1445 {
1446 emsg(p);
1447 vim_free(p);
1448 }
1449 vim_free(SOURCING_NAME);
1450 ESTACK_CHECK_NOW
1451 estack_pop();
1452}
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001455 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 */
1457 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001458get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001460 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 wcmd_T *wp;
1462 char_u *line;
1463
1464 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1465 {
1466 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001467 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001469 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001471 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001473 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001474 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 ++cp->current_line;
1476
1477 return line;
1478 }
1479
1480 KeyTyped = FALSE;
1481 ++cp->current_line;
1482 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001483 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 return vim_strsave(wp->line);
1485}
1486
1487/*
1488 * Store a line in "gap" so that a ":while" loop can execute it again.
1489 */
1490 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001491store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493 if (ga_grow(gap, 1) == FAIL)
1494 return FAIL;
1495 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001496 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 return OK;
1499}
1500
1501/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001502 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 */
1504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001505free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
1507 while (gap->ga_len > 0)
1508 {
1509 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1510 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512}
1513#endif
1514
1515/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001516 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1517 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001520getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001521 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001523 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001526 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001529 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1530 // function that's originally used to obtain the lines. This may be
1531 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001532 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533 cp = (struct loop_cookie *)cookie;
1534 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 {
1536 gp = cp->getline;
1537 cp = cp->cookie;
1538 }
1539 return gp == func;
1540#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001541 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#endif
1543}
1544
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001546 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 * getline function. Otherwise return "cookie".
1548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001550getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001551 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001552 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
Bram Moolenaar13505972019-01-24 15:04:48 +01001554#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001555 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001556 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001558 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1559 // cookie that's originally used to obtain the lines. This may be nested
1560 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001561 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 cp = (struct loop_cookie *)cookie;
1563 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
1565 gp = cp->getline;
1566 cp = cp->cookie;
1567 }
1568 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001569#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001572}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaard5053d02020-06-28 15:51:16 +02001574#if defined(FEAT_EVAL) || defined(PROT)
1575/*
1576 * Get the next line source line without advancing.
1577 */
1578 char_u *
1579getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001580 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001581 void *cookie) // argument for fgetline()
1582{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001583 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001584 struct loop_cookie *cp;
1585 wcmd_T *wp;
1586
1587 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1588 // cookie that's originally used to obtain the lines. This may be nested
1589 // several levels.
1590 gp = fgetline;
1591 cp = (struct loop_cookie *)cookie;
1592 while (gp == get_loop_line)
1593 {
1594 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1595 {
1596 // executing lines a second time, use the stored copy
1597 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1598 return wp->line;
1599 }
1600 gp = cp->getline;
1601 cp = cp->cookie;
1602 }
1603 if (gp == getsourceline)
1604 return source_nextline(cp);
1605 return NULL;
1606}
1607#endif
1608
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001609
1610/*
1611 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1612 * ":bwipeout", etc.
1613 * Returns the buffer number.
1614 */
1615 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001616compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617{
1618 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001619 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001620 int count = offset;
1621
1622 buf = firstbuf;
1623 while (buf->b_next != NULL && buf->b_fnum < lnum)
1624 buf = buf->b_next;
1625 while (count != 0)
1626 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001627 count += (offset < 0) ? 1 : -1;
1628 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1629 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001631 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001632 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001633 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001634 while (buf->b_ml.ml_mfp == NULL)
1635 {
1636 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1637 if (nextbuf == NULL)
1638 break;
1639 buf = nextbuf;
1640 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001641 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001642 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001643 if (addr_type == ADDR_LOADED_BUFFERS)
1644 while (buf->b_ml.ml_mfp == NULL)
1645 {
1646 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1647 if (nextbuf == NULL)
1648 break;
1649 buf = nextbuf;
1650 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001651 return buf->b_fnum;
1652}
1653
Bram Moolenaarb7316892019-05-01 18:08:42 +02001654/*
1655 * Return the window number of "win".
1656 * When "win" is NULL return the number of windows.
1657 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001658 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001659current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001660{
1661 win_T *wp;
1662 int nr = 0;
1663
Bram Moolenaar29323592016-07-24 22:04:11 +02001664 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001665 {
1666 ++nr;
1667 if (wp == win)
1668 break;
1669 }
1670 return nr;
1671}
1672
1673 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001674current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001675{
1676 tabpage_T *tp;
1677 int nr = 0;
1678
Bram Moolenaar29323592016-07-24 22:04:11 +02001679 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001680 {
1681 ++nr;
1682 if (tp == tab)
1683 break;
1684 }
1685 return nr;
1686}
1687
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001688 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001689comment_start(char_u *p, int starts_with_colon UNUSED)
1690{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001691 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001692 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001693 return *p == '"';
1694}
1695
Bram Moolenaarf240e182014-11-27 18:33:02 +01001696# define CURRENT_WIN_NR current_win_nr(curwin)
1697# define LAST_WIN_NR current_win_nr(NULL)
1698# define CURRENT_TAB_NR current_tab_nr(curtab)
1699# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/*
1702 * Execute one Ex command.
1703 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001704 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1705 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 *
1707 * 1. skip comment lines and leading space
1708 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001709 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001711 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001712 * 6. parse arguments
1713 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001715 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 *
1717 * This function may be called recursively!
1718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001720do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001721 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001722 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001724 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001726 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001729 char_u *p;
1730 linenr_T lnum;
1731 long n;
1732 char *errormsg = NULL; // error message
1733 char_u *after_modifier = NULL;
1734 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001735 cmdmod_T save_cmdmod;
1736 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001737 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001738 int ni; // set when Not Implemented
1739 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001740 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001741 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001742#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001743 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001744#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001745 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001746 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaara80faa82020-04-12 19:37:17 +02001748 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ea.line1 = 1;
1750 ea.line2 = 1;
1751#ifdef FEAT_EVAL
1752 ++ex_nesting_level;
1753#endif
1754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (quitmore
1757#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001759 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001760#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001762 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 --quitmore;
1764
1765 /*
1766 * Reset browse, confirm, etc.. They are restored when returning, for
1767 * recursive calls.
1768 */
1769 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001771 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001772 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1773 goto doend;
1774
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001775/*
1776 * 1. Skip comment lines and leading white space and colons.
1777 * 2. Handle command modifiers.
1778 */
1779 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001781 ea.cmdlinep = cmdlinep;
1782 ea.getline = fgetline;
1783 ea.cookie = cookie;
1784#ifdef FEAT_EVAL
1785 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001786 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001788 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001789 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001790 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001791 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793#ifdef FEAT_EVAL
1794 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1795 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1796#else
1797 ea.skip = (if_level > 0);
1798#endif
1799
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001801 * 3. Skip over the range to find the command. Let "p" point to after it.
1802 *
1803 * We need the command to know what kind of range it uses.
1804 */
1805 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001806
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001807 // In Vim9 script a colon is required before the range. This may also be
1808 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001809 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001810 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001811 {
1812 may_have_range = FALSE;
1813 for (p = ea.cmd; p >= *cmdlinep; --p)
1814 {
1815 if (*p == ':')
1816 may_have_range = TRUE;
1817 if (p < ea.cmd && !VIM_ISWHITE(*p))
1818 break;
1819 }
1820 }
1821 else
1822 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001823 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001824 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001826#ifdef FEAT_EVAL
1827 // Handle ":export" - it functions almost like a command modifier.
1828 // ":export var Name: type"
1829 // ":export def Name(..."
1830 // etc.
1831 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1832 is_export = TRUE;
1833#endif
1834
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001835 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001836 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001837 if (ea.cmd == cmd + 1 && *cmd == '$')
1838 // should be "$VAR = val"
1839 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001840#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001841 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001842#else
1843 p = find_ex_command(&ea, NULL, NULL, NULL);
1844#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001845 if (ea.cmdidx == CMD_SIZE)
1846 {
1847 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1848
1849 // If a ':' before the range is missing, give a clearer error
1850 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001851 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001852 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001853 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001854 goto doend;
1855 }
1856 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001860
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001861#ifdef FEAT_EVAL
1862# ifdef FEAT_PROFILE
1863 // Count this line for profiling if skip is TRUE.
1864 if (do_profiling == PROF_YES
1865 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1866 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1867 {
1868 int skip = did_emsg || got_int || did_throw;
1869
1870 if (ea.cmdidx == CMD_catch)
1871 skip = !skip && !(cstack->cs_idx >= 0
1872 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1873 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1874 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1875 skip = skip || !(cstack->cs_idx >= 0
1876 && !(cstack->cs_flags[cstack->cs_idx]
1877 & (CSF_ACTIVE | CSF_TRUE)));
1878 else if (ea.cmdidx == CMD_finally)
1879 skip = FALSE;
1880 else if (ea.cmdidx != CMD_endif
1881 && ea.cmdidx != CMD_endfor
1882 && ea.cmdidx != CMD_endtry
1883 && ea.cmdidx != CMD_endwhile)
1884 skip = ea.skip;
1885
1886 if (!skip)
1887 {
1888 if (getline_equal(fgetline, cookie, get_func_line))
1889 func_line_exec(getline_cookie(fgetline, cookie));
1890 else if (getline_equal(fgetline, cookie, getsourceline))
1891 script_line_exec();
1892 }
1893 }
1894# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001895#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001896
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001897 ea.cmd = cmd;
1898
1899#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001900 // May go to debug mode. If this happens and the ">quit" debug command is
1901 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001902 dbg_check_breakpoint(&ea);
1903 if (!ea.skip && got_int)
1904 {
1905 ea.skip = TRUE;
1906 (void)do_intthrow(cstack);
1907 }
1908#endif
1909
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001910/*
1911 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 *
1913 * where 'addr' is:
1914 *
1915 * % (entire file)
1916 * $ [+-NUM]
1917 * 'x [+-NUM] (where x denotes a currently defined mark)
1918 * . [+-NUM]
1919 * [+-NUM]..
1920 * NUM
1921 *
1922 * The ea.cmd pointer is updated to point to the first character following the
1923 * range spec. If an initial address is found, but no second, the upper bound
1924 * is equal to the lower.
1925 */
1926
Bram Moolenaar25190db2019-05-04 15:05:28 +02001927 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001928 if (!IS_USER_CMDIDX(ea.cmdidx))
1929 {
1930 if (ea.cmdidx != CMD_SIZE)
1931 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1932 else
1933 ea.addr_type = ADDR_LINES;
1934
Bram Moolenaar25190db2019-05-04 15:05:28 +02001935 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001936 if (ea.cmdidx == CMD_wincmd && p != NULL)
1937 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001938#ifdef FEAT_QUICKFIX
1939 // :.cc in quickfix window uses line number
1940 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1941 ea.addr_type = ADDR_OTHER;
1942#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001943 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001944
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001945 if (!may_have_range)
1946 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001947 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1948 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001951 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 */
1953
1954 /*
1955 * Skip ':' and any white space
1956 */
1957 ea.cmd = skipwhite(ea.cmd);
1958 while (*ea.cmd == ':')
1959 ea.cmd = skipwhite(ea.cmd + 1);
1960
1961 /*
1962 * If we got a line, but no command, then go to the line.
1963 * If we find a '|' or '\n' we set ea.nextcmd.
1964 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001965 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1966 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 /*
1969 * strange vi behaviour:
1970 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001971 * ":3|..." prints line 3 (not in Vim9 script)
1972 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001974 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001976 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 goto doend;
1978 }
1979
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001980 // If this looks like an undefined user command and there are CmdUndefined
1981 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001982 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1983 && ASCII_ISUPPER(*ea.cmd)
1984 && has_cmdundefined())
1985 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001986 int ret;
1987
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001988 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001989 while (ASCII_ISALNUM(*p))
1990 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001991 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001992 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1993 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001994 // If the autocommands did something and didn't cause an error, try
1995 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001996 p = (ret
1997#ifdef FEAT_EVAL
1998 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001999#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002001 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (p == NULL)
2004 {
2005 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002006 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 goto doend;
2008 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002009 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002010 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2011 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
2013 errormsg = uc_fun_cmd();
2014 goto doend;
2015 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (ea.cmdidx == CMD_SIZE)
2018 {
2019 if (!ea.skip)
2020 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002021 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002024 // If the modifier was parsed OK the error must be in the
2025 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002026 if (after_modifier != NULL)
2027 append_command(after_modifier);
2028 else
2029 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002030 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002031 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002032 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002033 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035 goto doend;
2036 }
2037
Bram Moolenaar958636c2014-10-21 20:01:58 +02002038 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2039 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002040#ifdef HAVE_EX_SCRIPT_NI
2041 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2042#endif
2043 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045#ifndef FEAT_EVAL
2046 /*
2047 * When the expression evaluation is disabled, recognize the ":if" and
2048 * ":endif" commands and ignore everything in between it.
2049 */
2050 if (ea.cmdidx == CMD_if)
2051 ++if_level;
2052 if (if_level)
2053 {
2054 if (ea.cmdidx == CMD_endif)
2055 --if_level;
2056 goto doend;
2057 }
2058
2059#endif
2060
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002061 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002062 if (*p == '!' && ea.cmdidx != CMD_substitute
2063 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 ++p;
2066 ea.forceit = TRUE;
2067 }
2068 else
2069 ea.forceit = FALSE;
2070
2071/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002072 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002074 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002075 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
2077 if (!ea.skip)
2078 {
2079#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002080 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002082 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002083 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 goto doend;
2085 }
2086#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002087 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002088 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002089 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002090 goto doend;
2091 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002092 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002094 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002095 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 goto doend;
2097 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002098
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002099 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002101 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2102 {
2103 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002104 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002105 goto doend;
2106 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002107 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2108 {
2109 // Command not allowed when text is locked
2110 errormsg = _(get_text_locked_msg());
2111 goto doend;
2112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002115 // Disallow editing another buffer when "curbuf_lock" is set.
2116 // Do allow ":checktime" (it is postponed).
2117 // Do allow ":edit" (check for an argument later).
2118 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002119 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002120 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002121 && ea.cmdidx != CMD_edit
2122 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002123 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002124 && curbuf_locked())
2125 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002127 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002129 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 goto doend;
2131 }
2132 }
2133
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002134 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002136 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 goto doend;
2138 }
2139
2140 /*
2141 * Don't complain about the range if it is not used
2142 * (could happen if line_count is accidentally set to 0).
2143 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002144 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 /*
2147 * If the range is backwards, ask for confirmation and, if given, swap
2148 * ea.line1 & ea.line2 so it's forwards again.
2149 * When global command is busy, don't ask, will fail below.
2150 */
2151 if (!global_busy && ea.line1 > ea.line2)
2152 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002153 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002155 if (sourcing || exmode_active)
2156 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002157 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002158 goto doend;
2159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (ask_yesno((char_u *)
2161 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002162 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 lnum = ea.line1;
2165 ea.line1 = ea.line2;
2166 ea.line2 = lnum;
2167 }
2168 if ((errormsg = invalid_range(&ea)) != NULL)
2169 goto doend;
2170 }
2171
Bram Moolenaarb7316892019-05-01 18:08:42 +02002172 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2173 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 ea.line2 = 1;
2175
2176 correct_range(&ea);
2177
2178#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002179 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002180 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002182 // Put the first line at the start of a closed fold, put the last line
2183 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 (void)hasFolding(ea.line1, &ea.line1, NULL);
2185 (void)hasFolding(ea.line2, NULL, &ea.line2);
2186 }
2187#endif
2188
2189#ifdef FEAT_QUICKFIX
2190 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002191 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 * option here, so things like % get expanded.
2193 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002194 p = replace_makeprg(&ea, p, cmdlinep);
2195 if (p == NULL)
2196 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197#endif
2198
2199 /*
2200 * Skip to start of argument.
2201 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2202 */
2203 if (ea.cmdidx == CMD_bang)
2204 ea.arg = p;
2205 else
2206 ea.arg = skipwhite(p);
2207
Bram Moolenaar379fb762018-08-30 15:58:28 +02002208 // ":file" cannot be run with an argument when "curbuf_lock" is set
2209 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2210 goto doend;
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 /*
2213 * Check for "++opt=val" argument.
2214 * Must be first, allow ":w ++enc=utf8 !cmd"
2215 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002216 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2218 if (getargopt(&ea) == FAIL && !ni)
2219 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002220 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 goto doend;
2222 }
2223
2224 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002226 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002228 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002230 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 goto doend;
2232 }
2233 ea.arg = skipwhite(ea.arg + 1);
2234 ea.append = TRUE;
2235 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002236 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
2238 ++ea.arg;
2239 ea.usefilter = TRUE;
2240 }
2241 }
2242
2243 if (ea.cmdidx == CMD_read)
2244 {
2245 if (ea.forceit)
2246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 ea.forceit = FALSE;
2249 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002250 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
2252 ++ea.arg;
2253 ea.usefilter = TRUE;
2254 }
2255 }
2256
2257 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2258 {
2259 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002260 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
2262 ++ea.arg;
2263 ++ea.amount;
2264 }
2265 ea.arg = skipwhite(ea.arg);
2266 }
2267
2268 /*
2269 * Check for "+command" argument, before checking for next command.
2270 * Don't do this for ":read !cmd" and ":write !cmd".
2271 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002272 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2274
2275 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002276 * For commands that do not use '|' inside their argument: Check for '|' to
2277 * separate commands and '"' or '#' to start comments.
2278 *
2279 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002280 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002281 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002282 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002284 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2285 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002286 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002287 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002288 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002289 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002290 || ea.cmdidx == CMD_global
2291 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002292 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002293#ifdef FEAT_EVAL
2294 || inside_block(&ea)
2295#endif
2296 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 for (p = ea.arg; *p; ++p)
2299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002300 // Remove one backslash before a newline, so that it's possible to
2301 // pass a newline to the shell and also a newline that is preceded
2302 // with a backslash. This makes it impossible to end a shell
2303 // command in a backslash, but that doesn't appear useful.
2304 // Halving the number of backslashes is incompatible with previous
2305 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002307 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002308 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 ea.nextcmd = p + 1;
2311 *p = NUL;
2312 break;
2313 }
2314 }
2315 }
2316
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002318 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002320 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002321 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002323 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002324 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002325 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002327#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002328 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002329 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002331 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002332 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002335 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2336 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002337 {
2338 ea.regname = *ea.arg++;
2339#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002340 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002341 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2342 {
kuuote08d7b1c2021-10-04 22:17:36 +01002343 if (!ea.skip)
2344 {
2345 set_expr_line(vim_strsave(ea.arg), &ea);
2346 did_set_expr_line = TRUE;
2347 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002348 ea.arg += STRLEN(ea.arg);
2349 }
2350#endif
2351 ea.arg = skipwhite(ea.arg);
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354
2355 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002356 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 * count, it's a buffer name.
2358 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002360 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002361 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002363 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002365 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002367 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 goto doend;
2369 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002370 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 ea.line2 = n;
2373 if (ea.addr_count == 0)
2374 ea.addr_count = 1;
2375 }
2376 else
2377 {
2378 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002379 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002380 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002381 else
2382 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 ++ea.addr_count;
2384 /*
2385 * Be vi compatible: no error message for out of range.
2386 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002387 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 ea.line2 = curbuf->b_ml.ml_line_count;
2389 }
2390 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002391
2392 /*
2393 * Check for flags: 'l', 'p' and '#'.
2394 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002395 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002396 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2398 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002400 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002401 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 goto doend;
2403 }
2404
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002405 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002407 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 goto doend;
2409 }
2410
2411#ifdef FEAT_EVAL
2412 /*
2413 * Skip the command when it's not going to be executed.
2414 * The commands like :if, :endif, etc. always need to be executed.
2415 * Also make an exception for commands that handle a trailing command
2416 * themselves.
2417 */
2418 if (ea.skip)
2419 {
2420 switch (ea.cmdidx)
2421 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002422 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 case CMD_while:
2424 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002425 case CMD_for:
2426 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 case CMD_if:
2428 case CMD_elseif:
2429 case CMD_else:
2430 case CMD_endif:
2431 case CMD_try:
2432 case CMD_catch:
2433 case CMD_finally:
2434 case CMD_endtry:
2435 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002436 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 break;
2438
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002439 // Commands that handle '|' themselves. Check: A command should
2440 // either have the EX_TRLBAR flag, appear in this list or appear in
2441 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 case CMD_aboveleft:
2443 case CMD_and:
2444 case CMD_belowright:
2445 case CMD_botright:
2446 case CMD_browse:
2447 case CMD_call:
2448 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002449 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 case CMD_delfunction:
2451 case CMD_djump:
2452 case CMD_dlist:
2453 case CMD_dsearch:
2454 case CMD_dsplit:
2455 case CMD_echo:
2456 case CMD_echoerr:
2457 case CMD_echomsg:
2458 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002459 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002461 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002462 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_help:
2464 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002465 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_ijump:
2467 case CMD_ilist:
2468 case CMD_isearch:
2469 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002470 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 case CMD_keepjumps:
2472 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002473 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 case CMD_leftabove:
2475 case CMD_let:
2476 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002477 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002478 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002480 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002481 case CMD_noautocmd:
2482 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 case CMD_perl:
2484 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002485 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002486 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002487 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 case CMD_return:
2489 case CMD_rightbelow:
2490 case CMD_ruby:
2491 case CMD_silent:
2492 case CMD_smagic:
2493 case CMD_snomagic:
2494 case CMD_substitute:
2495 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002496 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 case CMD_tcl:
2498 case CMD_throw:
2499 case CMD_tilde:
2500 case CMD_topleft:
2501 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002502 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002503 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 case CMD_verbose:
2505 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002506 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 break;
2508
2509 default: goto doend;
2510 }
2511 }
2512#endif
2513
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002514 if ((ea.argt & EX_XFILE)
2515 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2516 goto doend;
2517
2518#ifdef FEAT_EVAL
2519 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002521 emsg(_(e_invalid_command_after_export));
2522 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 /*
2527 * Accept buffer name. Cannot be used at the same time with a buffer
2528 * number. Don't do this for a user command.
2529 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002530 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002531 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 /*
2534 * :bdelete, :bwipeout and :bunload take several arguments, separated
2535 * by spaces: find next space (skipping over escaped characters).
2536 * The others take one argument: ignore trailing spaces.
2537 */
2538 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2539 || ea.cmdidx == CMD_bunload)
2540 p = skiptowhite_esc(ea.arg);
2541 else
2542 {
2543 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002544 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 --p;
2546 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002547 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002548 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002549 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 goto doend;
2551 ea.addr_count = 1;
2552 ea.arg = skipwhite(p);
2553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002555 // The :try command saves the emsg_silent flag, reset it here when
2556 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002557 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002558 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002559 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002560 if (emsg_silent < 0)
2561 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002562 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002563 }
2564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002566 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar958636c2014-10-21 20:01:58 +02002569 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
2571 /*
2572 * Execute a user-defined command.
2573 */
2574 do_ucmd(&ea);
2575 }
2576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
2578 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002579 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2582 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002583 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585
2586#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002587 // A command will reset "is_export" when exporting an item. If it is still
2588 // set something went wrong.
2589 if (is_export)
2590 {
2591 if (errormsg == NULL)
2592 errormsg = _(e_export_with_invalid_argument);
2593 is_export = FALSE;
2594 }
2595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002597 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002598 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002599 && current_sctx.sc_sid > 0
2600 && ea.cmdidx != CMD_endif
2601 && (cstack->cs_idx < 0
2602 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002603 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 /*
2606 * If the command just executed called do_cmdline(), any throw or ":return"
2607 * or ":finish" encountered there must also check the cstack of the still
2608 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2609 * exception, or reanimate a returned function or finished script file and
2610 * return or finish it again.
2611 */
2612 if (need_rethrow)
2613 do_throw(cstack);
2614 else if (check_cstack)
2615 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002616 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002618 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 && current_func_returned())
2620 do_return(&ea, TRUE, FALSE, NULL);
2621 }
2622 need_rethrow = check_cstack = FALSE;
2623#endif
2624
2625doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002626 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002629 curwin->w_cursor.col = 0;
2630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
2632 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2633 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002634 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002636 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
2638 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002639 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002641 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643 emsg(errormsg);
2644 }
2645#ifdef FEAT_EVAL
2646 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002647 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2648 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002649
2650 if (did_set_expr_line)
2651 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002652 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653#endif
2654
Bram Moolenaare1004402020-10-24 20:49:43 +02002655 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002657 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002658 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002660 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 ea.nextcmd = NULL;
2662
2663#ifdef FEAT_EVAL
2664 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002665 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666#endif
2667
2668 return ea.nextcmd;
2669}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002671static char ex_error_buf[MSG_BUF_LEN];
2672
2673/*
2674 * Return an error message with argument included.
2675 * Uses a static buffer, only the last error will be kept.
2676 * "msg" will be translated, caller should use N_().
2677 */
2678 char *
2679ex_errmsg(char *msg, char_u *arg)
2680{
2681 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2682 return ex_error_buf;
2683}
2684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002686 * Handle a range without a command.
2687 * Returns an error message on failure.
2688 */
2689 char *
2690ex_range_without_command(exarg_T *eap)
2691{
2692 char *errormsg = NULL;
2693
2694 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2695#ifdef FEAT_EVAL
2696 && !in_vim9script()
2697#endif
2698 )
2699 {
2700 eap->cmdidx = CMD_print;
2701 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2702 if ((errormsg = invalid_range(eap)) == NULL)
2703 {
2704 correct_range(eap);
2705 ex_print(eap);
2706 }
2707 }
2708 else if (eap->addr_count != 0)
2709 {
2710 if (eap->line2 > curbuf->b_ml.ml_line_count)
2711 {
2712 // With '-' in 'cpoptions' a line number past the file is an
2713 // error, otherwise put it at the end of the file.
2714 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2715 eap->line2 = -1;
2716 else
2717 eap->line2 = curbuf->b_ml.ml_line_count;
2718 }
2719
2720 if (eap->line2 < 0)
2721 errormsg = _(e_invalid_range);
2722 else
2723 {
2724 if (eap->line2 == 0)
2725 curwin->w_cursor.lnum = 1;
2726 else
2727 curwin->w_cursor.lnum = eap->line2;
2728 beginline(BL_SOL | BL_FIX);
2729 }
2730 }
2731 return errormsg;
2732}
2733
2734/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002735 * Check for an Ex command with optional tail.
2736 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002737 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002738 */
2739 static int
2740checkforcmd_opt(
2741 char_u **pp, // start of command
2742 char *cmd, // name of command
2743 int len, // required length
2744 int noparen)
2745{
2746 int i;
2747
2748 for (i = 0; cmd[i] != NUL; ++i)
2749 if (((char_u *)cmd)[i] != (*pp)[i])
2750 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002751 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2752 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002753 {
2754 *pp = skipwhite(*pp + i);
2755 return TRUE;
2756 }
2757 return FALSE;
2758}
2759
2760/*
2761 * Check for an Ex command with optional tail.
2762 * If there is a match advance "pp" to the argument and return TRUE.
2763 */
2764 int
2765checkforcmd(
2766 char_u **pp, // start of command
2767 char *cmd, // name of command
2768 int len) // required length
2769{
2770 return checkforcmd_opt(pp, cmd, len, FALSE);
2771}
2772
2773/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002774 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002775 * If there is a match advance "pp" to the argument and return TRUE.
2776 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002777 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002778checkforcmd_noparen(
2779 char_u **pp, // start of command
2780 char *cmd, // name of command
2781 int len) // required length
2782{
2783 return checkforcmd_opt(pp, cmd, len, TRUE);
2784}
2785
2786/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002787 * Parse and skip over command modifiers:
2788 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002789 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002791 * When "skip_only" is TRUE the global variables are not changed, except for
2792 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002793 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2794 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002795 * Call apply_cmdmod() to get the side effects of the modifiers:
2796 * - Increment "sandbox" for ":sandbox"
2797 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002798 * - set msg_silent for ":silent"
2799 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002800 * Return FAIL when the command is not to be executed.
2801 * May set "errormsg" to an error message.
2802 */
2803 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002804parse_command_modifiers(
2805 exarg_T *eap,
2806 char **errormsg,
2807 cmdmod_T *cmod,
2808 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002809{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002810 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002811 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002812 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002813 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002814 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002815 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002816
Bram Moolenaare1004402020-10-24 20:49:43 +02002817 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002818 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002819
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002820 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2821 {
2822 // The automatically inserted Visual area range is skipped, so that
2823 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002824 // range to after the modififiers. The command will be
2825 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2826 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002827 eap->cmd += 5;
2828 cmd_start = eap->cmd;
2829 has_visual_range = TRUE;
2830 }
2831
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002832 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 for (;;)
2834 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002835 char_u *p;
2836
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002838 {
2839 if (*eap->cmd == ':')
2840 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002842 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002843
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002844 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 if (*eap->cmd == NUL && exmode_active
2846 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2847 || getline_equal(eap->getline, eap->cookie, getexline))
2848 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2849 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002850 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002851 if (!skip_only)
2852 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002853 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 }
2855
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002856 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002857 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002858 {
2859 // a comment ends at a NL
2860 if (eap->nextcmd == NULL)
2861 {
2862 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2863 if (eap->nextcmd != NULL)
2864 ++eap->nextcmd;
2865 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002866 if (vim9script)
2867 {
2868 if (has_cmdmod(cmod, FALSE))
2869 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002870#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002871 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2872 && eap->cmd[2] != '{')
2873 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002874#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002875 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002877 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002878 if (*eap->cmd == NUL)
2879 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002880 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002881 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002882 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002883 if (vim9script && has_cmdmod(cmod, FALSE))
2884 *errormsg = _(e_command_modifier_without_command);
2885 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 return FAIL;
2887 }
2888
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002889 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002890
2891 // In Vim9 script a variable can shadow a command modifier:
2892 // verbose = 123
2893 // verbose += 123
2894 // silent! verbose = func()
2895 // verbose.member = 2
2896 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002897 // But not:
2898 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002899 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002900 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002901 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002902
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002903 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002904 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002905 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002906 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2907 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002908 break;
2909 }
2910
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 switch (*p)
2912 {
Bram Moolenaar91324262022-09-09 13:27:59 +01002913 // When adding an entry, also modify cmdmods[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002914 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002915 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002916 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002917 continue;
2918
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002919 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002920 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002921 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 continue;
2923 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002924 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002925 {
2926#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002927 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002928#endif
2929 continue;
2930 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002931 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002932 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002933 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002934 continue;
2935
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002936 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002937 break;
2938#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002939 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002940#endif
2941 continue;
2942
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002943 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002944 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002945 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002946 continue;
2947 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002948 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002949 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002950 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002951 continue;
2952 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002953 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002954 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002955 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 continue;
2957 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002958 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002959 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002960 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002961 continue;
2962
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002964 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002965 char_u *reg_pat;
2966 char_u *nulp = NULL;
2967 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002968
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002969 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002970 || *p == NUL
2971 || (ends_excmd(*p)
2972#ifdef FEAT_EVAL
2973 // in ":filter #pat# cmd" # does not
2974 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002975 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002976#endif
2977 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002978 break;
2979 if (*p == '!')
2980 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002981 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 p = skipwhite(p + 1);
2983 if (*p == NUL || ends_excmd(*p))
2984 break;
2985 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002986#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002987 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002988 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002989 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002990#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002991 if (skip_only)
2992 p = skip_vimgrep_pat(p, NULL, NULL);
2993 else
2994 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002995 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2996 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 if (p == NULL || *p == NUL)
2998 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002999 if (!skip_only)
3000 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003004 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003005 // restore the character overwritten by NUL
3006 if (nulp != NULL)
3007 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003008 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 eap->cmd = p;
3010 continue;
3011 }
3012
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003013 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3014 {
3015 cmod->cmod_split |= WSP_HOR;
3016 continue;
3017 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003018 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003019 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003020 || *p == NUL || ends_excmd(*p))
3021 break;
3022 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003023 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003024 continue;
3025
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003026 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003028 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003029 continue;
3030 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003031 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3032 {
3033 if (ends_excmd2(p, eap->cmd))
3034 {
3035 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003036 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003037 return FAIL;
3038 }
3039 cmod->cmod_flags |= CMOD_LEGACY;
3040 continue;
3041 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003042
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003043 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003044 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003045 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003046 continue;
3047
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003048 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003049 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003050 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003051 continue;
3052 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003053 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003054 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003055 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003056 continue;
3057
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003058 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003059 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003060 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003061 continue;
3062
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003063 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003064 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003065 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003066 continue;
3067 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003068 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003069 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003070 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003071 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3072 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003073 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003074 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003075 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003076 }
3077 continue;
3078
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003079 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003080 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003081 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003082 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003083 long tabnr = get_address(eap, &eap->cmd,
3084 ADDR_TABS, eap->skip,
3085 skip_only, FALSE, 1);
3086 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003087 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003088 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003089 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003090 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3091 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003092 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003093 return FAIL;
3094 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003095 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003096 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 }
3098 eap->cmd = p;
3099 continue;
3100 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003101 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003103 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003104 continue;
3105
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003106 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003107 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003108 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003109 continue;
3110
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003111 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003112 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003113 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003114 continue;
3115 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003116 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003117 {
3118 if (ends_excmd2(p, eap->cmd))
3119 {
3120 *errormsg =
3121 _(e_vim9cmd_must_be_followed_by_command);
3122 return FAIL;
3123 }
3124 cmod->cmod_flags |= CMOD_VIM9CMD;
3125 continue;
3126 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003127 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003128 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003129 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003130 {
zeertzjqcd749632022-06-14 13:30:35 +01003131 // zero means not set, one is verbose == 0, etc.
3132 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003133 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003134 else
zeertzjqcd749632022-06-14 13:30:35 +01003135 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003136 eap->cmd = p;
3137 continue;
3138 }
3139 break;
3140 }
3141
Bram Moolenaar1501b632022-03-27 16:56:21 +01003142 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003143 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003144 if (eap->cmd > cmd_start)
3145 {
3146 // Move the '<,'> range to after the modifiers and insert a colon.
3147 // Since the modifiers have been parsed put the colon on top of the
3148 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3149 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003150 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003151 {
3152 size_t len = STRLEN(cmd_start);
3153
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003154 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003155 // "'<,'>mods" -> "mods *+
3156 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003157 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003158 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003159 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003160 }
3161 else
3162 {
3163 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3164 eap->cmd -= 5;
3165 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3166 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003167 }
3168 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003169 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003170 // Special case: change empty command to "+".
3171 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003172 eap->cmd = (char_u *)"'<,'>+";
3173 else
3174 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003175 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003176 else if (use_plus_cmd)
3177 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003178
Bram Moolenaareffed932018-08-14 13:38:17 +02003179 return OK;
3180}
3181
3182/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003183 * Return TRUE if "cmod" has anything set.
3184 */
3185 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003186has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003187{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003188 return (cmod->cmod_flags != 0 && (!ignore_silent
3189 || (cmod->cmod_flags
3190 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003191 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003192 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003193 || cmod->cmod_tab != 0
3194 || cmod->cmod_filter_regmatch.regprog != NULL;
3195}
3196
Bram Moolenaar8991be22022-02-14 21:51:46 +00003197#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003198/*
3199 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3200 */
3201 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003202cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003203{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003204 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003205 {
3206 emsg(_(e_misplaced_command_modifier));
3207 return TRUE;
3208 }
3209 return FALSE;
3210}
Dominique Pelle748b3082022-01-08 12:41:16 +00003211#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003212
3213/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003214 * Apply the command modifiers. Saves current state in "cmdmod", call
3215 * undo_cmdmod() later.
3216 */
3217 void
3218apply_cmdmod(cmdmod_T *cmod)
3219{
3220#ifdef HAVE_SANDBOX
3221 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3222 {
3223 ++sandbox;
3224 cmod->cmod_did_sandbox = TRUE;
3225 }
3226#endif
zeertzjqcd749632022-06-14 13:30:35 +01003227 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003228 {
3229 if (cmod->cmod_verbose_save == 0)
3230 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003231 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003232 }
3233
3234 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3235 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003236 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003237 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003238 cmod->cmod_save_msg_scroll = msg_scroll;
3239 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003240 if (cmod->cmod_flags & CMOD_SILENT)
3241 ++msg_silent;
3242 if (cmod->cmod_flags & CMOD_UNSILENT)
3243 msg_silent = 0;
3244
3245 if (cmod->cmod_flags & CMOD_ERRSILENT)
3246 {
3247 ++emsg_silent;
3248 ++cmod->cmod_did_esilent;
3249 }
3250
Bram Moolenaare1004402020-10-24 20:49:43 +02003251 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003252 {
3253 // Set 'eventignore' to "all".
3254 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003255 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003256 set_string_option_direct((char_u *)"ei", -1,
3257 (char_u *)"all", OPT_FREE, SID_NONE);
3258 }
3259}
3260
3261/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003262 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003263 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003264 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003265undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003266{
Bram Moolenaare1004402020-10-24 20:49:43 +02003267 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003268 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003269 p_verbose = cmod->cmod_verbose_save - 1;
3270 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003271 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003272
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003273#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003274 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003275 {
3276 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003277 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003278 }
3279#endif
3280
Bram Moolenaare1004402020-10-24 20:49:43 +02003281 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003283 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003284 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3285 OPT_FREE, SID_NONE);
3286 free_string_option(cmod->cmod_save_ei);
3287 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003288 }
3289
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003290 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003291
Bram Moolenaare1004402020-10-24 20:49:43 +02003292 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003293 {
3294 // messages could be enabled for a serious error, need to check if the
3295 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003296 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3297 msg_silent = cmod->cmod_save_msg_silent - 1;
3298 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003299 if (emsg_silent < 0)
3300 emsg_silent = 0;
3301 // Restore msg_scroll, it's set by file I/O commands, even when no
3302 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003303 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003304
3305 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3306 // somewhere in the line. Put it back in the first column.
3307 if (redirecting())
3308 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003309
Bram Moolenaare1004402020-10-24 20:49:43 +02003310 cmod->cmod_save_msg_silent = 0;
3311 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003312 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003313}
3314
3315/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003316 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003317 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003318 * Return FAIL and set "errormsg" or return OK.
3319 */
3320 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003321parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003322{
3323 int address_count = 1;
3324 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003325 int need_check_cursor = FALSE;
3326 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003327
3328 // Repeat for all ',' or ';' separated addresses.
3329 for (;;)
3330 {
3331 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003332 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003333 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003334 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003335 eap->addr_count == 0, address_count++);
3336 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003337 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003338 if (lnum == MAXLNUM)
3339 {
3340 if (*eap->cmd == '%') // '%' - all lines
3341 {
3342 ++eap->cmd;
3343 switch (eap->addr_type)
3344 {
3345 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003346 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003347 eap->line1 = 1;
3348 eap->line2 = curbuf->b_ml.ml_line_count;
3349 break;
3350 case ADDR_LOADED_BUFFERS:
3351 {
3352 buf_T *buf = firstbuf;
3353
3354 while (buf->b_next != NULL
3355 && buf->b_ml.ml_mfp == NULL)
3356 buf = buf->b_next;
3357 eap->line1 = buf->b_fnum;
3358 buf = lastbuf;
3359 while (buf->b_prev != NULL
3360 && buf->b_ml.ml_mfp == NULL)
3361 buf = buf->b_prev;
3362 eap->line2 = buf->b_fnum;
3363 break;
3364 }
3365 case ADDR_BUFFERS:
3366 eap->line1 = firstbuf->b_fnum;
3367 eap->line2 = lastbuf->b_fnum;
3368 break;
3369 case ADDR_WINDOWS:
3370 case ADDR_TABS:
3371 if (IS_USER_CMDIDX(eap->cmdidx))
3372 {
3373 eap->line1 = 1;
3374 eap->line2 = eap->addr_type == ADDR_WINDOWS
3375 ? LAST_WIN_NR : LAST_TAB_NR;
3376 }
3377 else
3378 {
3379 // there is no Vim command which uses '%' and
3380 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003381 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003382 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003383 }
3384 break;
3385 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003386 case ADDR_UNSIGNED:
3387 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003388 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003389 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003390 case ADDR_ARGUMENTS:
3391 if (ARGCOUNT == 0)
3392 eap->line1 = eap->line2 = 0;
3393 else
3394 {
3395 eap->line1 = 1;
3396 eap->line2 = ARGCOUNT;
3397 }
3398 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003399 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003400#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003401 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003402 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003403 if (eap->line2 == 0)
3404 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003405#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003406 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003407 case ADDR_NONE:
3408 // Will give an error later if a range is found.
3409 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003410 }
3411 ++eap->addr_count;
3412 }
3413 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3414 {
3415 pos_T *fp;
3416
3417 // '*' - visual area
3418 if (eap->addr_type != ADDR_LINES)
3419 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003420 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003421 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003422 }
3423
3424 ++eap->cmd;
3425 if (!eap->skip)
3426 {
3427 fp = getmark('<', FALSE);
3428 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003429 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003430 eap->line1 = fp->lnum;
3431 fp = getmark('>', FALSE);
3432 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003433 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003434 eap->line2 = fp->lnum;
3435 ++eap->addr_count;
3436 }
3437 }
3438 }
3439 else
3440 eap->line2 = lnum;
3441 eap->addr_count++;
3442
3443 if (*eap->cmd == ';')
3444 {
3445 if (!eap->skip)
3446 {
3447 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003448
Bram Moolenaar0e717042020-04-27 19:29:01 +02003449 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003450 // accept zero as address, so 0;/PATTERN/ works correctly
3451 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003452 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003453 if (eap->line2 > 0)
3454 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003455 else
3456 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003457 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003458 }
3459 }
3460 else if (*eap->cmd != ',')
3461 break;
3462 ++eap->cmd;
3463 }
3464
3465 // One address given: set start and end lines.
3466 if (eap->addr_count == 1)
3467 {
3468 eap->line1 = eap->line2;
3469 // ... but only implicit: really no address given
3470 if (lnum == MAXLNUM)
3471 eap->addr_count = 0;
3472 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003473 ret = OK;
3474
3475theend:
3476 if (need_check_cursor)
3477 check_cursor();
3478 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003479}
3480
3481/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003482 * Append "cmd" to the error message in IObuff.
3483 * Takes care of limiting the length and handling 0xa0, which would be
3484 * invisible otherwise.
3485 */
3486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003487append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003488{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003489 size_t len = STRLEN(IObuff);
3490 char_u *s = cmd;
3491 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003492
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003493 if (len > IOSIZE - 100)
3494 {
3495 // Not enough space, truncate and put in "...".
3496 d = IObuff + IOSIZE - 100;
3497 d -= mb_head_off(IObuff, d);
3498 STRCPY(d, "...");
3499 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003500 STRCAT(IObuff, ": ");
3501 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003502 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003503 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003504 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003505 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003506 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003507 STRCPY(d, "<a0>");
3508 d += 4;
3509 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003510 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3511 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003512 else
3513 MB_COPY_CHAR(s, d);
3514 }
3515 *d = NUL;
3516}
3517
Dominique Pelle748b3082022-01-08 12:41:16 +00003518#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003519/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003520 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3521 * the name. Otherwise just return "start".
3522 */
3523 char_u *
3524skip_option_env_lead(char_u *start)
3525{
3526 char_u *name = start;
3527
3528 if (*start == '&')
3529 {
3530 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3531 name += 3;
3532 else
3533 name += 1;
3534 }
3535 else if (*start == '$')
3536 name += 1;
3537 return name;
3538}
Dominique Pelle748b3082022-01-08 12:41:16 +00003539#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003540
3541/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003542 * Return TRUE and set "*idx" if "p" points to a one letter command.
3543 * If not in Vim9 script:
3544 * - The 'k' command can directly be followed by any character.
3545 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3546 * but :sre[wind] is another command, as are :scr[iptnames],
3547 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3548 */
3549 static int
3550one_letter_cmd(char_u *p, cmdidx_T *idx)
3551{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003552 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003553 return FALSE;
3554 if (*p == 'k')
3555 {
3556 *idx = CMD_k;
3557 return TRUE;
3558 }
3559 if (p[0] == 's'
3560 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3561 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3562 || p[1] == 'g'
3563 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3564 || p[1] == 'I'
3565 || (p[1] == 'r' && p[2] != 'e')))
3566 {
3567 *idx = CMD_substitute;
3568 return TRUE;
3569 }
3570 return FALSE;
3571}
3572
3573/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003574 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3575 * call.
3576 */
3577 int
3578number_method(char_u *cmd)
3579{
3580 char_u *p = skipdigits(cmd);
3581
3582 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3583}
3584
3585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003587 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003588 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003589 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 *
3591 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3592 * assignment without "let". Sets eap->cmdidx to the command while returning
3593 * "eap->cmd".
3594 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 * Returns NULL for an ambiguous user command.
3596 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 char_u *
3598find_ex_command(
3599 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003600 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003601 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003602 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 int len;
3605 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003606 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003607#ifndef FEAT_EVAL
3608 int vim9 = FALSE;
3609#else
3610 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 /*
3613 * Recognize a Vim9 script function/method call and assignment:
3614 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3615 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003616 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003617 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003619 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003621 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003622 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3623 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003625 int oplen;
3626 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003627 char_u *swp;
3628
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003629 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003630 || (eap->cmd[0] == '$'
3631 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003632 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003633 && (valid_yank_reg(eap->cmd[1], FALSE)
3634 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003635 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003636 if (*eap->cmd == '&')
3637 {
3638 p = eap->cmd + 1;
3639 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3640 p += 2;
3641 p = to_name_end(p, FALSE);
3642 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003643 else if (*eap->cmd == '$')
3644 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003645 else
3646 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003647 if (ends_excmd(*skipwhite(p)))
3648 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003649 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3650 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003651 eap->cmdidx = CMD_eval;
3652 return eap->cmd;
3653 }
3654 // "&option" can be followed by "->" or "=", check below
3655 }
3656
3657 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003659 if (
3660 // "(..." is an expression.
3661 // "funcname(" is always a function call.
3662 *p == '('
3663 || (p == eap->cmd
3664 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003665 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003666 *eap->cmd == '{'
3667 // "'string'->func()" is an expression.
3668 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003669 // '"string"->func()' is an expression.
3670 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003671 // '$"string"->func()' is an expression.
3672 // "$'string'->func()" is an expression.
3673 || (eap->cmd[0] == '$'
3674 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3675 // '0z1234->func()' is an expression.
3676 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003677 // "g:varname" is an expression.
3678 || eap->cmd[1] == ':'
3679 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003680 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003681 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003682 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003683 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3684 {
3685 // "{" by itself is the start of a block.
3686 eap->cmdidx = CMD_block;
3687 return eap->cmd + 1;
3688 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003689 eap->cmdidx = CMD_eval;
3690 return eap->cmd;
3691 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003692
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003693 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003694 // "varname[]" is an expression.
3695 *p == '['
3696 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003697 || (*p == '.'
3698 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3699 // g:[key] is an expression
3700 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003701 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003702 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003703
3704 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003705 // Skip over the whole thing, it can be:
3706 // name.member = val
3707 // name[a : b] = val
3708 // name[idx] = val
3709 // name[idx].member = val
3710 // etc.
3711 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003712 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003713 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003714 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003715 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003716 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003717 || (after[0] == '.' && after[1] == '.'
3718 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003719 eap->cmdidx = CMD_var;
3720 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003721 --emsg_silent;
3722 return eap->cmd;
3723 }
3724
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003725 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3726 // an assignment.
3727 // If there is no line break inside the "[...]" then "p" is
3728 // advanced to after the "]" by to_name_const_end(): check if a "="
3729 // follows.
3730 // If "[...]" has a line break "p" still points at the "[" and it
3731 // can't be an assignment.
3732 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003733 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003734 char_u *eq;
3735
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003736 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003737 if (p == eap->cmd && *p == '[')
3738 {
3739 int count = 0;
3740 int semicolon = FALSE;
3741
3742 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3743 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003744 eq = p;
3745 if (eq != NULL)
3746 {
3747 eq = skipwhite(eq);
3748 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3749 ++eq;
3750 }
3751 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003752 {
3753 eap->cmdidx = CMD_eval;
3754 return eap->cmd;
3755 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003756 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003757 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003758 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003759 return eap->cmd;
3760 }
3761 }
3762
3763 // Recognize an assignment if we recognize the variable name:
3764 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003765 // "@r = expr"
3766 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003767 // "var = expr" where "var" is a variable name or we are skipping
3768 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003769 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003770 oplen = assignment_len(skipwhite(p), &heredoc);
3771 if (oplen > 0)
3772 {
3773 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3774 || *eap->cmd == '&'
3775 || *eap->cmd == '$'
3776 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003777 || (eap->skip && IS_WHITE_OR_NUL(
3778 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003779 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003780 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003781 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003782 return eap->cmd;
3783 }
3784 }
3785
Bram Moolenaar9510d222022-09-11 15:14:05 +01003786 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003787 // "w:varname: number = 123".
3788 if (eap->cmd[1] == ':' && *p == ':')
3789 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003790 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003791 return eap->cmd;
3792 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003793 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003794
Bram Moolenaar31d99482022-05-26 22:24:43 +01003795 // 1234->func() is a method call
3796 if (number_method(eap->cmd))
3797 {
3798 eap->cmdidx = CMD_eval;
3799 return eap->cmd;
3800 }
3801
Bram Moolenaar7b829262021-10-13 15:04:34 +01003802 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3803 // an expression. A global/substitute/list command needs to use a
3804 // longer name.
3805 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3806 {
3807 eap->cmdidx = CMD_eval;
3808 return eap->cmd;
3809 }
3810
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003811 // If it is an ID it might be a variable with an operator on the next
3812 // line, if the variable exists it can't be an Ex command.
3813 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003814 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003815 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3816 {
3817 eap->cmdidx = CMD_eval;
3818 return eap->cmd;
3819 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003820
3821 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003822 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3823 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003824 {
3825 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3826 return eap->cmd + 2;
3827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 }
3829#endif
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 /*
3832 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 */
3834 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003835 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 ++p;
3838 }
3839 else
3840 {
3841 while (ASCII_ISALPHA(*p))
3842 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003843 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003844 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003846 while (ASCII_ISALNUM(*p))
3847 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 }
3849 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3850 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003851 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 ++p;
3853 while (ASCII_ISALPHA(*p))
3854 ++p;
3855 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003857 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003858 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 ++p;
3860 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003861 // The "d" command can directly be followed by 'l' or 'p' flag, when
3862 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003863 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003864 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003865 // Check for ":dl", ":dell", etc. to ":deletel": that's
3866 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003867 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003868 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003869 break;
3870 if (i == len - 1)
3871 {
3872 --len;
3873 if (p[-1] == 'l')
3874 eap->flags |= EXFLAG_LIST;
3875 else
3876 eap->flags |= EXFLAG_PRINT;
3877 }
3878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003880 if (ASCII_ISLOWER(eap->cmd[0]))
3881 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003882 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003883 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003884
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003885 if (command_count != (int)CMD_SIZE)
3886 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003887 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003888 getout(1);
3889 }
3890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003891 // Use a precomputed index for fast look-up in cmdnames[]
3892 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003893 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3894 if (ASCII_ISLOWER(c2))
3895 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3896 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003897 else if (ASCII_ISUPPER(eap->cmd[0]))
3898 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003900 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
3902 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3903 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3904 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3905 (size_t)len) == 0)
3906 {
3907#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003908 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 *full = TRUE;
3910#endif
3911 break;
3912 }
3913
Bram Moolenaar204852a2022-03-05 12:56:44 +00003914 // :Print and :mode are not supported in Vim9 script.
3915 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003916 if (vim9 && eap->cmdidx != CMD_SIZE)
3917 {
3918 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3919 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003920 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003921 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003922 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003923 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003924 eap->cmdidx = CMD_SIZE;
3925 }
3926 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003927
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003928 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003929 // 'cpoptions'.
3930 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3931 p = eap->cmd;
3932
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003933 // Look for a user defined command as a last resort. Let ":Print" be
3934 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003935 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3936 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003938 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 while (ASCII_ISALNUM(*p))
3940 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003941 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003943 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 eap->cmdidx = CMD_SIZE;
3945 }
3946
Bram Moolenaar204852a2022-03-05 12:56:44 +00003947 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3948 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003949 eap->cmdidx = CMD_finally;
3950
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003951#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003952 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003953 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003954 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003955 && (eap->cmdidx < 0 ||
3956 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003957 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003958 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3959
3960 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003961 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003962 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003963 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003964#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return p;
3967}
3968
3969#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003970static struct cmdmod
3971{
3972 char *name;
3973 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003974 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003975} cmdmods[] = {
3976 {"aboveleft", 3, FALSE},
3977 {"belowright", 3, FALSE},
3978 {"botright", 2, FALSE},
3979 {"browse", 3, FALSE},
3980 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003981 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003982 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003983 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003984 {"keepalt", 5, FALSE},
3985 {"keepjumps", 5, FALSE},
3986 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003987 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003988 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01003989 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003990 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003991 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003992 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003993 {"rightbelow", 6, FALSE},
3994 {"sandbox", 3, FALSE},
3995 {"silent", 3, FALSE},
3996 {"tab", 3, TRUE},
3997 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003998 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003999 {"verbose", 4, TRUE},
4000 {"vertical", 4, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004001 {"vim9cmd", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004002};
4003
4004/*
4005 * Return length of a command modifier (including optional count).
4006 * Return zero when it's not a modifier.
4007 */
4008 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004009modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004010{
4011 int i, j;
4012 char_u *p = cmd;
4013
4014 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004015 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02004016 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004017 {
4018 for (j = 0; p[j] != NUL; ++j)
4019 if (p[j] != cmdmods[i].name[j])
4020 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02004021 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00004022 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00004023 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004024 }
4025 return 0;
4026}
4027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028/*
4029 * Return > 0 if an Ex command "name" exists.
4030 * Return 2 if there is an exact match.
4031 * Return 3 if there is an ambiguous match.
4032 */
4033 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004034cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
4036 exarg_T ea;
4037 int full = FALSE;
4038 int i;
4039 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004040 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004042 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004043 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
4045 for (j = 0; name[j] != NUL; ++j)
4046 if (name[j] != cmdmods[i].name[j])
4047 break;
4048 if (name[j] == NUL && j >= cmdmods[i].minlen)
4049 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4050 }
4051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004052 // Check built-in commands and user defined commands.
4053 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004054 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004056 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004058 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004060 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4061 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004062 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004063 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4065}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004066
4067/*
4068 * "fullcommand" function
4069 */
4070 void
4071f_fullcommand(typval_T *argvars, typval_T *rettv)
4072{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004073 exarg_T ea;
4074 char_u *name;
4075 char_u *p;
4076 int vim9script = in_vim9script();
4077 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004078
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004079 rettv->v_type = VAR_STRING;
4080 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004081
Bram Moolenaaraa534142022-09-15 21:46:02 +01004082 if (in_vim9script()
4083 && (check_for_string_arg(argvars, 0) == FAIL
4084 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004085 return;
4086
4087 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004088 if (name == NULL)
4089 return;
4090
Bram Moolenaaraa534142022-09-15 21:46:02 +01004091 if (argvars[1].v_type != VAR_UNKNOWN)
4092 {
4093 vim9script = tv_get_bool(&argvars[1]);
4094 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4095 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4096 }
4097
zeertzjqc024ed92022-01-04 16:22:52 +00004098 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004099 name++;
4100 name = skip_range(name, TRUE, NULL);
4101
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004102 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4103 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004104 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004105 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004106 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004107 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004108 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004109 goto theend;
4110
4111 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004112 {
4113 int res;
4114
4115 ++emsg_silent;
4116 res = not_in_vim9(&ea);
4117 --emsg_silent;
4118
4119 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004120 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004121 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004122
4123 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004124 ? get_user_command_name(ea.useridx, ea.cmdidx)
4125 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004126theend:
4127 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004128}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129#endif
4130
Bram Moolenaard0190392019-08-23 21:17:35 +02004131 cmdidx_T
4132excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
Bram Moolenaard0190392019-08-23 21:17:35 +02004134 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004136 if (!one_letter_cmd(cmd, &idx))
4137 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4138 idx = (cmdidx_T)((int)idx + 1))
4139 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4140 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Bram Moolenaard0190392019-08-23 21:17:35 +02004142 return idx;
4143}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
Bram Moolenaard0190392019-08-23 21:17:35 +02004145 long
4146excmd_get_argt(cmdidx_T idx)
4147{
4148 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149}
4150
4151/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004152 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 *
4154 * Backslashed delimiters after / or ? will be skipped, and commands will
4155 * not be expanded between /'s and ?'s or after "'".
4156 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004157 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * Returns the "cmd" pointer advanced to beyond the range.
4159 */
4160 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004161skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004162 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004163 int skip_star, // skip "*" used for Visual range
4164 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004166 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004167 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004169 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004171 if (*cmd == '\\')
4172 {
4173 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4174 ++cmd;
4175 else
4176 break;
4177 }
4178 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004180 char_u *p = cmd;
4181
4182 // a quote is only valid at the start or after a separator
4183 while (p > cmd_start)
4184 {
4185 --p;
4186 if (!VIM_ISWHITE(*p))
4187 break;
4188 }
4189 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4190 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 if (*++cmd == NUL && ctx != NULL)
4192 *ctx = EXPAND_NOTHING;
4193 }
4194 else if (*cmd == '/' || *cmd == '?')
4195 {
4196 delim = *cmd++;
4197 while (*cmd != NUL && *cmd != delim)
4198 if (*cmd++ == '\\' && *cmd != NUL)
4199 ++cmd;
4200 if (*cmd == NUL && ctx != NULL)
4201 *ctx = EXPAND_NOTHING;
4202 }
4203 if (*cmd != NUL)
4204 ++cmd;
4205 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004206
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004207 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004208 while (*cmd == ':')
4209 cmd = skipwhite(cmd + 1);
4210
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004211 // Skip "*" used for Visual range.
4212 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4213 cmd = skipwhite(cmd + 1);
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 return cmd;
4216}
4217
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004218 static void
4219addr_error(cmd_addr_T addr_type)
4220{
4221 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004222 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004223 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004224 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004225}
4226
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004228 * Return the default address for an address type.
4229 */
4230 static linenr_T
4231default_address(exarg_T *eap)
4232{
4233 linenr_T lnum = 0;
4234
4235 switch (eap->addr_type)
4236 {
4237 case ADDR_LINES:
4238 case ADDR_OTHER:
4239 // Default is the cursor line number. Avoid using an invalid
4240 // line number though.
4241 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4242 lnum = curbuf->b_ml.ml_line_count;
4243 else
4244 lnum = curwin->w_cursor.lnum;
4245 break;
4246 case ADDR_WINDOWS:
4247 lnum = CURRENT_WIN_NR;
4248 break;
4249 case ADDR_ARGUMENTS:
4250 lnum = curwin->w_arg_idx + 1;
4251 if (lnum > ARGCOUNT)
4252 lnum = ARGCOUNT;
4253 break;
4254 case ADDR_LOADED_BUFFERS:
4255 case ADDR_BUFFERS:
4256 lnum = curbuf->b_fnum;
4257 break;
4258 case ADDR_TABS:
4259 lnum = CURRENT_TAB_NR;
4260 break;
4261 case ADDR_TABS_RELATIVE:
4262 case ADDR_UNSIGNED:
4263 lnum = 1;
4264 break;
4265 case ADDR_QUICKFIX:
4266#ifdef FEAT_QUICKFIX
4267 lnum = qf_get_cur_idx(eap);
4268#endif
4269 break;
4270 case ADDR_QUICKFIX_VALID:
4271#ifdef FEAT_QUICKFIX
4272 lnum = qf_get_cur_valid_idx(eap);
4273#endif
4274 break;
4275 case ADDR_NONE:
4276 // Will give an error later if a range is found.
4277 break;
4278 }
4279 return lnum;
4280}
4281
4282/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004283 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 *
4285 * Set ptr to the next character after the part that was interpreted.
4286 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004287 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 *
4289 * Return MAXLNUM when no Ex address was found.
4290 */
4291 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004292get_address(
4293 exarg_T *eap UNUSED,
4294 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004295 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004296 int skip, // only skip the address, don't use it
4297 int silent, // no errors or side effects
4298 int to_other_file, // flag: may jump to other file
4299 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
4301 int c;
4302 int i;
4303 long n;
4304 char_u *cmd;
4305 pos_T pos;
4306 pos_T *fp;
4307 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004308 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 cmd = skipwhite(*ptr);
4311 lnum = MAXLNUM;
4312 do
4313 {
4314 switch (*cmd)
4315 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004316 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004317 ++cmd;
4318 switch (addr_type)
4319 {
4320 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004321 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 lnum = curwin->w_cursor.lnum;
4323 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004324 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004325 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004326 break;
4327 case ADDR_ARGUMENTS:
4328 lnum = curwin->w_arg_idx + 1;
4329 break;
4330 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004331 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004332 lnum = curbuf->b_fnum;
4333 break;
4334 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004335 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004337 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004338 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004339 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004340 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004341 cmd = NULL;
4342 goto error;
4343 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004344 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004345#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004346 lnum = qf_get_cur_idx(eap);
4347#endif
4348 break;
4349 case ADDR_QUICKFIX_VALID:
4350#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004351 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004352#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004353 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004354 }
4355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004357 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 ++cmd;
4359 switch (addr_type)
4360 {
4361 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004362 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 lnum = curbuf->b_ml.ml_line_count;
4364 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004365 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004366 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004367 break;
4368 case ADDR_ARGUMENTS:
4369 lnum = ARGCOUNT;
4370 break;
4371 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004372 buf = lastbuf;
4373 while (buf->b_ml.ml_mfp == NULL)
4374 {
4375 if (buf->b_prev == NULL)
4376 break;
4377 buf = buf->b_prev;
4378 }
4379 lnum = buf->b_fnum;
4380 break;
4381 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004382 lnum = lastbuf->b_fnum;
4383 break;
4384 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004385 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004387 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004388 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004389 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004390 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004391 cmd = NULL;
4392 goto error;
4393 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004394 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004395#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004396 lnum = qf_get_size(eap);
4397 if (lnum == 0)
4398 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004399#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004400 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004401 case ADDR_QUICKFIX_VALID:
4402#ifdef FEAT_QUICKFIX
4403 lnum = qf_get_valid_size(eap);
4404 if (lnum == 0)
4405 lnum = 1;
4406#endif
4407 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 }
4409 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004411 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 if (*++cmd == NUL)
4413 {
4414 cmd = NULL;
4415 goto error;
4416 }
4417 if (addr_type != ADDR_LINES)
4418 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004419 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004420 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 goto error;
4422 }
4423 if (skip)
4424 ++cmd;
4425 else
4426 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 // Only accept a mark in another file when it is
4428 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4430 ++cmd;
4431 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004432 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 lnum = curwin->w_cursor.lnum;
4434 else
4435 {
4436 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
4438 cmd = NULL;
4439 goto error;
4440 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 lnum = fp->lnum;
4442 }
4443 }
4444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004447 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 c = *cmd++;
4449 if (addr_type != ADDR_LINES)
4450 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004451 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004452 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 goto error;
4454 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004455 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004457 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004458 if (*cmd == c)
4459 ++cmd;
4460 }
4461 else
4462 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004463 int flags;
4464
4465 pos = curwin->w_cursor; // save curwin->w_cursor
4466
4467 // When '/' or '?' follows another address, start from
4468 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004469 if (lnum > 0 && lnum != MAXLNUM)
4470 curwin->w_cursor.lnum =
4471 lnum > curbuf->b_ml.ml_line_count
4472 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004473
4474 // Start a forward search at the end of the line (unless
4475 // before the first line).
4476 // Start a backward search at the start of the line.
4477 // This makes sure we never match in the current
4478 // line, and can match anywhere in the
4479 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004480 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 curwin->w_cursor.col = MAXCOL;
4482 else
4483 curwin->w_cursor.col = 0;
4484 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004485 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004486 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 {
4488 curwin->w_cursor = pos;
4489 cmd = NULL;
4490 goto error;
4491 }
4492 lnum = curwin->w_cursor.lnum;
4493 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004494 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 cmd += searchcmdlen;
4496 }
4497 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004499 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 ++cmd;
4501 if (addr_type != ADDR_LINES)
4502 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004503 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004504 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 goto error;
4506 }
4507 if (*cmd == '&')
4508 i = RE_SUBST;
4509 else if (*cmd == '?' || *cmd == '/')
4510 i = RE_SEARCH;
4511 else
4512 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004513 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 cmd = NULL;
4515 goto error;
4516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 if (!skip)
4519 {
4520 /*
4521 * When search follows another address, start from
4522 * there.
4523 */
4524 if (lnum != MAXLNUM)
4525 pos.lnum = lnum;
4526 else
4527 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004529 /*
4530 * Start the search just like for the above
4531 * do_search().
4532 */
4533 if (*cmd != '?')
4534 pos.col = MAXCOL;
4535 else
4536 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004537 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004538 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004539 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004540 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004541 lnum = pos.lnum;
4542 else
4543 {
4544 cmd = NULL;
4545 goto error;
4546 }
4547 }
4548 ++cmd;
4549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004552 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004553 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555
4556 for (;;)
4557 {
4558 cmd = skipwhite(cmd);
4559 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4560 break;
4561
4562 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 {
4564 switch (addr_type)
4565 {
4566 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004567 case ADDR_OTHER:
4568 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004569 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004570 break;
4571 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004572 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004573 break;
4574 case ADDR_ARGUMENTS:
4575 lnum = curwin->w_arg_idx + 1;
4576 break;
4577 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004578 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004579 lnum = curbuf->b_fnum;
4580 break;
4581 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004582 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004583 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004584 case ADDR_TABS_RELATIVE:
4585 lnum = 1;
4586 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004587 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004588#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004589 lnum = qf_get_cur_idx(eap);
4590#endif
4591 break;
4592 case ADDR_QUICKFIX_VALID:
4593#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004594 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004595#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004596 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004597 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004598 case ADDR_UNSIGNED:
4599 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004600 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004601 }
4602 }
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004605 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 else
4607 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004608 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 n = 1;
4610 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004611 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004612 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004614 if (n == MAXLNUM)
4615 {
4616 emsg(_(e_line_number_out_of_range));
4617 goto error;
4618 }
4619 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004620
4621 if (addr_type == ADDR_TABS_RELATIVE)
4622 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004623 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004624 cmd = NULL;
4625 goto error;
4626 }
4627 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004628 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004629 lnum = compute_buffer_local_count(
4630 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004632 {
4633#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004634 // Relative line addressing: need to adjust for lines in a
4635 // closed fold after the first address.
4636 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4637 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004638 (void)hasFolding(lnum, NULL, &lnum);
4639#endif
4640 if (i == '-')
4641 lnum -= n;
4642 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004643 {
4644 if (n >= LONG_MAX - lnum)
4645 {
4646 emsg(_(e_line_number_out_of_range));
4647 goto error;
4648 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004649 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004650 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653 } while (*cmd == '/' || *cmd == '?');
4654
4655error:
4656 *ptr = cmd;
4657 return lnum;
4658}
4659
4660/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004661 * Set eap->line1 and eap->line2 to the whole range.
4662 * Used for commands with the EX_DFLALL flag and no range given.
4663 */
4664 static void
4665address_default_all(exarg_T *eap)
4666{
4667 eap->line1 = 1;
4668 switch (eap->addr_type)
4669 {
4670 case ADDR_LINES:
4671 case ADDR_OTHER:
4672 eap->line2 = curbuf->b_ml.ml_line_count;
4673 break;
4674 case ADDR_LOADED_BUFFERS:
4675 {
4676 buf_T *buf = firstbuf;
4677
4678 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4679 buf = buf->b_next;
4680 eap->line1 = buf->b_fnum;
4681 buf = lastbuf;
4682 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4683 buf = buf->b_prev;
4684 eap->line2 = buf->b_fnum;
4685 }
4686 break;
4687 case ADDR_BUFFERS:
4688 eap->line1 = firstbuf->b_fnum;
4689 eap->line2 = lastbuf->b_fnum;
4690 break;
4691 case ADDR_WINDOWS:
4692 eap->line2 = LAST_WIN_NR;
4693 break;
4694 case ADDR_TABS:
4695 eap->line2 = LAST_TAB_NR;
4696 break;
4697 case ADDR_TABS_RELATIVE:
4698 eap->line2 = 1;
4699 break;
4700 case ADDR_ARGUMENTS:
4701 if (ARGCOUNT == 0)
4702 eap->line1 = eap->line2 = 0;
4703 else
4704 eap->line2 = ARGCOUNT;
4705 break;
4706 case ADDR_QUICKFIX_VALID:
4707#ifdef FEAT_QUICKFIX
4708 eap->line2 = qf_get_valid_size(eap);
4709 if (eap->line2 == 0)
4710 eap->line2 = 1;
4711#endif
4712 break;
4713 case ADDR_NONE:
4714 case ADDR_UNSIGNED:
4715 case ADDR_QUICKFIX:
4716 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4717 break;
4718 }
4719}
4720
4721
4722/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004723 * Get flags from an Ex command argument.
4724 */
4725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004726get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004727{
4728 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4729 {
4730 if (*eap->arg == 'l')
4731 eap->flags |= EXFLAG_LIST;
4732 else if (*eap->arg == 'p')
4733 eap->flags |= EXFLAG_PRINT;
4734 else
4735 eap->flags |= EXFLAG_NR;
4736 eap->arg = skipwhite(eap->arg + 1);
4737 }
4738}
4739
4740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 * Function called for command which is Not Implemented. NI!
4742 */
4743 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004744ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004747 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004748 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749}
4750
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004751#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752/*
4753 * Function called for script command which is Not Implemented. NI!
4754 * Skips over ":perl <<EOF" constructs.
4755 */
4756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004757ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759 if (!eap->skip)
4760 ex_ni(eap);
4761 else
4762 vim_free(script_get(eap, eap->arg));
4763}
4764#endif
4765
4766/*
4767 * Check range in Ex command for validity.
4768 * Return NULL when valid, error message when invalid.
4769 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004770 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004771invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004773 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004774
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 if ( eap->line1 < 0
4776 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004777 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004778 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004779
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004780 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004781 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004782 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004783 {
4784 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004785 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004786#ifdef FEAT_DIFF
4787 + (eap->cmdidx == CMD_diffget)
4788#endif
4789 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004790 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004791 break;
4792 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004793 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004794 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004795 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004796 break;
4797 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004798 // Only a boundary check, not whether the buffers actually
4799 // exist.
4800 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004801 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004802 break;
4803 case ADDR_LOADED_BUFFERS:
4804 buf = firstbuf;
4805 while (buf->b_ml.ml_mfp == NULL)
4806 {
4807 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004808 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004809 buf = buf->b_next;
4810 }
4811 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004812 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004813 buf = lastbuf;
4814 while (buf->b_ml.ml_mfp == NULL)
4815 {
4816 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004817 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004818 buf = buf->b_prev;
4819 }
4820 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004821 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004822 break;
4823 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004824 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004825 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004826 break;
4827 case ADDR_TABS:
4828 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004829 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004830 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004831 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004832 case ADDR_OTHER:
4833 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004834 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004835 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004836#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004837 // No error for value that is too big, will use the last entry.
4838 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004839 {
4840 if (eap->addr_count == 0)
4841 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004842 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004843 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004844#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004845 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004846 case ADDR_QUICKFIX_VALID:
4847#ifdef FEAT_QUICKFIX
4848 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4849 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004850 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004851#endif
4852 break;
4853 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004854 case ADDR_NONE:
4855 // Will give an error elsewhere.
4856 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004857 }
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 return NULL;
4860}
4861
4862/*
4863 * Correct the range for zero line number, if required.
4864 */
4865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004866correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004868 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
4870 if (eap->line1 == 0)
4871 eap->line1 = 1;
4872 if (eap->line2 == 0)
4873 eap->line2 = 1;
4874 }
4875}
4876
Bram Moolenaar748bf032005-02-02 23:04:36 +00004877#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004878/*
4879 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4880 * pattern. Otherwise return eap->arg.
4881 */
4882 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004883skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004884{
4885 char_u *p = eap->arg;
4886
Bram Moolenaara37420f2006-02-04 22:37:47 +00004887 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4888 || eap->cmdidx == CMD_vimgrepadd
4889 || eap->cmdidx == CMD_lvimgrepadd
4890 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004891 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004892 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004893 if (p == NULL)
4894 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004895 }
4896 return p;
4897}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004898
4899/*
4900 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4901 * in the command line, so that things like % get expanded.
4902 */
4903 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004904replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004905{
4906 char_u *new_cmdline;
4907 char_u *program;
4908 char_u *pos;
4909 char_u *ptr;
4910 int len;
4911 int i;
4912
4913 /*
4914 * Don't do it when ":vimgrep" is used for ":grep".
4915 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004916 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4917 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4918 || eap->cmdidx == CMD_grepadd
4919 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004920 && !grep_internal(eap->cmdidx))
4921 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004922 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4923 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004924 {
4925 if (*curbuf->b_p_gp == NUL)
4926 program = p_gp;
4927 else
4928 program = curbuf->b_p_gp;
4929 }
4930 else
4931 {
4932 if (*curbuf->b_p_mp == NUL)
4933 program = p_mp;
4934 else
4935 program = curbuf->b_p_mp;
4936 }
4937
4938 p = skipwhite(p);
4939
4940 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4941 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004942 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004943 i = 1;
4944 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4945 ++i;
4946 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004947 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004948 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004949 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004950 ptr = new_cmdline;
4951 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4952 {
4953 i = (int)(pos - program);
4954 STRNCPY(ptr, program, i);
4955 STRCPY(ptr += i, p);
4956 ptr += len;
4957 program = pos + 2;
4958 }
4959 STRCPY(ptr, program);
4960 }
4961 else
4962 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004963 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004964 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004965 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004966 STRCPY(new_cmdline, program);
4967 STRCAT(new_cmdline, " ");
4968 STRCAT(new_cmdline, p);
4969 }
4970 msg_make(p);
4971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004972 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004973 vim_free(*cmdlinep);
4974 *cmdlinep = new_cmdline;
4975 p = new_cmdline;
4976 }
4977 return p;
4978}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004979#endif
4980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981/*
4982 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004983 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 * Return FAIL for failure, OK otherwise.
4985 */
4986 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004987expand_filename(
4988 exarg_T *eap,
4989 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004990 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004992 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 char_u *repl;
4994 int srclen;
4995 char_u *p;
4996 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004997 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
Bram Moolenaar748bf032005-02-02 23:04:36 +00004999#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005000 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005001 p = skip_grep_pat(eap);
5002#else
5003 p = eap->arg;
5004#endif
5005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 /*
5007 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5008 * the file name contains a wildcard it should not cause expanding.
5009 * (it will be expanded anyway if there is a wildcard before replacing).
5010 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005011 has_wildcards = mch_has_wildcard(p);
5012 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005014#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005015 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005016 if (p[0] == '`' && p[1] == '=')
5017 {
5018 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005019 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005020 if (*p == '`')
5021 ++p;
5022 continue;
5023 }
5024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 /*
5026 * Quick check if this cannot be the start of a special string.
5027 * Also removes backslash before '%', '#' and '<'.
5028 */
5029 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5030 {
5031 ++p;
5032 continue;
5033 }
5034
5035 /*
5036 * Try to find a match at this position.
5037 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005038 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005039 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005042 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
5044 p += srclen;
5045 continue;
5046 }
5047
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005048 // Wildcards won't be expanded below, the replacement is taken
5049 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005050 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5051 {
5052 char_u *l = repl;
5053
5054 repl = expand_env_save(repl);
5055 vim_free(l);
5056 }
5057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005058 // Need to escape white space et al. with a backslash.
5059 // Don't do this for:
5060 // - replacement that already has been escaped: "##"
5061 // - shell commands (may have to use quotes instead).
5062 // - non-unix systems when there is a single argument (spaces don't
5063 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005065 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 && eap->cmdidx != CMD_grep
5068 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005069 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005070 && eap->cmdidx != CMD_lgrep
5071 && eap->cmdidx != CMD_lgrepadd
5072 && eap->cmdidx != CMD_lmake
5073 && eap->cmdidx != CMD_make
5074 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005076 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077#endif
5078 )
5079 {
5080 char_u *l;
5081#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 // Don't escape a backslash here, because rem_backslash() doesn't
5083 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 static char_u *nobslash = (char_u *)" \t\"|";
5085# define ESCAPE_CHARS nobslash
5086#else
5087# define ESCAPE_CHARS escape_chars
5088#endif
5089
5090 for (l = repl; *l; ++l)
5091 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5092 {
5093 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5094 if (l != NULL)
5095 {
5096 vim_free(repl);
5097 repl = l;
5098 }
5099 break;
5100 }
5101 }
5102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005103 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005104 if ((eap->usefilter || eap->cmdidx == CMD_bang
5105 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005106 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 char_u *l;
5109
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005110 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (l != NULL)
5112 {
5113 vim_free(repl);
5114 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 }
5117
5118 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5119 vim_free(repl);
5120 if (p == NULL)
5121 return FAIL;
5122 }
5123
5124 /*
5125 * One file argument: Expand wildcards.
5126 * Don't do this with ":r !command" or ":w !command".
5127 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005128 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
5130 /*
5131 * May do this twice:
5132 * 1. Replace environment variables.
5133 * 2. Replace any other wildcards, remove backslashes.
5134 */
5135 for (n = 1; n <= 2; ++n)
5136 {
5137 if (n == 2)
5138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 /*
5140 * Halve the number of backslashes (this is Vi compatible).
5141 * For Unix and OS/2, when wildcards are expanded, this is
5142 * done by ExpandOne() below.
5143 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005144#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (!has_wildcards)
5146#endif
5147 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149
5150 if (has_wildcards)
5151 {
5152 if (n == 1)
5153 {
5154 /*
5155 * First loop: May expand environment variables. This
5156 * can be done much faster with expand_env() than with
5157 * something else (e.g., calling a shell).
5158 * After expanding environment variables, check again
5159 * if there are still wildcards present.
5160 */
5161 if (vim_strchr(eap->arg, '$') != NULL
5162 || vim_strchr(eap->arg, '~') != NULL)
5163 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005164 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005165 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 has_wildcards = mch_has_wildcard(NameBuff);
5167 p = NameBuff;
5168 }
5169 else
5170 p = NULL;
5171 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005172 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
5174 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005175 int options = WILD_LIST_NOTFOUND
5176 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
5178 ExpandInit(&xpc);
5179 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005180 if (p_wic)
5181 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005183 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (p == NULL)
5185 return FAIL;
5186 }
5187 if (p != NULL)
5188 {
5189 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5190 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005191 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 vim_free(p);
5193 }
5194 }
5195 }
5196 }
5197 return OK;
5198}
5199
5200/*
5201 * Replace part of the command line, keeping eap->cmd, eap->arg and
5202 * eap->nextcmd correct.
5203 * "src" points to the part that is to be replaced, of length "srclen".
5204 * "repl" is the replacement string.
5205 * Returns a pointer to the character after the replaced string.
5206 * Returns NULL for failure.
5207 */
5208 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005209repl_cmdline(
5210 exarg_T *eap,
5211 char_u *src,
5212 int srclen,
5213 char_u *repl,
5214 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
5216 int len;
5217 int i;
5218 char_u *new_cmdline;
5219
5220 /*
5221 * The new command line is build in new_cmdline[].
5222 * First allocate it.
5223 * Careful: a "+cmd" argument may have been NUL terminated.
5224 */
5225 len = (int)STRLEN(repl);
5226 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005227 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005228 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005229 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005230 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
5232 /*
5233 * Copy the stuff before the expanded part.
5234 * Copy the expanded stuff.
5235 * Copy what came after the expanded part.
5236 * Copy the next commands, if there are any.
5237 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005238 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005242 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005244 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005246 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 {
5248 i = (int)STRLEN(new_cmdline) + 1;
5249 STRCPY(new_cmdline + i, eap->nextcmd);
5250 eap->nextcmd = new_cmdline + i;
5251 }
5252 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5253 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5254 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5255 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5256 vim_free(*cmdlinep);
5257 *cmdlinep = new_cmdline;
5258
5259 return src;
5260}
5261
5262/*
5263 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005264 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 */
5266 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005267separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 char_u *p;
5270
Bram Moolenaar86b68352004-12-27 21:59:20 +00005271#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005272 p = skip_grep_pat(eap);
5273#else
5274 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005275#endif
5276
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005277 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
5279 if (*p == Ctrl_V)
5280 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005281 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005282 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005284 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005285 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005286 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 break;
5288 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005289
5290#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005291 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005292 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005293 {
5294 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005295 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005296 if (*p == NUL) // stop at NUL after CTRL-V
5297 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005298 }
5299#endif
5300
Bram Moolenaareda29c92022-10-02 12:59:00 +01005301 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005302 // :@" and :*" do not start a comment!
5303 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005304 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005305 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005306 && !(eap->argt & EX_NOTRLCOM)
5307 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5308 || p != eap->arg)
5309 && (eap->cmdidx != CMD_redir
5310 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005311 || (*p == '#'
5312 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005313 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005314 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 || *p == '|' || *p == '\n')
5316 {
5317 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005318 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 * AND 'b' is present in 'cpoptions'.
5320 */
5321 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005322 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005324 if (!keep_backslash)
5325 {
5326 STRMOVE(p - 1, p); // remove the '\'
5327 --p;
5328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330 else
5331 {
5332 eap->nextcmd = check_nextcmd(p);
5333 *p = NUL;
5334 break;
5335 }
5336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005339 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 del_trailing_spaces(eap->arg);
5341}
5342
5343/*
5344 * get + command from ex argument
5345 */
5346 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005347getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348{
5349 char_u *arg = *argp;
5350 char_u *command = NULL;
5351
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005352 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
5354 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005355 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 command = dollar_command;
5357 else
5358 {
5359 command = arg;
5360 arg = skip_cmd_arg(command, TRUE);
5361 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005362 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
5364
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005365 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 *argp = arg;
5367 }
5368 return command;
5369}
5370
5371/*
5372 * Find end of "+command" argument. Skip over "\ " and "\\".
5373 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005374 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005375skip_cmd_arg(
5376 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005377 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 while (*p && !vim_isspace(*p))
5380 {
5381 if (*p == '\\' && p[1] != NUL)
5382 {
5383 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005384 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 else
5386 ++p;
5387 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005388 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390 return p;
5391}
5392
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005393 int
5394get_bad_opt(char_u *p, exarg_T *eap)
5395{
5396 if (STRICMP(p, "keep") == 0)
5397 eap->bad_char = BAD_KEEP;
5398 else if (STRICMP(p, "drop") == 0)
5399 eap->bad_char = BAD_DROP;
5400 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5401 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005402 else
5403 return FAIL;
5404 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005405}
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407/*
5408 * Get "++opt=arg" argument.
5409 * Return FAIL or OK.
5410 */
5411 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005412getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413{
5414 char_u *arg = eap->arg + 2;
5415 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005416 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005419 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5421 {
5422 if (*arg == 'n')
5423 {
5424 arg += 2;
5425 eap->force_bin = FORCE_NOBIN;
5426 }
5427 else
5428 eap->force_bin = FORCE_BIN;
5429 if (!checkforcmd(&arg, "binary", 3))
5430 return FAIL;
5431 eap->arg = skipwhite(arg);
5432 return OK;
5433 }
5434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005436 if (STRNCMP(arg, "edit", 4) == 0)
5437 {
5438 eap->read_edit = TRUE;
5439 eap->arg = skipwhite(arg + 4);
5440 return OK;
5441 }
5442
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 if (STRNCMP(arg, "ff", 2) == 0)
5444 {
5445 arg += 2;
5446 pp = &eap->force_ff;
5447 }
5448 else if (STRNCMP(arg, "fileformat", 10) == 0)
5449 {
5450 arg += 10;
5451 pp = &eap->force_ff;
5452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 else if (STRNCMP(arg, "enc", 3) == 0)
5454 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005455 if (STRNCMP(arg, "encoding", 8) == 0)
5456 arg += 8;
5457 else
5458 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 pp = &eap->force_enc;
5460 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005461 else if (STRNCMP(arg, "bad", 3) == 0)
5462 {
5463 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005464 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 if (pp == NULL || *arg != '=')
5468 return FAIL;
5469
5470 ++arg;
5471 *pp = (int)(arg - eap->cmd);
5472 arg = skip_cmd_arg(arg, FALSE);
5473 eap->arg = skipwhite(arg);
5474 *arg = NUL;
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (pp == &eap->force_ff)
5477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5479 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005480 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005482 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005484 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5486 *p = TOLOWER_ASC(*p);
5487 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005488 else
5489 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005490 // Check ++bad= argument. Must be a single-byte character, "keep" or
5491 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005492 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005493 return FAIL;
5494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
5496 return OK;
5497}
5498
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005500ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501{
5502 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005503 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 * directory for security reasons.
5505 */
5506 if (secure)
5507 {
5508 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005509 eap->errmsg =
5510 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 }
5512 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005513 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 else
5515 do_augroup(eap->arg, eap->forceit);
5516}
5517
5518/*
5519 * ":doautocmd": Apply the automatic commands to the current buffer.
5520 */
5521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005522ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005524 char_u *arg = eap->arg;
5525 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005526 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005527
Bram Moolenaar1610d052016-06-09 22:53:01 +02005528 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005529 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005530 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005531 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534/*
5535 * :[N]bunload[!] [N] [bufname] unload buffer
5536 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5537 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5538 */
5539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005542 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005543 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 eap->errmsg = do_bufdel(
5545 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5546 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5547 : DOBUF_UNLOAD, eap->arg,
5548 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5549}
5550
5551/*
5552 * :[N]buffer [N] to buffer N
5553 * :[N]sbuffer [N] to buffer N
5554 */
5555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005558 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005559 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005561 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 else
5563 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005564 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5566 else
5567 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005568 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005569 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571}
5572
5573/*
5574 * :[N]bmodified [N] to next mod. buffer
5575 * :[N]sbmodified [N] to next mod. buffer
5576 */
5577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005581 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005582 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583}
5584
5585/*
5586 * :[N]bnext [N] to next buffer
5587 * :[N]sbnext [N] split and to next buffer
5588 */
5589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005592 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005593 return;
5594
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005596 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005597 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598}
5599
5600/*
5601 * :[N]bNext [N] to previous buffer
5602 * :[N]bprevious [N] to previous buffer
5603 * :[N]sbNext [N] split and to previous buffer
5604 * :[N]sbprevious [N] split and to previous buffer
5605 */
5606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005607ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005609 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005610 return;
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005613 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005614 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616
5617/*
5618 * :brewind to first buffer
5619 * :bfirst to first buffer
5620 * :sbrewind split and to first buffer
5621 * :sbfirst split and to first buffer
5622 */
5623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005624ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005626 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005627 return;
5628
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005630 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005631 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632}
5633
5634/*
5635 * :blast to last buffer
5636 * :sblast split and to last buffer
5637 */
5638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005641 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005642 return;
5643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005645 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005646 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
Bram Moolenaar7a092242020-04-16 22:10:49 +02005649/*
5650 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005651 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005656 int comment_char = '"';
5657
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005658 if (in_vim9script())
5659 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005660 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005661}
5662
5663/*
5664 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5665 * to "cmd_start" or has a white space character before it.
5666 */
5667 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005668ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005669{
5670 int c = *cmd;
5671
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005672 if (c == NUL || c == '|' || c == '\n')
5673 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005674 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005675 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5676 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005677 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005678 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679}
5680
5681#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5682 || defined(PROTO)
5683/*
5684 * Return the next command, after the first '|' or '\n'.
5685 * Return NULL if not found.
5686 */
5687 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 while (*p != '|' && *p != '\n')
5691 {
5692 if (*p == NUL)
5693 return NULL;
5694 ++p;
5695 }
5696 return (p + 1);
5697}
5698#endif
5699
5700/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005701 * Check if *p is a separator between Ex commands, skipping over white space.
5702 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 */
5704 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005705check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005707 char_u *s = skipwhite(p);
5708
5709 if (*s == '|' || *s == '\n')
5710 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 else
5712 return NULL;
5713}
5714
5715/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005716 * If "eap->nextcmd" is not set, check for a next command at "p".
5717 */
5718 void
5719set_nextcmd(exarg_T *eap, char_u *arg)
5720{
5721 char_u *p = check_nextcmd(arg);
5722
5723 if (eap->nextcmd == NULL)
5724 eap->nextcmd = p;
5725 else if (p != NULL)
5726 // cannot use "| command" inside a {} block
5727 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5728}
5729
5730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 * - if there are more files to edit
5732 * - and this is the last window
5733 * - and forceit not used
5734 * - and not repeated twice on a row
5735 * return FAIL and give error message if 'message' TRUE
5736 * return OK otherwise
5737 */
5738 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005740 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005741 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742{
5743 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5744
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005745 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005746 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 if (message)
5749 {
5750#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005751 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5752 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005754 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005756 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5757 NGETTEXT("%d more file to edit. Quit anyway?",
5758 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5760 return OK;
5761 return FAIL;
5762 }
5763#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005764 semsg(NGETTEXT(e_nr_more_file_to_edit,
5765 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005766 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
5768 return FAIL;
5769 }
5770 return OK;
5771}
5772
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773/*
5774 * Function given to ExpandGeneric() to obtain the list of command names.
5775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005777get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005780 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 return cmdnames[idx].cmd_name;
5782}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005785ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
Bram Moolenaare6850792010-05-14 15:28:44 +02005787 if (*eap->arg == NUL)
5788 {
5789#ifdef FEAT_EVAL
5790 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5791 char_u *p = NULL;
5792
5793 if (expr != NULL)
5794 {
5795 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005796 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005797 --emsg_off;
5798 vim_free(expr);
5799 }
5800 if (p != NULL)
5801 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005802 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005803 vim_free(p);
5804 }
5805 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005806 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005807#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005808 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005809#endif
5810 }
5811 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005812 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005813
5814#ifdef FEAT_VTP
5815 else if (has_vtp_working())
5816 {
5817 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005818 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005819 redrawcmd();
5820 }
5821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822}
5823
5824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005825ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005828 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 do_highlight(eap->arg, eap->forceit, FALSE);
5830}
5831
5832
5833/*
5834 * Call this function if we thought we were going to exit, but we won't
5835 * (because of an error). May need to restore the terminal mode.
5836 */
5837 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005838not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840 exiting = FALSE;
5841 settmode(TMODE_RAW);
5842}
5843
Bram Moolenaar3552e742021-05-29 12:21:58 +02005844 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005845before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5846{
5847 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5848
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005849 // Bail out when autocommands closed the window.
5850 // Refuse to quit when the buffer in the last window is being closed (can
5851 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005852 if (!win_valid(wp)
5853 || curbuf_locked()
5854 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5855 return TRUE;
5856
5857 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5858 {
5859 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005860 // Refuse to quit when locked or when the window was closed or the
5861 // buffer in the last window is being closed (can only happen in
5862 // autocommands).
5863 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005864 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5865 return TRUE;
5866 }
5867
5868 return FALSE;
5869}
5870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005872 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005873 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005874 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005876 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005877ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005879 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005880
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 if (cmdwin_type != 0)
5882 {
5883 cmdwin_result = Ctrl_C;
5884 return;
5885 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005886 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005887 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005888 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005889 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005890 return;
5891 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005892 if (eap->addr_count > 0)
5893 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005894 int wnr = eap->line2;
5895
5896 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5897 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005898 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005899 }
5900 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005901 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005902
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005903 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005904 if (curbuf_locked())
5905 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005906
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005907 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005908 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005909 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
5911#ifdef FEAT_NETBEANS_INTG
5912 netbeansForcedQuit = eap->forceit;
5913#endif
5914
5915 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005916 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 */
5918 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5919 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005920 if ((!buf_hide(wp->w_buffer)
5921 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005922 | (eap->forceit ? CCGD_FORCEIT : 0)
5923 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005925 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
5927 not_exiting();
5928 }
5929 else
5930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005931 // quit last window
5932 // Note: only_one_window() returns true, even so a help window is
5933 // still open. In that case only quit, if no address has been
5934 // specified. Example:
5935 // :h|wincmd w|1q - don't quit
5936 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005937 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005939 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005940#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005942#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005943 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005944 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 }
5946}
5947
5948/*
5949 * ":cquit".
5950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005952ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005954 // this does not always pass on the exit code to the Manx compiler. why?
5955 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956}
5957
5958/*
5959 * ":qall": try to quit all windows
5960 */
5961 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005962ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (cmdwin_type != 0)
5965 {
5966 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005967 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 else
5969 cmdwin_result = K_XF2;
5970 return;
5971 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005972
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005973 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005974 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005975 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005976 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005977 return;
5978 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005979
5980 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005981 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005984 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 getout(0);
5986 not_exiting();
5987}
5988
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989/*
5990 * ":close": close current window, unless it is the last one
5991 */
5992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005993ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005995 win_T *win;
5996 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005998 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006000 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006001 {
6002 if (eap->addr_count == 0)
6003 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006004 else
6005 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006006 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006007 {
6008 winnr++;
6009 if (winnr == eap->line2)
6010 break;
6011 }
6012 if (win == NULL)
6013 win = lastwin;
6014 ex_win_close(eap->forceit, win, NULL);
6015 }
6016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017}
6018
Bram Moolenaar4033c552017-09-16 20:54:51 +02006019#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006020/*
6021 * ":pclose": Close any preview window.
6022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006024ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006025{
6026 win_T *win;
6027
Bram Moolenaar79648732019-07-18 21:43:07 +02006028 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006029 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006030 if (win->w_p_pvw)
6031 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006032 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006033 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006034 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006035# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006036 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006037 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006038# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006039}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006040#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006041
Bram Moolenaarf740b292006-02-16 22:11:02 +00006042/*
6043 * Close window "win" and take care of handling closing the last window for a
6044 * modified buffer.
6045 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006047ex_win_close(
6048 int forceit,
6049 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006050 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 int need_hide;
6053 buf_T *buf = win->w_buffer;
6054
Bram Moolenaarcf844172020-06-26 19:44:06 +02006055 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006056 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006057 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006058 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006059 return;
6060 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006061 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006062 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006065 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006067#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006068 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006070# ifdef FEAT_TERMINAL
6071 if (term_job_running(buf->b_term))
6072 {
6073 if (term_confirm_stop(buf) == FAIL)
6074 return;
6075 // Manually kill the terminal here because this command will
6076 // hide it otherwise.
6077 free_terminal(buf);
6078 need_hide = FALSE;
6079 }
6080 else
6081# endif
6082 {
6083 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006084
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006085 set_bufref(&bufref, buf);
6086 dialog_changed(buf, FALSE);
6087 if (bufref_valid(&bufref) && bufIsChanged(buf))
6088 return;
6089 need_hide = FALSE;
6090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 }
6092 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006095 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 return;
6097 }
6098 }
6099
Bram Moolenaar4033c552017-09-16 20:54:51 +02006100#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006102#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006103
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006104 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006105 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006106 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006107 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006108 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109}
6110
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006112 * Handle the argument for a tabpage related ex command.
6113 * Returns a tabpage number.
6114 * When an error is encountered then eap->errmsg is set.
6115 */
6116 static int
6117get_tabpage_arg(exarg_T *eap)
6118{
6119 int tab_number;
6120 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6121
6122 if (eap->arg && *eap->arg != NUL)
6123 {
6124 char_u *p = eap->arg;
6125 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006126 int relative = 0; // argument +N/-N means: go to N places to the
6127 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006128
6129 if (*p == '-')
6130 {
6131 relative = -1;
6132 p++;
6133 }
6134 else if (*p == '+')
6135 {
6136 relative = 1;
6137 p++;
6138 }
6139
6140 p_save = p;
6141 tab_number = getdigits(&p);
6142
6143 if (relative == 0)
6144 {
6145 if (STRCMP(p, "$") == 0)
6146 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006147 else if (STRCMP(p, "#") == 0)
6148 if (valid_tabpage(lastused_tabpage))
6149 tab_number = tabpage_index(lastused_tabpage);
6150 else
6151 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006152 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006153 tab_number = 0;
6154 goto theend;
6155 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006156 else if (p == p_save || *p_save == '-' || *p != NUL
6157 || tab_number > LAST_TAB_NR)
6158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006159 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006160 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006161 goto theend;
6162 }
6163 }
6164 else
6165 {
6166 if (*p_save == NUL)
6167 tab_number = 1;
6168 else if (p == p_save || *p_save == '-' || *p != NUL
6169 || tab_number == 0)
6170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006171 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006172 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006173 goto theend;
6174 }
6175 tab_number = tab_number * relative + tabpage_index(curtab);
6176 if (!unaccept_arg0 && relative == -1)
6177 --tab_number;
6178 }
6179 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006180 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006181 }
6182 else if (eap->addr_count > 0)
6183 {
6184 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006185 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006186 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006187 tab_number = 0;
6188 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006189 else
6190 {
6191 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006192 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006193 {
6194 --tab_number;
6195 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006196 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006197 }
6198 }
6199 }
6200 else
6201 {
6202 switch (eap->cmdidx)
6203 {
6204 case CMD_tabnext:
6205 tab_number = tabpage_index(curtab) + 1;
6206 if (tab_number > LAST_TAB_NR)
6207 tab_number = 1;
6208 break;
6209 case CMD_tabmove:
6210 tab_number = LAST_TAB_NR;
6211 break;
6212 default:
6213 tab_number = tabpage_index(curtab);
6214 }
6215 }
6216
6217theend:
6218 return tab_number;
6219}
6220
6221/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006222 * ":tabclose": close current tab page, unless it is the last one.
6223 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 */
6225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006226ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006228 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006229 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006230
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006231 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006232 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006233 cmdwin_result = K_IGNORE;
6234 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006235 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006236
6237 if (first_tabpage->tp_next == NULL)
6238 {
6239 emsg(_(e_cannot_close_last_tab_page));
6240 return;
6241 }
6242
6243 if (window_layout_locked(CMD_tabclose))
6244 return;
6245
6246 tab_number = get_tabpage_arg(eap);
6247 if (eap->errmsg != NULL)
6248 return;
6249
6250 tp = find_tabpage(tab_number);
6251 if (tp == NULL)
6252 {
6253 beep_flush();
6254 return;
6255 }
6256 if (tp != curtab)
6257 {
6258 tabpage_close_other(tp, eap->forceit);
6259 return;
6260 }
6261 else if (!text_locked() && !curbuf_locked())
6262 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006263}
6264
6265/*
6266 * ":tabonly": close all tab pages except the current one
6267 */
6268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006269ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006270{
6271 tabpage_T *tp;
6272 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006273 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006274
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006275 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006276 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006277 cmdwin_result = K_IGNORE;
6278 return;
6279 }
6280
6281 if (first_tabpage->tp_next == NULL)
6282 {
6283 msg(_("Already only one tab page"));
6284 return;
6285 }
6286
6287 if (window_layout_locked(CMD_tabonly))
6288 return;
6289
6290 tab_number = get_tabpage_arg(eap);
6291 if (eap->errmsg != NULL)
6292 return;
6293
6294 goto_tabpage(tab_number);
6295 // Repeat this up to a 1000 times, because autocommands may
6296 // mess up the lists.
6297 for (done = 0; done < 1000; ++done)
6298 {
6299 FOR_ALL_TABPAGES(tp)
6300 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006301 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006302 tabpage_close_other(tp, eap->forceit);
6303 // if we failed to close it quit
6304 if (valid_tabpage(tp))
6305 done = 1000;
6306 // start over, "tp" is now invalid
6307 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006308 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006309 if (first_tabpage->tp_next == NULL)
6310 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
6314/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006315 * Close the current tab page.
6316 */
6317 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006318tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006319{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006320 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006321 return;
6322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006323 // First close all the windows but the current one. If that worked then
6324 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006325 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006326 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006327 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006328 ex_win_close(forceit, curwin, NULL);
6329# ifdef FEAT_GUI
6330 need_mouse_correct = TRUE;
6331# endif
6332}
6333
6334/*
6335 * Close tab page "tp", which is not the current tab page.
6336 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006337 * Also takes care of the tab pages line disappearing when closing the
6338 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006339 */
6340 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006341tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006342{
6343 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006344 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006345
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006346 // Limit to 1000 windows, autocommands may add a window while we close
6347 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006348 while (++done < 1000)
6349 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006350 wp = tp->tp_firstwin;
6351 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006352
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006353 // Autocommands may delete the tab page under our fingers and we may
6354 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006355 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006356 break;
6357 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006358
Bram Moolenaar29323592016-07-24 22:04:11 +02006359 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006360}
6361
6362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 * ":only".
6364 */
6365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006366ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006368 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006369 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370# ifdef FEAT_GUI
6371 need_mouse_correct = TRUE;
6372# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006373 if (eap->addr_count > 0)
6374 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006375 win_T *wp;
6376 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006377 for (wp = firstwin; --wnr > 0; )
6378 {
6379 if (wp->w_next == NULL)
6380 break;
6381 else
6382 wp = wp->w_next;
6383 }
6384 win_goto(wp);
6385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 close_others(TRUE, eap->forceit);
6387}
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006390ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006392 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006393 if (eap->skip)
6394 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006395
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006396 if (window_layout_locked(CMD_hide))
6397 return;
6398#ifdef FEAT_GUI
6399 need_mouse_correct = TRUE;
6400#endif
6401 if (eap->addr_count == 0)
6402 win_close(curwin, FALSE); // don't free buffer
6403 else
6404 {
6405 int winnr = 0;
6406 win_T *win;
6407
6408 FOR_ALL_WINDOWS(win)
6409 {
6410 winnr++;
6411 if (winnr == eap->line2)
6412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006414 if (win == NULL)
6415 win = lastwin;
6416 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 }
6418}
6419
6420/*
6421 * ":stop" and ":suspend": Suspend Vim.
6422 */
dbivolaruab16ad32021-12-29 19:41:47 +00006423 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006424ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 /*
6427 * Disallow suspending for "rvim".
6428 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006429 if (check_restricted())
6430 return;
6431
6432 if (!eap->forceit)
6433 autowrite_all();
6434 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6435 windgoto((int)Rows - 1, 0);
6436 out_char('\n');
6437 out_flush();
6438 stoptermcap();
6439 out_flush(); // needed for SUN to restore xterm buffer
6440 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6441 ui_suspend(); // call machine specific function
6442 maketitle();
6443 resettitle(); // force updating the title
6444 starttermcap();
6445 scroll_start(); // scroll screen before redrawing
6446 redraw_later_clear();
6447 shell_resized(); // may have resized window
6448 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449}
6450
6451/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006452 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 */
6454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006455ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006457#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006458 if (not_in_vim9(eap) == FAIL)
6459 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (cmdwin_type != 0)
6462 {
6463 cmdwin_result = Ctrl_C;
6464 return;
6465 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006466 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006467 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006468 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006469 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006470 return;
6471 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006474 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 */
6476 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6477 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006478
6479 // Write the buffer for ":wq" or when it was changed.
6480 // Trigger QuitPre and ExitPre.
6481 // Check if we can exit now, after autocommands have changed things.
6482 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6483 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006485 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 {
6487 not_exiting();
6488 }
6489 else
6490 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006491 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006493 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494# ifdef FEAT_GUI
6495 need_mouse_correct = TRUE;
6496# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006497 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006498 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 }
6500}
6501
6502/*
6503 * ":print", ":list", ":number".
6504 */
6505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006506ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006508 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006509 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006510 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006512 for ( ;!got_int; ui_breakcheck())
6513 {
6514 print_line(eap->line1,
6515 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6516 || (eap->flags & EXFLAG_NR)),
6517 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6518 if (++eap->line1 > eap->line2)
6519 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006520 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006521 }
6522 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006523 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006524 curwin->w_cursor.lnum = eap->line2;
6525 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
6530
6531#ifdef FEAT_BYTEOFF
6532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006533ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
6535 goto_byte(eap->line2);
6536}
6537#endif
6538
6539/*
6540 * ":shell".
6541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006543ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
6545 do_shell(NULL, 0);
6546}
6547
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006548#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006550static int drop_busy = FALSE;
6551static int drop_filec;
6552static char_u **drop_filev = NULL;
6553static int drop_split;
6554static void (*drop_callback)(void *);
6555static void *drop_cookie;
6556
6557 static void
6558handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 exarg_T ea;
6561 int save_msg_scroll = msg_scroll;
6562
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006563 // Setting the argument list may cause screen updates and being called
6564 // recursively. Avoid that by setting drop_busy.
6565 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006567 // Check whether the current buffer is changed. If so, we will need
6568 // to split the current window or data could be lost.
6569 // We don't need to check if the 'hidden' option is set, as in this
6570 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006571 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 {
6573 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006574 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 --emsg_off;
6576 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006577 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 if (win_split(0, 0) == FAIL)
6580 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006581 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006583 // When splitting the window, create a new alist. Otherwise the
6584 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 alist_unlink(curwin->w_alist);
6586 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 }
6588
6589 /*
6590 * Set up the new argument list.
6591 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006592 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593
6594 /*
6595 * Move to the first file.
6596 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006597 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006598 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 ea.cmd = (char_u *)"next";
6600 do_argfile(&ea, 0);
6601
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006602 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6603 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 need_start_insertmode = FALSE;
6605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006606 // Restore msg_scroll, otherwise a following command may cause scrolling
6607 // unexpectedly. The screen will be redrawn by the caller, thus
6608 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006610
6611 if (drop_callback != NULL)
6612 drop_callback(drop_cookie);
6613
6614 drop_filev = NULL;
6615 drop_busy = FALSE;
6616}
6617
6618/*
6619 * Handle a file drop. The code is here because a drop is *nearly* like an
6620 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006621 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006622 * code is very similar to :args and hence needs access to a lot of the static
6623 * functions in this file.
6624 *
6625 * The "filev" list must have been allocated using alloc(), as should each item
6626 * in the list. This function takes over responsibility for freeing the "filev"
6627 * list.
6628 */
6629 void
6630handle_drop(
6631 int filec, // the number of files dropped
6632 char_u **filev, // the list of files dropped
6633 int split, // force splitting the window
6634 void (*callback)(void *), // to be called after setting the argument
6635 // list
6636 void *cookie) // argument for "callback" (allocated)
6637{
6638 // Cannot handle recursive drops, finish the pending one.
6639 if (drop_busy)
6640 {
6641 FreeWild(filec, filev);
6642 vim_free(cookie);
6643 return;
6644 }
6645
6646 // When calling handle_drop() more than once in a row we only use the last
6647 // one.
6648 if (drop_filev != NULL)
6649 {
6650 FreeWild(drop_filec, drop_filev);
6651 vim_free(drop_cookie);
6652 }
6653
6654 drop_filec = filec;
6655 drop_filev = filev;
6656 drop_split = split;
6657 drop_callback = callback;
6658 drop_cookie = cookie;
6659
6660 // Postpone this when:
6661 // - editing the command line
6662 // - not possible to change the current buffer
6663 // - updating the screen
6664 // As it may change buffers and window structures that are in use and cause
6665 // freed memory to be used.
6666 if (text_locked() || curbuf_locked() || updating_screen)
6667 return;
6668
6669 handle_drop_internal();
6670}
6671
6672/*
6673 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6674 * reset: Handle a postponed drop.
6675 */
6676 void
6677handle_any_postponed_drop(void)
6678{
6679 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006680 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006681 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682}
6683#endif
6684
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 * ":preserve".
6687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006689ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006691 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 ml_preserve(curbuf, TRUE);
6693}
6694
6695/*
6696 * ":recover".
6697 */
6698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006699ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006701 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006703 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6704 | CCGD_MULTWIN
6705 | (eap->forceit ? CCGD_FORCEIT : 0)
6706 | CCGD_EXCMD)
6707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 && (*eap->arg == NUL
6709 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006710 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 recoverymode = FALSE;
6712}
6713
6714/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006715 * Command modifier used in a wrong way. Also for other commands that can't
6716 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 */
6718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006719ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006721 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722}
6723
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724/*
6725 * :sview [+command] file split window with new file, read-only
6726 * :split [[+command] file] split window with current or new file
6727 * :vsplit [[+command] file] split window vertically with current or new file
6728 * :new [[+command] file] split window with no or new file
6729 * :vnew [[+command] file] split vertically window with no or new file
6730 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006731 *
6732 * :tabedit open new Tab page with empty window
6733 * :tabedit [+command] file open new Tab page and edit "file"
6734 * :tabnew [[+command] file] just like :tabedit
6735 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 */
6737 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006738ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006740 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006742#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006743 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006744 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006745#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006746 int use_tab = eap->cmdidx == CMD_tabedit
6747 || eap->cmdidx == CMD_tabfind
6748 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006750 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006751 return;
6752
Bram Moolenaar4033c552017-09-16 20:54:51 +02006753#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
Bram Moolenaar4033c552017-09-16 20:54:51 +02006757#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006758 // A ":split" in the quickfix window works like ":new". Don't want two
6759 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006760 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {
6762 if (eap->cmdidx == CMD_split)
6763 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 if (eap->cmdidx == CMD_vsplit)
6765 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006769 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {
6771 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6772 FNAME_MESS, TRUE, curbuf->b_ffname);
6773 if (fname == NULL)
6774 goto theend;
6775 eap->arg = fname;
6776 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006777# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006778 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 && eap->cmdidx != CMD_new)
6781 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006782 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006783# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006784 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006785# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006786 au_has_group((char_u *)"FileExplorer"))
6787 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006788 // No browsing supported but we do have the file explorer:
6789 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006790 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006791 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006792 }
6793 else
6794 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006795 fname = do_browse(0, (char_u *)(use_tab
6796 ? _("Edit File in new tab page")
6797 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006799 if (fname == NULL)
6800 goto theend;
6801 eap->arg = fname;
6802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006804 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006807 /*
6808 * Either open new tab page or split the window.
6809 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006810 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006811 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006812 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006813 : eap->addr_count == 0 ? 0
6814 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006815 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006816 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006817
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006818 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006819 if (curwin != old_curwin
6820 && win_valid(old_curwin)
6821 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006822 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006823 old_curwin->w_alt_fnum = curbuf->b_fnum;
6824 }
6825 }
6826 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6828 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006829 // Reset 'scrollbind' when editing another file, but keep it when
6830 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006831 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006832 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 else
6834 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 do_exedit(eap, old_curwin);
6836 }
6837
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006838# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006839 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842theend:
6843 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006845
6846/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006847 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006848 */
6849 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006850tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006851{
6852 exarg_T ea;
6853
Bram Moolenaara80faa82020-04-12 19:37:17 +02006854 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006855 ea.cmdidx = CMD_tabnew;
6856 ea.cmd = (char_u *)"tabn";
6857 ea.arg = (char_u *)"";
6858 ex_splitview(&ea);
6859}
6860
6861/*
6862 * :tabnext command
6863 */
6864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006865ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006866{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006867 int tab_number;
6868
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006869 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006870 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006871 switch (eap->cmdidx)
6872 {
6873 case CMD_tabfirst:
6874 case CMD_tabrewind:
6875 goto_tabpage(1);
6876 break;
6877 case CMD_tablast:
6878 goto_tabpage(9999);
6879 break;
6880 case CMD_tabprevious:
6881 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006882 if (eap->arg && *eap->arg != NUL)
6883 {
6884 char_u *p = eap->arg;
6885 char_u *p_save = p;
6886
6887 tab_number = getdigits(&p);
6888 if (p == p_save || *p_save == '-' || *p != NUL
6889 || tab_number == 0)
6890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006891 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006892 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006893 return;
6894 }
6895 }
6896 else
6897 {
6898 if (eap->addr_count == 0)
6899 tab_number = 1;
6900 else
6901 {
6902 tab_number = eap->line2;
6903 if (tab_number < 1)
6904 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006905 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006906 return;
6907 }
6908 }
6909 }
6910 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006911 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006912 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006913 tab_number = get_tabpage_arg(eap);
6914 if (eap->errmsg == NULL)
6915 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006916 break;
6917 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006918}
6919
6920/*
6921 * :tabmove command
6922 */
6923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006925{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006926 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006927
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006928 tab_number = get_tabpage_arg(eap);
6929 if (eap->errmsg == NULL)
6930 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006931}
6932
6933/*
6934 * :tabs command: List tabs and their contents.
6935 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006937ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006938{
6939 tabpage_T *tp;
6940 win_T *wp;
6941 int tabcount = 1;
6942
6943 msg_start();
6944 msg_scroll = TRUE;
6945 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6946 {
6947 msg_putchar('\n');
6948 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006949 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006950 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006951 ui_breakcheck();
6952
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006953 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006954 wp = firstwin;
6955 else
6956 wp = tp->tp_firstwin;
6957 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6958 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006959 msg_putchar('\n');
6960 msg_putchar(wp == curwin ? '>' : ' ');
6961 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006962 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6963 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006964 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006965 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006966 else
6967 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6968 IObuff, IOSIZE, TRUE);
6969 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006970 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006971 ui_breakcheck();
6972 }
6973 }
6974}
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976/*
6977 * ":mode": Set screen mode.
6978 * If no argument given, just get the screen size and redraw.
6979 */
6980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006981ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983 if (*eap->arg == NUL)
6984 shell_resized();
6985 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006986 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987}
6988
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989/*
6990 * ":resize".
6991 * set, increment or decrement current window height
6992 */
6993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 int n;
6997 win_T *wp = curwin;
6998
6999 if (eap->addr_count > 0)
7000 {
7001 n = eap->line2;
7002 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7003 ;
7004 }
7005
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007006# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007008# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007010 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
7012 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007013 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007014 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007016 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 {
7020 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007021 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007022 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007024 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 }
7026}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
7028/*
7029 * ":find [+command] <file>" command.
7030 */
7031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007032ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 char_u *fname;
7035 int count;
7036
7037 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7038 TRUE, curbuf->b_ffname);
7039 if (eap->addr_count > 0)
7040 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007041 // Repeat finding the file "count" times. This matters when it
7042 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 count = eap->line2;
7044 while (fname != NULL && --count > 0)
7045 {
7046 vim_free(fname);
7047 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7048 FALSE, curbuf->b_ffname);
7049 }
7050 }
7051
7052 if (fname != NULL)
7053 {
7054 eap->arg = fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 do_exedit(eap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 vim_free(fname);
7057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058}
7059
7060/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007061 * ":open" simulation: for now just work like ":visual".
7062 */
7063 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007064ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007065{
7066 regmatch_T regmatch;
7067 char_u *p;
7068
Bram Moolenaar65088802021-03-13 21:07:21 +01007069#ifdef FEAT_EVAL
7070 if (not_in_vim9(eap) == FAIL)
7071 return;
7072#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007073 curwin->w_cursor.lnum = eap->line2;
7074 beginline(BL_SOL | BL_FIX);
7075 if (*eap->arg == '/')
7076 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007077 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007078 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007079 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007080 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007081 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007082 if (regmatch.regprog != NULL)
7083 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007084 // make a copy of the line, when searching for a mark it might be
7085 // flushed
7086 char_u *line = vim_strsave(ml_get_curline());
7087
Bram Moolenaardf177f62005-02-22 08:39:57 +00007088 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007089 if (vim_regexec(&regmatch, line, (colnr_T)0))
7090 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007091 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007092 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007093 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007094 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007095 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007096 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007097 eap->arg += STRLEN(eap->arg);
7098 }
7099 check_cursor();
7100
7101 eap->cmdidx = CMD_visual;
7102 do_exedit(eap, NULL);
7103}
7104
7105/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007106 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 */
7108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007109ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110{
7111 do_exedit(eap, NULL);
7112}
7113
7114/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007115 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007118do_exedit(
7119 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007120 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007124 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007126 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7127 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007128 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 /*
7130 * ":vi" command ends Ex mode.
7131 */
7132 if (exmode_active && (eap->cmdidx == CMD_visual
7133 || eap->cmdidx == CMD_view))
7134 {
7135 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007136 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007139 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007140 if (global_busy)
7141 {
7142 int rd = RedrawingDisabled;
7143 int nwr = no_wait_return;
7144 int ms = msg_scroll;
7145#ifdef FEAT_GUI
7146 int he = hold_gui_events;
7147#endif
7148
7149 if (eap->nextcmd != NULL)
7150 {
7151 stuffReadbuff(eap->nextcmd);
7152 eap->nextcmd = NULL;
7153 }
7154
7155 if (exmode_was != EXMODE_VIM)
7156 settmode(TMODE_RAW);
7157 RedrawingDisabled = 0;
7158 no_wait_return = 0;
7159 need_wait_return = FALSE;
7160 msg_scroll = 0;
7161#ifdef FEAT_GUI
7162 hold_gui_events = 0;
7163#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007164 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007165 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007166
7167 main_loop(FALSE, TRUE);
7168
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007169 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007170 RedrawingDisabled = rd;
7171 no_wait_return = nwr;
7172 msg_scroll = ms;
7173#ifdef FEAT_GUI
7174 hold_gui_events = he;
7175#endif
7176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 }
7180
7181 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007182 || eap->cmdidx == CMD_tabnew
7183 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007184 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007186 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 setpcmark();
7188 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007189 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7190 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007192 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 || *eap->arg != NUL
7194#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007195 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196#endif
7197 )
7198 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007199 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7200 // Only ":edit" or ":script" can bring us here, others are stopped
7201 // earlier.
7202 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007203 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007204
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 n = readonlymode;
7206 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7207 readonlymode = TRUE;
7208 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007209 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7210 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007211 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7212 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7214 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007215 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7217 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7218 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007219 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007221 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007222 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007223 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7224 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007225 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007227 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 if (old_curwin != NULL)
7229 {
7230 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007231 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007233#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007234 cleanup_T cs;
7235
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007236 // Reset the error/interrupt/exception state here so that
7237 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007238 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007239#endif
7240#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007242#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007243 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007244
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007245#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007246 // Restore the error/interrupt/exception state if not
7247 // discarded by a new aborting error, interrupt, or
7248 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007249 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 }
7252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
7254 else if (readonlymode && curbuf->b_nwindows == 1)
7255 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007256 // When editing an already visited buffer, 'readonly' won't be set
7257 // but the previous value is kept. With ":view" and ":sview" we
7258 // want the file to be readonly, except when another window is
7259 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 curbuf->b_p_ro = TRUE;
7261 }
7262 readonlymode = n;
7263 }
7264 else
7265 {
7266 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007267 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 if (n != curwin->w_arg_idx_invalid)
7271 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
7273
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 /*
7275 * if ":split file" worked, set alternate file name in old window to new
7276 * file
7277 */
7278 if (old_curwin != NULL
7279 && *eap->arg != NUL
7280 && curwin != old_curwin
7281 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007282 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007283 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
7286 ex_no_reprint = TRUE;
7287}
7288
7289#ifndef FEAT_GUI
7290/*
7291 * ":gui" and ":gvim" when there is no GUI.
7292 */
7293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007294ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007296 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297}
7298#endif
7299
Bram Moolenaar4f974752019-02-17 17:44:42 +01007300#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007302ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 gui_make_tearoff(eap->arg);
7305}
7306#endif
7307
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007308#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7309 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007311ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007313# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7314 if (gui.in_use)
7315 gui_make_popup(eap->arg, eap->forceit);
7316# ifdef FEAT_TERM_POPUP_MENU
7317 else
7318# endif
7319# endif
7320# ifdef FEAT_TERM_POPUP_MENU
7321 pum_make_popup(eap->arg, eap->forceit);
7322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323}
7324#endif
7325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007327ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
7329 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007330 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007332 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333}
7334
7335/*
7336 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7337 * offset.
7338 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007341ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007344 win_T *save_curwin = curwin;
7345 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 long topline;
7347 long y;
7348 linenr_T old_linenr = curwin->w_cursor.lnum;
7349
7350 setpcmark();
7351
7352 /*
7353 * determine max topline
7354 */
7355 if (curwin->w_p_scb)
7356 {
7357 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007358 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
7360 if (wp->w_p_scb && wp->w_buffer)
7361 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007362 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if (topline > y)
7364 topline = y;
7365 }
7366 }
7367 if (topline < 1)
7368 topline = 1;
7369 }
7370 else
7371 {
7372 topline = 1;
7373 }
7374
7375
7376 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007377 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007379 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 if (curwin->w_p_scb)
7382 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007383 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 y = topline - curwin->w_topline;
7385 if (y > 0)
7386 scrollup(y, TRUE);
7387 else
7388 scrolldown(-y, TRUE);
7389 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007390 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 }
7394 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007395 curwin = save_curwin;
7396 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 if (curwin->w_p_scb)
7398 {
7399 did_syncbind = TRUE;
7400 checkpcmark();
7401 if (old_linenr != curwin->w_cursor.lnum)
7402 {
7403 char_u ctrl_o[2];
7404
7405 ctrl_o[0] = Ctrl_O;
7406 ctrl_o[1] = 0;
7407 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7408 }
7409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410}
7411
7412
7413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007414ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007416 int i;
7417 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7418 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007420 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007422 do_bang(1, eap, FALSE, FALSE, TRUE);
7423 return;
7424 }
7425
7426 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7427 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
7429#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007430 if (cmdmod.cmod_flags & CMOD_BROWSE)
7431 {
7432 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007434 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7435 NULL, NULL, NULL, curbuf);
7436 if (browseFile != NULL)
7437 {
7438 i = readfile(browseFile, NULL,
7439 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7440 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 }
7442 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007443 i = OK;
7444 }
7445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007447 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007449 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 return;
7451 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007452 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
7454 else
7455 {
7456 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7457 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7458 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007459 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
7461 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007462 if (i != OK)
7463 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007464#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007465 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007467 semsg(_(e_cant_open_file_str), eap->arg);
7468 }
7469 else
7470 {
7471 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007472 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007473 // Delete the empty line that remains. Historically ex does
7474 // this but vi doesn't.
7475 if (eap->line2 == 0)
7476 lnum = curbuf->b_ml.ml_line_count;
7477 else
7478 lnum = 1;
7479 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007480 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007481 ml_delete(lnum);
7482 if (curwin->w_cursor.lnum > 1
7483 && curwin->w_cursor.lnum >= lnum)
7484 --curwin->w_cursor.lnum;
7485 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007486 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007487 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007488 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
7490}
7491
Bram Moolenaarea408852005-06-25 22:49:46 +00007492static char_u *prev_dir = NULL;
7493
7494#if defined(EXITFREE) || defined(PROTO)
7495 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007496free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007497{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007498 VIM_CLEAR(prev_dir);
7499 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007500}
7501#endif
7502
Bram Moolenaarf4258302013-06-02 18:20:17 +02007503/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007504 * Get the previous directory for the given chdir scope.
7505 */
7506 static char_u *
7507get_prevdir(cdscope_T scope)
7508{
7509 if (scope == CDSCOPE_WINDOW)
7510 return curwin->w_prevdir;
7511 else if (scope == CDSCOPE_TABPAGE)
7512 return curtab->tp_prevdir;
7513 return prev_dir;
7514}
7515
7516/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007517 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007518 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7519 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007520 */
7521 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007522post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007523{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007524 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007525 // Clear tab local directory for both :cd and :tcd
7526 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007527 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007528 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007529 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007530 char_u *pdir = get_prevdir(scope);
7531
7532 // If still in the global directory, need to remember current
7533 // directory as the global directory.
7534 if (globaldir == NULL && pdir != NULL)
7535 globaldir = vim_strsave(pdir);
7536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007537 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007538 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007539 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007540 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007541 curtab->tp_localdir = vim_strsave(NameBuff);
7542 else
7543 curwin->w_localdir = vim_strsave(NameBuff);
7544 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007545 }
7546 else
7547 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007548 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007549 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007550 }
7551
zeertzjq64be6aa2021-11-19 11:59:08 +00007552 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007553 shorten_fnames(TRUE);
7554}
7555
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007556/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007557 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7558 */
7559 void
7560trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7561{
7562#ifdef FEAT_EVAL
7563 dict_T *v_event;
7564 save_v_event_T save_v_event;
7565
7566 v_event = get_v_event(&save_v_event);
7567 (void)dict_add_string(v_event, "directory", new_dir);
7568 dict_set_items_ro(v_event);
7569#endif
7570 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7571#ifdef FEAT_EVAL
7572 restore_v_event(v_event, &save_v_event);
7573#endif
7574}
7575
7576/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007577 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007578 * chdir() function.
7579 * scope == CDSCOPE_WINDOW: changes the window-local directory
7580 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7581 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007582 * Returns TRUE if the directory is successfully changed.
7583 */
7584 int
7585changedir_func(
7586 char_u *new_dir,
7587 int forceit,
7588 cdscope_T scope)
7589{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007590 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007591 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007592 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007593 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007594 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007595
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007596 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007597 return FALSE;
7598
7599 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7600 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007601 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007602 return FALSE;
7603 }
7604
7605 // ":cd -": Change to previous directory
7606 if (STRCMP(new_dir, "-") == 0)
7607 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007608 pdir = get_prevdir(scope);
7609 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007610 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007611 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007612 return FALSE;
7613 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007614 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007615 }
7616
7617 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007618 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007619 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007620 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007621 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007622
Bakudankun29f3a452021-12-11 12:28:08 +00007623 // For UNIX ":cd" means: go to home directory.
7624 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007625#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007626 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007627#else
7628 if (*new_dir == NUL && p_cdh)
7629#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007630 {
7631 // use NameBuff for home directory name
7632# ifdef VMS
7633 char_u *p;
7634
7635 p = mch_getenv((char_u *)"SYS$LOGIN");
7636 if (p == NULL || *p == NUL) // empty is the same as not set
7637 NameBuff[0] = NUL;
7638 else
7639 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7640# else
7641 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7642# endif
7643 new_dir = NameBuff;
7644 }
zeertzjqf38aad82021-12-30 13:45:57 +00007645 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007646 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7647 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007648 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007649 if (scope == CDSCOPE_WINDOW)
7650 acmd_fname = (char_u *)"window";
7651 else if (scope == CDSCOPE_TABPAGE)
7652 acmd_fname = (char_u *)"tabpage";
7653 else
7654 acmd_fname = (char_u *)"global";
7655 trigger_DirChangedPre(acmd_fname, new_dir);
7656
7657 if (vim_chdir(new_dir))
7658 {
7659 emsg(_(e_command_failed));
7660 vim_free(pdir);
7661 return FALSE;
7662 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007663 }
zeertzjq73257142022-02-02 13:16:37 +00007664
7665 if (scope == CDSCOPE_WINDOW)
7666 pp = &curwin->w_prevdir;
7667 else if (scope == CDSCOPE_TABPAGE)
7668 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007669 else
zeertzjq73257142022-02-02 13:16:37 +00007670 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007671 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007672 *pp = pdir;
7673
7674 post_chdir(scope);
7675
7676 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007677 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007678 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007679 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007680}
Bram Moolenaarea408852005-06-25 22:49:46 +00007681
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007683 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007685 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007686ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007688 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689
7690 new_dir = eap->arg;
7691#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007692 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7693 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007695 ex_pwd(NULL);
7696 return;
7697 }
7698#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007699
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007700 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007701
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007702 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7703 scope = CDSCOPE_WINDOW;
7704 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7705 scope = CDSCOPE_TABPAGE;
7706
7707 if (changedir_func(new_dir, eap->forceit, scope))
7708 {
7709 // Echo the new current directory if the command was typed.
7710 if (KeyTyped || p_verbose >= 5)
7711 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
7713}
7714
7715/*
7716 * ":pwd".
7717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007719ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
7721 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7722 {
7723#ifdef BACKSLASH_IN_FILENAME
7724 slash_adjust(NameBuff);
7725#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007726 if (p_verbose > 0)
7727 {
7728 char *context = "global";
7729
Bram Moolenaar05268152021-11-18 18:53:45 +00007730 if (last_chdir_reason != NULL)
7731 context = last_chdir_reason;
7732 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007733 context = "window";
7734 else if (curtab->tp_localdir != NULL)
7735 context = "tabpage";
7736 smsg("[%s] %s", context, (char *)NameBuff);
7737 }
7738 else
7739 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 }
7741 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007742 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
7745/*
7746 * ":=".
7747 */
7748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007749ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007751 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007752 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753}
7754
7755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007756ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007758 int n;
7759 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
7761 if (cursor_valid())
7762 {
7763 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7764 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007765 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007767
7768 len = eap->line2;
7769 switch (*eap->arg)
7770 {
7771 case 'm': break;
7772 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007773 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007774 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007775
7776 // Hide the cursor if invoked with !
7777 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778}
7779
7780/*
7781 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007782 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 */
7784 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007785do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
Bram Moolenaar52953192019-08-16 10:27:13 +02007787 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007788 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007789# ifdef ELAPSED_FUNC
7790 elapsed_T start_tv;
7791
7792 // Remember at what time we started, so that we know how much longer we
7793 // should wait after waiting for a bit.
7794 ELAPSED_INIT(start_tv);
7795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007797 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007798 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007799 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007800 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007801
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007802 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007803 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007805 wait_now = msec - done > 1000L ? 1000L : msec - done;
7806#ifdef FEAT_TIMERS
7807 {
7808 long due_time = check_due_timer();
7809
7810 if (due_time > 0 && due_time < wait_now)
7811 wait_now = due_time;
7812 }
7813#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007814#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007815 if (has_any_channel() && wait_now > 20L)
7816 wait_now = 20L;
7817#endif
7818#ifdef FEAT_SOUND
7819 if (has_any_sound_callback() && wait_now > 20L)
7820 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007821#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007822 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007823
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007824#ifdef FEAT_JOB_CHANNEL
7825 if (has_any_channel())
7826 ui_breakcheck_force(TRUE);
7827 else
7828#endif
7829 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007830#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007831 // Process the netbeans and clientserver messages that may have been
7832 // received in the call to ui_breakcheck() when the GUI is in use. This
7833 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007834 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007835#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007836
7837# ifdef ELAPSED_FUNC
7838 // actual time passed
7839 done = ELAPSED_FUNC(start_tv);
7840# else
7841 // guestimate time passed (will actually be more)
7842 done += wait_now;
7843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007845
7846 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7847 // input buffer, otherwise a following call to input() fails.
7848 if (got_int)
7849 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007850
7851 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007852 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853}
7854
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855/*
7856 * ":winsize" command (obsolete).
7857 */
7858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007859ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 int w, h;
7862 char_u *arg = eap->arg;
7863 char_u *p;
7864
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007865 if (!isdigit(*arg))
7866 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007867 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007868 return;
7869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 w = getdigits(&arg);
7871 arg = skipwhite(arg);
7872 p = arg;
7873 h = getdigits(&arg);
7874 if (*p != NUL && *arg == NUL)
7875 set_shellsize(w, h, TRUE);
7876 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007877 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878}
7879
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007881ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
7883 int xchar = NUL;
7884 char_u *p;
7885
7886 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007888 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 if (eap->arg[1] == NUL)
7890 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007891 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 return;
7893 }
7894 xchar = eap->arg[1];
7895 p = eap->arg + 2;
7896 }
7897 else
7898 p = eap->arg + 1;
7899
Bram Moolenaar63b91732021-08-05 20:40:03 +02007900 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007902 if (*p != NUL && *p != (
7903#ifdef FEAT_EVAL
7904 in_vim9script() ? '#' :
7905#endif
7906 '"')
7907 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007908 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007909 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007911 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007912 postponed_split_flags = cmdmod.cmod_split;
7913 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7915 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007916 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 }
7918}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
Bram Moolenaar843ee412004-06-30 16:16:41 +00007920#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921/*
7922 * ":winpos".
7923 */
7924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007925ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 int x, y;
7928 char_u *arg = eap->arg;
7929 char_u *p;
7930
7931 if (*arg == NUL)
7932 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007933# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007934# ifdef VIMDLL
7935 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7936 mch_get_winpos(&x, &y) != FAIL)
7937# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007939# else
7940 if (mch_get_winpos(&x, &y) != FAIL)
7941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
7943 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007944 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 }
7946 else
7947# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007948 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 }
7950 else
7951 {
7952 x = getdigits(&arg);
7953 arg = skipwhite(arg);
7954 p = arg;
7955 y = getdigits(&arg);
7956 if (*p == NUL || *arg != NUL)
7957 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007958 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 return;
7960 }
7961# ifdef FEAT_GUI
7962 if (gui.in_use)
7963 gui_mch_set_winpos(x, y);
7964 else if (gui.starting)
7965 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007966 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 gui_win_x = x;
7968 gui_win_y = y;
7969 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007970# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 else
7972# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007973# endif
7974# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007975 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976# endif
7977# ifdef HAVE_TGETENT
7978 if (*T_CWP)
7979 term_set_winpos(x, y);
7980# endif
7981 }
7982}
7983#endif
7984
7985/*
7986 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7987 */
7988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007989ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
7991 oparg_T oa;
7992
7993 clear_oparg(&oa);
7994 oa.regname = eap->regname;
7995 oa.start.lnum = eap->line1;
7996 oa.end.lnum = eap->line2;
7997 oa.line_count = eap->line2 - eap->line1 + 1;
7998 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008000 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
8002 setpcmark();
8003 curwin->w_cursor.lnum = eap->line1;
8004 beginline(BL_SOL | BL_FIX);
8005 }
8006
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008007 if (VIsual_active)
8008 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008009
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 switch (eap->cmdidx)
8011 {
8012 case CMD_delete:
8013 oa.op_type = OP_DELETE;
8014 op_delete(&oa);
8015 break;
8016
8017 case CMD_yank:
8018 oa.op_type = OP_YANK;
8019 (void)op_yank(&oa, FALSE, TRUE);
8020 break;
8021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008022 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008023 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008025 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8026#else
8027 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008029 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 oa.op_type = OP_RSHIFT;
8031 else
8032 oa.op_type = OP_LSHIFT;
8033 op_shift(&oa, FALSE, eap->amount);
8034 break;
8035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008037 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038}
8039
8040/*
8041 * ":put".
8042 */
8043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008044ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008046 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 if (eap->line2 == 0)
8048 {
8049 eap->line2 = 1;
8050 eap->forceit = TRUE;
8051 }
8052 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008053 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008054 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008055 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056}
8057
8058/*
8059 * Handle ":copy" and ":move".
8060 */
8061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
8064 long n;
8065
Bram Moolenaar491799b2020-08-01 19:23:43 +02008066#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008067 if (not_in_vim9(eap) == FAIL)
8068 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008069#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008070 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008071 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {
8073 eap->nextcmd = NULL;
8074 return;
8075 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008076 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077
8078 /*
8079 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8080 */
8081 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8082 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008083 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 return;
8085 }
8086
8087 if (eap->cmdidx == CMD_move)
8088 {
8089 if (do_move(eap->line1, eap->line2, n) == FAIL)
8090 return;
8091 }
8092 else
8093 ex_copy(eap->line1, eap->line2, n);
8094 u_clearline();
8095 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008096 ex_may_print(eap);
8097}
8098
8099/*
8100 * Print the current line if flags were given to the Ex command.
8101 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008102 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008103ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008104{
8105 if (eap->flags != 0)
8106 {
8107 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8108 (eap->flags & EXFLAG_LIST));
8109 ex_no_reprint = TRUE;
8110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111}
8112
8113/*
8114 * ":smagic" and ":snomagic".
8115 */
8116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008117ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008119 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008121 magic_overruled = eap->cmdidx == CMD_smagic
8122 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008123 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008124 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125}
8126
8127/*
8128 * ":join".
8129 */
8130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008131ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
8133 curwin->w_cursor.lnum = eap->line1;
8134 if (eap->line1 == eap->line2)
8135 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008136 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 return;
8138 if (eap->line2 == curbuf->b_ml.ml_line_count)
8139 {
8140 beep_flush();
8141 return;
8142 }
8143 ++eap->line2;
8144 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008145 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008147 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148}
8149
8150/*
8151 * ":[addr]@r" or ":[addr]*r": execute register
8152 */
8153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008154ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155{
8156 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008157 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
8159 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008160 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161
8162#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008163 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164#endif
8165
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008166 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 c = *eap->arg;
8168 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8169 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008170 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008171 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8172 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008175 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008178 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008180 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008182 /*
8183 * Execute from the typeahead buffer.
8184 * Continue until the stuff buffer is empty and all added characters
8185 * have been consumed.
8186 */
8187 while (!stuff_empty() || typebuf.tb_len > prev_len)
8188 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8189
8190 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191}
8192
8193/*
8194 * ":!".
8195 */
8196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008197ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198{
8199 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8200}
8201
8202/*
8203 * ":undo".
8204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008206ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008208 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008209 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008210 else
8211 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212}
8213
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008214#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008216ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008217{
8218 char_u hash[UNDO_HASH_SIZE];
8219
8220 u_compute_hash(hash);
8221 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8222}
8223
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008225ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008226{
8227 char_u hash[UNDO_HASH_SIZE];
8228
8229 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008230 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008231}
8232#endif
8233
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234/*
8235 * ":redo".
8236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008238ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239{
8240 u_redo(1);
8241}
8242
8243/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008244 * ":earlier" and ":later".
8245 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008247ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008248{
8249 long count = 0;
8250 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008251 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008252 char_u *p = eap->arg;
8253
8254 if (*p == NUL)
8255 count = 1;
8256 else if (isdigit(*p))
8257 {
8258 count = getdigits(&p);
8259 switch (*p)
8260 {
8261 case 's': ++p; sec = TRUE; break;
8262 case 'm': ++p; sec = TRUE; count *= 60; break;
8263 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008264 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8265 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008266 }
8267 }
8268
8269 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008270 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008271 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008272 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8273 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008274}
8275
8276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 * ":redir": start/stop redirection.
8278 */
8279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008280ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281{
8282 char *mode;
8283 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008284 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285
Bram Moolenaarba768492016-07-08 15:32:54 +02008286#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008287 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008288 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008289 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008290 return;
8291 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008292#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008293
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (STRICMP(eap->arg, "END") == 0)
8295 close_redir();
8296 else
8297 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008298 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008300 ++arg;
8301 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008303 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 mode = "a";
8305 }
8306 else
8307 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008308 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
8310 close_redir();
8311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008312 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008313 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (fname == NULL)
8315 return;
8316#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008317 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
8319 char_u *browseFile;
8320
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008321 browseFile = do_browse(BROWSE_SAVE,
8322 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008323 fname, NULL, NULL,
8324 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008326 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 vim_free(fname);
8328 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008329 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 }
8331#endif
8332
8333 redir_fd = open_exfile(fname, eap->forceit, mode);
8334 vim_free(fname);
8335 }
8336#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008337 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008339 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008341 ++arg;
8342 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008344 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008345 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008347 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008349 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008350 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008351 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008352 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008354 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008355 if (*arg == '>')
8356 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008357 // Make register empty when not using @A-@Z and the
8358 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008359 if (*arg == NUL && !isupper(redir_reg))
8360 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008362 }
8363 if (*arg != NUL)
8364 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008365 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008366 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008367 }
8368 }
8369 else if (*arg == '=' && arg[1] == '>')
8370 {
8371 int append;
8372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008373 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008374 close_redir();
8375 arg += 2;
8376
8377 if (*arg == '>')
8378 {
8379 ++arg;
8380 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 }
8382 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008383 append = FALSE;
8384
8385 if (var_redir_start(skipwhite(arg), append) == OK)
8386 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 }
8388#endif
8389
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008393 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008395
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008396 // Make sure redirection is not off. Can happen for cmdline completion
8397 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008398 if (redir_fd != NULL
8399#ifdef FEAT_EVAL
8400 || redir_reg || redir_vname
8401#endif
8402 )
8403 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404}
8405
8406/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008407 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008409 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008410ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008412 redraw_cmd(eap->forceit);
8413}
8414
8415/*
8416 * ":redraw": force redraw, with clear if "clear" is TRUE.
8417 */
8418 void
8419redraw_cmd(int clear)
8420{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 int r = RedrawingDisabled;
8422 int p = p_lz;
8423
8424 RedrawingDisabled = 0;
8425 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008426 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008428 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 if (need_maketitle)
8430 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008431#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8432# ifdef VIMDLL
8433 if (!gui.in_use)
8434# endif
8435 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 RedrawingDisabled = r;
8438 p_lz = p;
8439
Bram Moolenaar5017c662022-04-07 18:06:08 +01008440 // After drawing the statusline screen_attr may still be set.
8441 screen_stop_highlight();
8442
Bram Moolenaara2a89732022-08-31 14:46:18 +01008443 // Reset msg_didout, so that a message that's there is overwritten.
8444 msg_didout = FALSE;
8445 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008447 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008448 need_wait_return = FALSE;
8449
Bram Moolenaara653e532022-04-19 11:38:24 +01008450 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008451 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008452 redrawcmdline();
8453
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 out_flush();
8455}
8456
8457/*
8458 * ":redrawstatus": force redraw of status line(s)
8459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008461ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 int r = RedrawingDisabled;
8464 int p = p_lz;
8465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 if (eap->forceit)
8467 status_redraw_all();
8468 else
8469 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008470 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008471 return; // redraw later
8472
8473 RedrawingDisabled = 0;
8474 p_lz = FALSE;
zeertzjq320d9102022-09-20 17:12:13 +01008475 if (State & MODE_CMDLINE)
8476 redraw_statuslines();
8477 else
8478 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 RedrawingDisabled = r;
8480 p_lz = p;
8481 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482}
8483
Bram Moolenaare12bab32019-01-08 22:02:56 +01008484/*
8485 * ":redrawtabline": force redraw of the tabline
8486 */
8487 static void
8488ex_redrawtabline(exarg_T *eap UNUSED)
8489{
8490 int r = RedrawingDisabled;
8491 int p = p_lz;
8492
8493 RedrawingDisabled = 0;
8494 p_lz = FALSE;
8495
8496 draw_tabline();
8497
8498 RedrawingDisabled = r;
8499 p_lz = p;
8500 out_flush();
8501}
8502
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008504close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506 if (redir_fd != NULL)
8507 {
8508 fclose(redir_fd);
8509 redir_fd = NULL;
8510 }
8511#ifdef FEAT_EVAL
8512 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008513 if (redir_vname)
8514 {
8515 var_redir_stop();
8516 redir_vname = 0;
8517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518#endif
8519}
8520
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008521#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008522 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008523vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008524{
8525 if (vim_mkdir(name, prot) != 0)
8526 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008527 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008528 return FAIL;
8529 }
8530 return OK;
8531}
8532#endif
8533
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534/*
8535 * Open a file for writing for an Ex command, with some checks.
8536 * Return file descriptor, or NULL on failure.
8537 */
8538 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008539open_exfile(
8540 char_u *fname,
8541 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008542 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 FILE *fd;
8545
8546#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008547 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 if (mch_isdir(fname))
8549 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008550 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 return NULL;
8552 }
8553#endif
8554 if (!forceit && *mode != 'a' && vim_fexists(fname))
8555 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008556 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 return NULL;
8558 }
8559
8560 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008561 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562
8563 return fd;
8564}
8565
8566/*
8567 * ":mark" and ":k".
8568 */
8569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008570ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 pos_T pos;
8573
Bram Moolenaar10b94212021-02-19 21:42:57 +01008574#ifdef FEAT_EVAL
8575 if (not_in_vim9(eap) == FAIL)
8576 return;
8577#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008578 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008580 emsg(_(e_argument_required));
8581 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008583
8584 if (eap->arg[1] != NUL) // more than one character?
8585 {
8586 semsg(_(e_trailing_characters_str), eap->arg);
8587 return;
8588 }
8589
8590 pos = curwin->w_cursor; // save curwin->w_cursor
8591 curwin->w_cursor.lnum = eap->line2;
8592 beginline(BL_WHITE | BL_FIX);
8593 if (setmark(*eap->arg) == FAIL) // set mark
8594 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8595 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596}
8597
8598/*
8599 * Update w_topline, w_leftcol and the cursor position.
8600 */
8601 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008602update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008604 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 update_topline();
8606 if (!curwin->w_p_wrap)
8607 validate_cursor();
8608 update_curswant();
8609}
8610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008612 * Save the current State and go to Normal mode.
8613 * Return TRUE if the typeahead could be saved.
8614 */
8615 int
8616save_current_state(save_state_T *sst)
8617{
8618 sst->save_msg_scroll = msg_scroll;
8619 sst->save_restart_edit = restart_edit;
8620 sst->save_msg_didout = msg_didout;
8621 sst->save_State = State;
8622 sst->save_insertmode = p_im;
8623 sst->save_finish_op = finish_op;
8624 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008625 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008626 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008627
Bram Moolenaar4324d872020-12-01 20:12:24 +01008628 msg_scroll = FALSE; // no msg scrolling in Normal mode
8629 restart_edit = 0; // don't go to Insert mode
8630 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008631
Bram Moolenaara452b802020-12-01 21:47:59 +01008632 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008633 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008634
8635 /*
8636 * Save the current typeahead. This is required to allow using ":normal"
8637 * from an event handler and makes sure we don't hang when the argument
8638 * ends with half a command.
8639 */
8640 save_typeahead(&sst->tabuf);
8641 return sst->tabuf.typebuf_valid;
8642}
8643
8644 void
8645restore_current_state(save_state_T *sst)
8646{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008647 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008648 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008649
8650 msg_scroll = sst->save_msg_scroll;
8651 restart_edit = sst->save_restart_edit;
8652 p_im = sst->save_insertmode;
8653 finish_op = sst->save_finish_op;
8654 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008655 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008656 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008657 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008658 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008660 // Restore the state (needed when called from a function executed for
8661 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008662 State = sst->save_State;
8663#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008664 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008665#endif
8666}
8667
8668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 * ":normal[!] {commands}": Execute normal mode commands.
8670 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008671 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008672ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008674 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 char_u *arg = NULL;
8676 int l;
8677 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008679 if (ex_normal_lock > 0)
8680 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008681 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008682 return;
8683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 if (ex_normal_busy >= p_mmd)
8685 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008686 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 return;
8688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 /*
8691 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8692 * this for the K_SPECIAL leading byte, otherwise special keys will not
8693 * work.
8694 */
8695 if (has_mbyte)
8696 {
8697 int len = 0;
8698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008699 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 for (p = eap->arg; *p != NUL; ++p)
8701 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008702#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008703 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008705#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008706 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008707 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008708#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 )
8712 len += 2;
8713 }
8714 if (len > 0)
8715 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008716 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 if (arg != NULL)
8718 {
8719 len = 0;
8720 for (p = eap->arg; *p != NUL; ++p)
8721 {
8722 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008723#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 if (*p == CSI)
8725 {
8726 arg[len++] = KS_EXTRA;
8727 arg[len++] = (int)KE_CSI;
8728 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008729#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008730 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
8732 arg[len++] = *++p;
8733 if (*p == K_SPECIAL)
8734 {
8735 arg[len++] = KS_SPECIAL;
8736 arg[len++] = KE_FILLER;
8737 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008738#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 else if (*p == CSI)
8740 {
8741 arg[len++] = KS_EXTRA;
8742 arg[len++] = (int)KE_CSI;
8743 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 }
8746 arg[len] = NUL;
8747 }
8748 }
8749 }
8750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008752 ++ex_normal_busy;
8753 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 {
8755 /*
8756 * Repeat the :normal command for each line in the range. When no
8757 * range given, execute it just once, without positioning the cursor
8758 * first.
8759 */
8760 do
8761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 if (eap->addr_count != 0)
8763 {
8764 curwin->w_cursor.lnum = eap->line1++;
8765 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008766 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 }
8768
Bram Moolenaar13505972019-01-24 15:04:48 +01008769 exec_normal_cmd(arg != NULL
8770 ? arg
8771 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 }
8773 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8774 }
8775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008776 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 update_topline_cursor();
8778
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008779 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008781 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008782#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008783 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008784#endif
8785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787}
8788
8789/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008790 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 */
8792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008793ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008795 if (eap->forceit)
8796 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008797 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008798 if (!curwin->w_cursor.lnum)
8799 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008800 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008801 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008802#ifdef FEAT_TERMINAL
8803 // Ignore this when running in an active terminal.
8804 if (term_job_running(curbuf->b_term))
8805 return;
8806#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008807
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008808 // Ignore the command when already in Insert mode. Inserting an
8809 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01008810 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00008811 return;
8812
Bram Moolenaar64969662005-12-14 21:59:55 +00008813 if (eap->cmdidx == CMD_startinsert)
8814 restart_edit = 'a';
8815 else if (eap->cmdidx == CMD_startreplace)
8816 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008818 restart_edit = 'V';
8819
8820 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008822 if (eap->cmdidx == CMD_startinsert)
8823 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008824 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008826
8827 if (VIsual_active)
8828 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
8831/*
8832 * ":stopinsert"
8833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008835ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836{
8837 restart_edit = 0;
8838 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008839 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008842/*
8843 * Execute normal mode command "cmd".
8844 * "remap" can be REMAP_NONE or REMAP_YES.
8845 */
8846 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008847exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008848{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008849 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008850 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008851 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008852}
Bram Moolenaar25281632016-01-21 23:32:32 +01008853
Bram Moolenaar25281632016-01-21 23:32:32 +01008854/*
8855 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008856 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008857 */
8858 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008859exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008860{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008861 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008862 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008863
Bram Moolenaar905dd902019-04-07 14:21:47 +02008864 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8865 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008866 clear_oparg(&oa);
8867 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008868 while ((!stuff_empty()
8869 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008870 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008871 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008872 {
8873 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008874#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008875 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008876 && oa.op_type == OP_NOP && oa.regname == NUL
8877 && !VIsual_active)
8878 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008879 // If terminal_loop() returns OK we got a key that is handled
8880 // in Normal model. With FAIL we first need to position the
8881 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008882 if (terminal_loop(TRUE) == OK)
8883 normal_cmd(&oa, TRUE);
8884 }
8885 else
8886#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008887 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008888 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008889 }
8890}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008891
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#ifdef FEAT_FIND_ID
8893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008894ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8897 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8898 (linenr_T)1, (linenr_T)MAXLNUM);
8899}
8900
Bram Moolenaar4033c552017-09-16 20:54:51 +02008901#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902/*
8903 * ":psearch"
8904 */
8905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008906ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907{
8908 g_do_tagpreview = p_pvh;
8909 ex_findpat(eap);
8910 g_do_tagpreview = 0;
8911}
8912#endif
8913
8914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008915ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
8917 int whole = TRUE;
8918 long n;
8919 char_u *p;
8920 int action;
8921
8922 switch (cmdnames[eap->cmdidx].cmd_name[2])
8923 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008924 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8926 action = ACTION_GOTO;
8927 else
8928 action = ACTION_SHOW;
8929 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008930 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 action = ACTION_SHOW_ALL;
8932 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008933 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 action = ACTION_GOTO;
8935 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008936 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 action = ACTION_SPLIT;
8938 break;
8939 }
8940
8941 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008942 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 {
8944 n = getdigits(&eap->arg);
8945 eap->arg = skipwhite(eap->arg);
8946 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008947 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 {
8949 whole = FALSE;
8950 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008951 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 if (*p)
8953 {
8954 *p++ = NUL;
8955 p = skipwhite(p);
8956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008957 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008958 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008959 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008961 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 }
8963 }
8964 if (!eap->skip)
8965 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8966 whole, !eap->forceit,
8967 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8968 n, action, eap->line1, eap->line2);
8969}
8970#endif
8971
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972
Bram Moolenaar4033c552017-09-16 20:54:51 +02008973#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974/*
8975 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8976 */
8977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008978ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008980 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8982}
8983
8984/*
8985 * ":pedit"
8986 */
8987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008988ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 win_T *curwin_save = curwin;
8991
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008992 if (ERROR_IF_ANY_POPUP_WINDOW)
8993 return;
8994
Bram Moolenaar026587b2019-08-17 15:08:00 +02008995 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008997 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008998
Bram Moolenaar026587b2019-08-17 15:08:00 +02008999 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009001
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 if (curwin != curwin_save && win_valid(curwin_save))
9003 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009004 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009006 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 win_enter(curwin_save, TRUE);
9008 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009009# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009010 else if (WIN_IS_POPUP(curwin))
9011 {
9012 // can't keep focus in popup window
9013 win_enter(firstwin, TRUE);
9014 }
9015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 g_do_tagpreview = 0;
9017}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
9020/*
9021 * ":stag", ":stselect" and ":stjump".
9022 */
9023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009024ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
9026 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009027 postponed_split_flags = cmdmod.cmod_split;
9028 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9030 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009031 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033
9034/*
9035 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9036 */
9037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009038ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
9040 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9041}
9042
9043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009044ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
9046 int cmd;
9047
9048 switch (name[1])
9049 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009050 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009052 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009054 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009056 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009058 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009060 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009062 case 'f': // ":tfirst"
9063 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009065 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009067 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009069 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009071 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 return;
9073 }
9074#endif
9075 cmd = DT_TAG;
9076 break;
9077 }
9078
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009079 if (name[0] == 'l')
9080 {
9081#ifndef FEAT_QUICKFIX
9082 ex_ni(eap);
9083 return;
9084#else
9085 cmd = DT_LTAG;
9086#endif
9087 }
9088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9090 eap->forceit, TRUE);
9091}
9092
9093/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009094 * Check "str" for starting with a special cmdline variable.
9095 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9096 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9097 */
9098 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009099find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009100{
9101 int len;
9102 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009103 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009104 "%",
9105#define SPEC_PERC 0
9106 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009107#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009108 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009109#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009110 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009111#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009112 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009113#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009114 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009115#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009116 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009117#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009118 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009119#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009120 "<stack>", // call stack
9121#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009122 "<script>", // script file name
9123#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009124 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009125#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009126 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009127#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009128 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009129#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009130 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009131#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009132 "<SID>", // script ID: <SNR>123_
9133#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009134#ifdef FEAT_CLIENTSERVER
9135 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009136# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009137#endif
9138 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009139
K.Takataeeec2542021-06-02 13:28:16 +02009140 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009141 {
9142 len = (int)STRLEN(spec_str[i]);
9143 if (STRNCMP(src, spec_str[i], len) == 0)
9144 {
9145 *usedlen = len;
9146 return i;
9147 }
9148 }
9149 return -1;
9150}
9151
9152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 * Evaluate cmdline variables.
9154 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009155 * change "%" to curbuf->b_ffname
9156 * "#" to curwin->w_alt_fnum
9157 * "%%" to curwin->w_alt_fnum in Vim9 script
9158 * "<cword>" to word under the cursor
9159 * "<cWORD>" to WORD under the cursor
9160 * "<cexpr>" to C-expression under the cursor
9161 * "<cfile>" to path name under the cursor
9162 * "<sfile>" to sourced file name
9163 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009164 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009165 * "<slnum>" to sourced file line number
9166 * "<afile>" to file name for autocommand
9167 * "<abuf>" to buffer number for autocommand
9168 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 *
9170 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9171 * "" for error without a message) and NULL is returned.
9172 * Returns an allocated string if a valid match was found.
9173 * Returns NULL if no match was found. "usedlen" then still contains the
9174 * number of characters to skip.
9175 */
9176 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009177eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009178 char_u *src, // pointer into commandline
9179 char_u *srcstart, // beginning of valid memory for src
9180 int *usedlen, // characters after src that are used
9181 linenr_T *lnump, // line number for :e command, or NULL
9182 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009183 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009184 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009185 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187 int i;
9188 char_u *s;
9189 char_u *result;
9190 char_u *resultbuf = NULL;
9191 int resultlen;
9192 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009193 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009195 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198
9199 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009200 if (escaped != NULL)
9201 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
9203 /*
9204 * Check if there is something to do.
9205 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009206 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009207 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 {
9209 *usedlen = 1;
9210 return NULL;
9211 }
9212
9213 /*
9214 * Skip when preceded with a backslash "\%" and "\#".
9215 * Note: In "\\%" the % is also not recognized!
9216 */
9217 if (src > srcstart && src[-1] == '\\')
9218 {
9219 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009220 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 return NULL;
9222 }
9223
9224 /*
9225 * word or WORD under cursor
9226 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009227 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9228 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009230 resultlen = find_ident_under_cursor(&result,
9231 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9232 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9233 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 if (resultlen == 0)
9235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009236 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 return NULL;
9238 }
9239 }
9240
9241 /*
9242 * '#': Alternate file name
9243 * '%': Current file name
9244 * File name under the cursor
9245 * File name for autocommand
9246 * and following modifiers
9247 */
9248 else
9249 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009250 int off = 0;
9251
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 switch (spec_idx)
9253 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009254 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009255#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009256 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009259 // '%': current file
9260 if (curbuf->b_fname == NULL)
9261 {
9262 result = (char_u *)"";
9263 valid = 0; // Must have ":p:h" to be valid
9264 }
9265 else
9266 {
9267 result = curbuf->b_fname;
9268 tilde_file = STRCMP(result, "~") == 0;
9269 }
9270 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009272#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009273 // "%%" alternate file
9274 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009275#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009276 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009277 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009278 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009280 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 result = arg_all();
9282 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009283 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009284 if (escaped != NULL)
9285 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 break;
9288 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009289 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009290 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009291 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009293 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009294 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009295 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009296 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009298 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009300 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009301 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009302 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009303 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009304 return NULL;
9305 }
9306#ifdef FEAT_EVAL
9307 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9308 (long)i);
9309 if (result == NULL)
9310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009311 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009312 return NULL;
9313 }
9314#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009315 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 }
9319 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009320 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009321 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9322 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009323 buf = buflist_findnr(i);
9324 if (buf == NULL)
9325 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009326 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009327 return NULL;
9328 }
9329 if (lnump != NULL)
9330 *lnump = ECMD_LAST;
9331 if (buf->b_fname == NULL)
9332 {
9333 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009334 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009335 }
9336 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009337 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009338 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009339 tilde_file = STRCMP(result, "~") == 0;
9340 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 break;
9343
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009344 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009345 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 if (result == NULL)
9347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009348 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 return NULL;
9350 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009351 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009354 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009356 if (result != NULL && !autocmd_fname_full)
9357 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009358 // Still need to turn the fname into a full path. It is
9359 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009360 autocmd_fname_full = TRUE;
9361 result = FullName_save(autocmd_fname, FALSE);
9362 vim_free(autocmd_fname);
9363 autocmd_fname = result;
9364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if (result == NULL)
9366 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009367 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 return NULL;
9369 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009370 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 break;
9372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009373 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 if (autocmd_bufnr <= 0)
9375 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009376 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 return NULL;
9378 }
9379 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9380 result = strbuf;
9381 break;
9382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009383 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 result = autocmd_match;
9385 if (result == NULL)
9386 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009387 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 return NULL;
9389 }
9390 break;
9391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009392 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009393 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 if (result == NULL)
9395 {
LemonBoy6013d002022-04-09 21:42:10 +01009396 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9397 return NULL;
9398 }
9399 resultbuf = result; // remember allocated string
9400 break;
9401 case SPEC_STACK: // call stack
9402 result = estack_sfile(ESTACK_STACK);
9403 if (result == NULL)
9404 {
9405 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9406 return NULL;
9407 }
9408 resultbuf = result; // remember allocated string
9409 break;
9410 case SPEC_SCRIPT: // script file name
9411 result = estack_sfile(ESTACK_SCRIPT);
9412 if (result == NULL)
9413 {
9414 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 return NULL;
9416 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009417 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009419
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009420 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009421 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009422 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009423 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009424 return NULL;
9425 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009426 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009427 result = strbuf;
9428 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009429
9430#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009431 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009432 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009433 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009434 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009435 return NULL;
9436 }
9437 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009438 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009439 result = strbuf;
9440 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009441
Bram Moolenaar90944302020-08-01 20:45:11 +02009442 case SPEC_SID:
9443 if (current_sctx.sc_sid <= 0)
9444 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009445 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009446 return NULL;
9447 }
9448 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9449 result = strbuf;
9450 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009451#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009452
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009453#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009454 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009455 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9456 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 result = strbuf;
9458 break;
9459#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009460
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009461 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009462 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 }
9465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009466 resultlen = (int)STRLEN(result); // length of new string
9467 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 {
9469 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 resultlen = (int)(s - result);
9472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 else if (!skip_mod)
9474 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009475 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 &resultlen);
9477 if (result == NULL)
9478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009479 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 return NULL;
9481 }
9482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 }
9484
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009485 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009487 if (empty_is_error)
9488 {
9489 if (valid != VALID_HEAD + VALID_PATH)
9490 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9491 else
9492 *errormsg = _(e_evaluates_to_an_empty_string);
9493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 result = NULL;
9495 }
9496 else
9497 result = vim_strnsave(result, resultlen);
9498 vim_free(resultbuf);
9499 return result;
9500}
9501
9502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 * Expand the <sfile> string in "arg".
9504 *
9505 * Returns an allocated string, or NULL for any error.
9506 */
9507 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009508expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009510 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 int len;
9512 char_u *result;
9513 char_u *newres;
9514 char_u *repl;
9515 int srclen;
9516 char_u *p;
9517
9518 result = vim_strsave(arg);
9519 if (result == NULL)
9520 return NULL;
9521
9522 for (p = result; *p; )
9523 {
9524 if (STRNCMP(p, "<sfile>", 7) != 0)
9525 ++p;
9526 else
9527 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009528 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009529 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 if (errormsg != NULL)
9531 {
9532 if (*errormsg)
9533 emsg(errormsg);
9534 vim_free(result);
9535 return NULL;
9536 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009537 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 {
9539 p += srclen;
9540 continue;
9541 }
9542 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9543 newres = alloc(len);
9544 if (newres == NULL)
9545 {
9546 vim_free(repl);
9547 vim_free(result);
9548 return NULL;
9549 }
9550 mch_memmove(newres, result, (size_t)(p - result));
9551 STRCPY(newres + (p - result), repl);
9552 len = (int)STRLEN(newres);
9553 STRCAT(newres, p + srclen);
9554 vim_free(repl);
9555 vim_free(result);
9556 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009557 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 }
9559 }
9560
9561 return result;
9562}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009565/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009566 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009567 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009570dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 if (fname == NULL)
9573 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009574 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575}
9576#endif
9577
9578/*
9579 * ":behave {mswin,xterm}"
9580 */
9581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009582ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583{
9584 if (STRCMP(eap->arg, "mswin") == 0)
9585 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009586 set_option_value_give_err((char_u *)"selection",
9587 0L, (char_u *)"exclusive", 0);
9588 set_option_value_give_err((char_u *)"selectmode",
9589 0L, (char_u *)"mouse,key", 0);
9590 set_option_value_give_err((char_u *)"mousemodel",
9591 0L, (char_u *)"popup", 0);
9592 set_option_value_give_err((char_u *)"keymodel",
9593 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 }
9595 else if (STRCMP(eap->arg, "xterm") == 0)
9596 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009597 set_option_value_give_err((char_u *)"selection",
9598 0L, (char_u *)"inclusive", 0);
9599 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9600 set_option_value_give_err((char_u *)"mousemodel",
9601 0L, (char_u *)"extend", 0);
9602 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 }
9604 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009605 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606}
9607
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608static int filetype_detect = FALSE;
9609static int filetype_plugin = FALSE;
9610static int filetype_indent = FALSE;
9611
9612/*
9613 * ":filetype [plugin] [indent] {on,off,detect}"
9614 * on: Load the filetype.vim file to install autocommands for file types.
9615 * off: Load the ftoff.vim file to remove all autocommands for file types.
9616 * plugin on: load filetype.vim and ftplugin.vim
9617 * plugin off: load ftplugof.vim
9618 * indent on: load filetype.vim and indent.vim
9619 * indent off: load indoff.vim
9620 */
9621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009622ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 char_u *arg = eap->arg;
9625 int plugin = FALSE;
9626 int indent = FALSE;
9627
9628 if (*eap->arg == NUL)
9629 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009630 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009631 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 filetype_detect ? "ON" : "OFF",
9633 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9634 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9635 return;
9636 }
9637
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009638 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 for (;;)
9640 {
9641 if (STRNCMP(arg, "plugin", 6) == 0)
9642 {
9643 plugin = TRUE;
9644 arg = skipwhite(arg + 6);
9645 continue;
9646 }
9647 if (STRNCMP(arg, "indent", 6) == 0)
9648 {
9649 indent = TRUE;
9650 arg = skipwhite(arg + 6);
9651 continue;
9652 }
9653 break;
9654 }
9655 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9656 {
9657 if (*arg == 'o' || !filetype_detect)
9658 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009659 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 filetype_detect = TRUE;
9661 if (plugin)
9662 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009663 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 filetype_plugin = TRUE;
9665 }
9666 if (indent)
9667 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009668 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 filetype_indent = TRUE;
9670 }
9671 }
9672 if (*arg == 'd')
9673 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009674 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009675 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 }
9677 }
9678 else if (STRCMP(arg, "off") == 0)
9679 {
9680 if (plugin || indent)
9681 {
9682 if (plugin)
9683 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009684 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 filetype_plugin = FALSE;
9686 }
9687 if (indent)
9688 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009689 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 filetype_indent = FALSE;
9691 }
9692 }
9693 else
9694 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009695 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 filetype_detect = FALSE;
9697 }
9698 }
9699 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009700 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
9702
9703/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009704 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 */
9706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009707ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009709 if (did_filetype)
9710 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009711
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009712 char_u *arg = eap->arg;
9713 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9714 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009715
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009716 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9717 if (arg != eap->arg)
9718 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009722ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
9724#ifdef FEAT_DIGRAPHS
9725 if (*eap->arg != NUL)
9726 putdigraph(eap->arg);
9727 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009728 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729#else
mityu61065042021-07-19 20:07:21 +02009730 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731#endif
9732}
9733
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009734#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9735 void
9736set_no_hlsearch(int flag)
9737{
9738 no_hlsearch = flag;
9739# ifdef FEAT_EVAL
9740 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9741# endif
9742}
9743
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744/*
9745 * ":nohlsearch"
9746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009748ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009750 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009751 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753#endif
9754
9755#ifdef FEAT_CRYPT
9756/*
9757 * ":X": Get crypt key
9758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009760ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009762 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009763 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764}
9765#endif
9766
9767#ifdef FEAT_FOLDING
9768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009769ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 if (foldManualAllowed(TRUE))
9772 foldCreate(eap->line1, eap->line2);
9773}
9774
9775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009776ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9779 eap->forceit, FALSE);
9780}
9781
9782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009783ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785 linenr_T lnum;
9786
Bram Moolenaar4facea32019-10-12 20:17:40 +02009787# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009788 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009789# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009790
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009791 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9793 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9794 ml_setmarked(lnum);
9795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009796 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009798 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009799# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009800 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802}
9803#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009805#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009806/*
9807 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9808 * instead of a quickfix command.
9809 */
9810 int
9811is_loclist_cmd(int cmdidx)
9812{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009813 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009814 return FALSE;
9815 return cmdnames[cmdidx].cmd_name[0] == 'l';
9816}
9817#endif
9818
Bram Moolenaar4facea32019-10-12 20:17:40 +02009819#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009820 int
9821get_pressedreturn(void)
9822{
9823 return ex_pressedreturn;
9824}
9825
9826 void
9827set_pressedreturn(int val)
9828{
9829 ex_pressedreturn = val;
9830}
9831#endif