blob: 981d270dc7563df82e6f37f6761c71a115053798 [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
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000305# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar05159a02005-02-26 23:04:13 +0000361#ifndef FEAT_PROFILE
362# define ex_profile ex_ni
363#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200364#ifndef FEAT_TERMINAL
365# define ex_terminal ex_ni
366#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200367#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
368# define ex_xrestore ex_ni
369#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100370#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200371# define ex_popupclear ex_ni
372#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374/*
375 * Declare cmdnames[].
376 */
377#define DO_DECLARE_EXCMD
378#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200379#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static char_u dollar_command[2] = {'$', 0};
382
383
384#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386typedef struct
387{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388 char_u *line; // command line
389 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390} wcmd_T;
391
392/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000393 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000395 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100399 garray_T *lines_gap; // growarray with line info
400 int current_line; // last read line from growarray
401 int repeating; // TRUE when looping a second time
402 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100403 char_u *(*lc_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 void *cookie;
405};
406
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200407static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static int store_loop_line(garray_T *gap, char_u *line);
409static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000410
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100411// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000412struct dbg_stuff
413{
414 int trylevel;
415 int force_abort;
416 except_T *caught_stack;
417 char_u *vv_exception;
418 char_u *vv_throwpoint;
419 int did_emsg;
420 int got_int;
421 int did_throw;
422 int need_rethrow;
423 int check_cstack;
424 except_T *current_exception;
425};
426
Bram Moolenaared203462004-06-16 11:19:22 +0000427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100428save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000429{
430 dsp->trylevel = trylevel; trylevel = 0;
431 dsp->force_abort = force_abort; force_abort = FALSE;
432 dsp->caught_stack = caught_stack; caught_stack = NULL;
433 dsp->vv_exception = v_exception(NULL);
434 dsp->vv_throwpoint = v_throwpoint(NULL);
435
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100436 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000437 dsp->did_emsg = did_emsg; did_emsg = FALSE;
438 dsp->got_int = got_int; got_int = FALSE;
439 dsp->did_throw = did_throw; did_throw = FALSE;
440 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
441 dsp->check_cstack = check_cstack; check_cstack = FALSE;
442 dsp->current_exception = current_exception; current_exception = NULL;
443}
444
445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100446restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000447{
448 suppress_errthrow = FALSE;
449 trylevel = dsp->trylevel;
450 force_abort = dsp->force_abort;
451 caught_stack = dsp->caught_stack;
452 (void)v_exception(dsp->vv_exception);
453 (void)v_throwpoint(dsp->vv_throwpoint);
454 did_emsg = dsp->did_emsg;
455 got_int = dsp->got_int;
456 did_throw = dsp->did_throw;
457 need_rethrow = dsp->need_rethrow;
458 check_cstack = dsp->check_cstack;
459 current_exception = dsp->current_exception;
460}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461#endif
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463/*
Colin Kennedy65e580b2024-03-26 18:29:30 +0100464 * Check if ffname differs from fnum.
465 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid buffer ID.
466 * ffname is a full path to where a buffer lives on-disk or would live on-disk.
467 *
468 */
469 static int
470is_other_file(int fnum, char_u *ffname)
471{
472 if (fnum != 0)
473 {
474 if (fnum == curbuf->b_fnum)
475 return FALSE;
476
477 return TRUE;
478 }
479
480 if (ffname == NULL)
481 return TRUE;
482
483 if (*ffname == NUL)
484 return FALSE;
485
486 // TODO: Need a reliable way to know whether a buffer is meant to live on-disk
487 // !curbuf->b_dev_valid is not always available (example: missing on Windows)
488 if (curbuf->b_sfname != NULL
489 && *curbuf->b_sfname != NUL)
490 // This occurs with unsaved buffers. In which case `ffname`
491 // actually corresponds to curbuf->b_sfname
492 return fnamecmp(ffname, curbuf->b_sfname) != 0;
493
494 return otherfile(ffname);
495}
496
497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
499 * command is given.
500 */
501 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100502do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100503 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
505 int save_msg_scroll;
506 int prev_msg_row;
507 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100508 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000509
510 if (improved)
511 exmode_active = EXMODE_VIM;
512 else
513 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100514 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100515 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000516
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100517 // When using ":global /pat/ visual" and then "Q" we return to continue
518 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000519 if (global_busy)
520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
522 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 ++RedrawingDisabled; // don't redisplay the window
524 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100526 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 ++hold_gui_events;
528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaar32526b32019-01-19 17:43:09 +0100530 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 while (exmode_active)
532 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100533 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000534 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
535 {
536 exmode_active = FALSE;
537 break;
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 msg_scroll = TRUE;
540 need_wait_return = FALSE;
541 ex_pressedreturn = FALSE;
542 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100543 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 prev_msg_row = msg_row;
545 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 if (improved)
547 {
548 cmdline_row = msg_row;
549 do_cmdline(NULL, getexline, NULL, 0);
550 }
551 else
552 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
553 lines_left = Rows - 1;
554
Bram Moolenaardf177f62005-02-22 08:39:57 +0000555 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100556 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000558 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000559 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000562 if (ex_pressedreturn)
563 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100564 // go up one line, to overwrite the ":<CR>" line, so the
565 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000566 msg_row = prev_msg_row;
567 if (prev_msg_row == Rows - 1)
568 msg_row--;
569 }
570 msg_col = 0;
571 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
572 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100575 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000576 {
577 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000578 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000579 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000580 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583
584#ifdef FEAT_GUI
585 --hold_gui_events;
586#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100587 if (RedrawingDisabled > 0)
588 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100590 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 need_wait_return = FALSE;
592 msg_scroll = save_msg_scroll;
593}
594
595/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200596 * Print the executed command for when 'verbose' is set.
597 * When "lnum" is 0 only print the command.
598 */
599 static void
600msg_verbose_cmd(linenr_T lnum, char_u *cmd)
601{
602 ++no_wait_return;
603 verbose_enter_scroll();
604
605 if (lnum == 0)
606 smsg(_("Executing: %s"), cmd);
607 else
608 smsg(_("line %ld: %s"), (long)lnum, cmd);
609 if (msg_silent == 0)
610 msg_puts("\n"); // don't overwrite this
611
612 verbose_leave_scroll();
613 --no_wait_return;
614}
615
616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 * Execute a simple command line. Used for translated commands like "*".
618 */
619 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100620do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 return do_cmdline(cmd, NULL, NULL,
623 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
624}
625
626/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100627 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
628 * This allows for using a range without ":" in Vim9 script.
629 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200630 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100631do_cmd_argument(char_u *cmd)
632{
633 return do_cmdline(cmd, NULL, NULL,
634 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
635}
636
637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 * do_cmdline(): execute one Ex command line
639 *
640 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100641 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 * 2. Split up in parts separated with '|'.
643 *
644 * This function can be called recursively!
645 *
646 * flags:
647 * DOCMD_VERBOSE - The command will be included in the error message.
648 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 * DOCMD_KEYTYPED - Don't reset KeyTyped.
651 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
652 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
653 *
654 * return FAIL if cmdline could not be executed, OK otherwise
655 */
656 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100657do_cmdline(
658 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200659 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100661 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100663 char_u *next_cmdline; // next cmd to execute
664 char_u *cmdline_copy = NULL; // copy of cmd line
665 int used_getline = FALSE; // used "fgetline" to obtain command
666 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100669 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 int retval = OK;
671#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100672 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100673 garray_T lines_ga; // keep lines for ":while"/":for"
674 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200675 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 char_u *fname = NULL; // function or script name
677 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
678 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
679 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200681 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200682 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100684 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200685 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100691# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# define cmd_cookie cookie
693#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100694 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200695#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100696 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
697 // location for storing error messages to be converted to an exception.
698 // This ensures that the do_errthrow() call in do_one_cmd() does not
699 // combine the messages stored by an earlier invocation of do_one_cmd()
700 // with the command name of the later one. This would happen when
701 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 saved_msg_list = msg_list;
703 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
705
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100706 // It's possible to create an endless loop with ":execute", catch that
707 // here. The value of 200 allows nested function calls, ":source", etc.
708 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100709 if (call_depth >= 200
710#ifdef FEAT_EVAL
711 && call_depth >= p_mfd
712#endif
713 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000715 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100717 // When converting to an exception, we do not include the command name
718 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100719 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 msg_list = saved_msg_list;
721#endif
722 return FAIL;
723 }
724 ++call_depth;
725
726#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100727 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000729 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100731 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100733 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100734 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000735 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 ++ex_nesting_level;
737
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100738 // Get the function or script name and the address where the next breakpoint
739 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000740 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
742 fname = func_name(real_cookie);
743 breakpoint = func_breakpoint(real_cookie);
744 dbg_tick = func_dbg_tick(real_cookie);
745 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100746 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100748 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 breakpoint = source_breakpoint(real_cookie);
750 dbg_tick = source_dbg_tick(real_cookie);
751 }
752
753 /*
754 * Initialize "force_abort" and "suppress_errthrow" at the top level.
755 */
756 if (!recursive)
757 {
758 force_abort = FALSE;
759 suppress_errthrow = FALSE;
760 }
761
762 /*
763 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000764 * exception handling (used when debugging). Otherwise clear it to avoid
765 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000767 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000768 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000769 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200770 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 initial_trylevel = trylevel;
773
774 /*
775 * "did_throw" will be set to TRUE when an exception is being thrown.
776 */
777 did_throw = FALSE;
778#endif
779 /*
780 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * If force_abort is set, we cancel everything.
783 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100784#ifdef FEAT_EVAL
785 did_emsg_cumul += did_emsg;
786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 did_emsg = FALSE;
788
789 /*
790 * KeyTyped is only set when calling vgetc(). Reset it here when not
791 * calling vgetc() (sourced command lines).
792 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100793 if (!(flags & DOCMD_KEYTYPED)
794 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 KeyTyped = FALSE;
796
797 /*
798 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * - for multiple commands on one line, separated with '|'
801 * - when repeating until there are no more lines (for ":source")
802 */
803 next_cmdline = cmdline;
804 do
805 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100807 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#endif
809
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (next_cmdline == NULL
812#ifdef FEAT_EVAL
813 && !force_abort
814 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#endif
817 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100818 {
819#ifdef FEAT_EVAL
820 did_emsg_cumul += did_emsg;
821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000826 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 * 3. If a line is given: Make a copy, so we can mess with it.
829 */
830
831#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000833 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100835 // Each '|' separated command is stored separately in lines_ga, to
836 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100837 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // Check if a function has returned or, unless it has an unclosed
840 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000844 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 func_line_end(real_cookie);
846# endif
847 if (func_has_ended(real_cookie))
848 {
849 retval = FAIL;
850 break;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000854 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100855 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856 script_line_end();
857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100859 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
862 retval = FAIL;
863 break;
864 }
865
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100866 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (breakpoint != NULL && dbg_tick != NULL
868 && *dbg_tick != debug_tick)
869 {
870 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100872 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 *dbg_tick = debug_tick;
874 }
875
876 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100877 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100879 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100881 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100883 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100887 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 *dbg_tick = debug_tick;
889 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000891 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000892 {
893 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100894 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100895 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896 script_line_start();
897 }
898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
901
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100902 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (next_cmdline == NULL)
904 {
905 /*
906 * Need to set msg_didout for the first line after an ":if",
907 * otherwise the ":if" will be overwritten.
908 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100909 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100911 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912#ifdef FEAT_EVAL
913 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
914#else
915 0
916#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100917 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
918 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100920 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100921 // returned for the end of a sourced file or executed function
922 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (KeyTyped && !(flags & DOCMD_REPEAT))
924 need_wait_return = FALSE;
925 retval = FAIL;
926 break;
927 }
928 used_getline = TRUE;
929
930 /*
931 * Keep the first typed line. Clear it when more lines are typed.
932 */
933 if (flags & DOCMD_KEEPLINE)
934 {
935 vim_free(repeat_cmdline);
936 if (count == 0)
937 repeat_cmdline = vim_strsave(next_cmdline);
938 else
939 repeat_cmdline = NULL;
940 }
941 }
942
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100943 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 else if (cmdline_copy == NULL)
945 {
946 next_cmdline = vim_strsave(next_cmdline);
947 if (next_cmdline == NULL)
948 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200949 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 retval = FAIL;
951 break;
952 }
953 }
954 cmdline_copy = next_cmdline;
955
956#ifdef FEAT_EVAL
957 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200958 * Inside a while/for loop, and when the command looks like a ":while"
959 * or ":for", the line is stored, because we may need it later when
960 * looping.
961 *
962 * When there is a '|' and another command, it is stored separately,
963 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000964 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200965 *
966 * Pass a different "fgetline" function to do_one_cmd() below,
967 * that it stores lines in or reads them from "lines_ga". Makes it
968 * possible to define a function inside a while/for loop and handles
969 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200971 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200973 cmd_getline = get_loop_line;
974 cmd_cookie = (void *)&cmd_loop_cookie;
975 cmd_loop_cookie.lines_gap = &lines_ga;
976 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100977 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200978 cmd_loop_cookie.cookie = cookie;
979 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
980
981 // Save the current line when encountering it the first time.
982 if (current_line == lines_ga.ga_len
983 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
985 retval = FAIL;
986 break;
987 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200988 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200990 else
991 {
992 cmd_getline = fgetline;
993 cmd_cookie = cookie;
994 }
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 did_endif = FALSE;
997#endif
998
999 if (count++ == 0)
1000 {
1001 /*
1002 * All output from the commands is put below each other, without
1003 * waiting for a return. Don't do this when executing commands
1004 * from a script or when being called recursive (e.g. for ":e
1005 * +command file").
1006 */
1007 if (!(flags & DOCMD_NOWAIT) && !recursive)
1008 {
1009 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001012 msg_scroll = TRUE; // put messages below each other
1013 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001015 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018
Bram Moolenaar823654b2020-05-29 23:03:09 +02001019 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001020 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021
1022 /*
1023 * 2. Execute one '|' separated command.
1024 * do_one_cmd() will return NULL if there is no trailing '|'.
1025 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1026 */
1027 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001028 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#ifdef FEAT_EVAL
1030 &cstack,
1031#endif
1032 cmd_getline, cmd_cookie);
1033 --recursive;
1034
1035#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001037 // Use "current_line" from "cmd_loop_cookie", it may have been
1038 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#endif
1041
1042 if (next_cmdline == NULL)
1043 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001044 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 /*
1047 * If the command was typed, remember it for the ':' register.
1048 * Do this AFTER executing the command to make :@: work.
1049 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001050 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 && new_last_cmdline != NULL)
1052 {
1053 vim_free(last_cmdline);
1054 last_cmdline = new_last_cmdline;
1055 new_last_cmdline = NULL;
1056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 }
1058 else
1059 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001060 // need to copy the command after the '|' to cmdline_copy, for the
1061 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001062 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 next_cmdline = cmdline_copy;
1064 }
1065
1066
1067#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001070 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001072 {
1073 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
1079 ++current_line;
1080
1081 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 * An ":endwhile", ":endfor" and ":continue" is handled here.
1083 * If we were executing commands, jump back to the ":while" or
1084 * ":for".
1085 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001091 // Jump back to the matching ":while" or ":for". Be careful
1092 // not to use a cs_line[] from an entry that isn't a ":while"
1093 // or ":for": It would make "current_line" invalid and can
1094 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (!did_emsg && !got_int && !did_throw
1096 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 && (cstack.cs_flags[cstack.cs_idx]
1098 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 && cstack.cs_line[cstack.cs_idx] >= 0
1100 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1101 {
1102 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001103 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001105 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001107 // Check for the next breakpoint at or after the ":while"
1108 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001109 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001112 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 fname,
1114 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1115 *dbg_tick = debug_tick;
1116 }
1117 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001120 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001122 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1123 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 }
1125 }
1126
1127 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001133 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 }
1136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001137 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001138 if (breakpoint != NULL && has_watchexpr())
1139 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001140 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001141 *dbg_tick = debug_tick;
1142 }
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 /*
1145 * When not inside any ":while" loop, clear remembered lines.
1146 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 if (lines_ga.ga_len > 0)
1150 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001151 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1153 free_cmdlines(&lines_ga);
1154 }
1155 current_line = 0;
1156 }
1157
1158 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001159 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1160 * being restored at the ":endtry". Reset them here and set the
1161 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1162 * This includes the case where a missing ":endif", ":endwhile" or
1163 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 cstack.cs_lflags &= ~CSL_HAD_FINA;
1168 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1169 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 did_throw ? (void *)current_exception : NULL);
1171 did_emsg = got_int = did_throw = FALSE;
1172 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1173 }
1174
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001175 // Update global "trylevel" for recursive calls to do_cmdline() from
1176 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 trylevel = initial_trylevel + cstack.cs_trylevel;
1178
1179 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001180 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 * files) is aborted because of an error, an interrupt, or an uncaught
1182 * exception, cancel everything. If it is left normally, reset
1183 * force_abort to get the non-EH compatible abortion behavior for
1184 * the rest of the script.
1185 */
1186 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1187 force_abort = FALSE;
1188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001189 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001191#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 }
1194 /*
1195 * Continue executing command lines when:
1196 * - no CTRL-C typed, no aborting error, no exception thrown or try
1197 * conditionals need to be checked for executing finally clauses or
1198 * catching an interrupt exception
1199 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 * looping for ":source" command or function call.
1202 */
1203 while (!((got_int
1204#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001205 || (did_emsg && (force_abort || in_vim9script()))
1206 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#endif
1208 )
1209#ifdef FEAT_EVAL
1210 && cstack.cs_trylevel == 0
1211#endif
1212 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001213 && !(did_emsg
1214#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001215 // Keep going when inside try/catch, so that the error can be
1216 // deal with, except when it is a syntax error, it may cause
1217 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001218 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1219#endif
1220 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001221 && (getline_equal(fgetline, cookie, getexmodeline)
1222 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 && (next_cmdline != NULL
1224#ifdef FEAT_EVAL
1225 || cstack.cs_idx >= 0
1226#endif
1227 || (flags & DOCMD_REPEAT)));
1228
1229 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001230 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231#ifdef FEAT_EVAL
1232 free_cmdlines(&lines_ga);
1233 ga_clear(&lines_ga);
1234
1235 if (cstack.cs_idx >= 0)
1236 {
1237 /*
1238 * If a sourced file or executed function ran to its end, report the
1239 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001240 * In Vim9 script do not give a second error, executing aborts after
1241 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001243 if (!got_int && !did_throw && !aborting()
1244 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001245 && ((getline_equal(fgetline, cookie, getsourceline)
1246 && !source_finished(fgetline, cookie))
1247 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 && !func_has_ended(real_cookie))))
1249 {
1250 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001251 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001253 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001254 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001255 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001257 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
1259
1260 /*
1261 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1262 * ":endtry" in a sourced file or executed function. If the try
1263 * conditional is in its finally clause, ignore anything pending.
1264 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001265 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
1267 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001268 {
1269 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1270
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001271 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001273 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1274 &cstack.cs_looplevel);
1275 }
1276 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 trylevel = initial_trylevel;
1278 }
1279
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001280 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1281 // lack was reported above and the error message is to be converted to an
1282 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001283 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 ? (char_u *)"endfunction" : (char_u *)NULL);
1285
1286 if (trylevel == 0)
1287 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001288 // Just in case did_throw got set but current_exception wasn't.
1289 if (current_exception == NULL)
1290 did_throw = FALSE;
1291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 /*
1293 * When an exception is being thrown out of the outermost try
1294 * conditional, discard the uncaught exception, disable the conversion
1295 * of interrupts or errors to exceptions, and ensure that no more
1296 * commands are executed.
1297 */
1298 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001299 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300
1301 /*
1302 * On an interrupt or an aborting error not converted to an exception,
1303 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001304 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * when force_abort is set and did_emsg unset in case of an interrupt
1306 * from a finally clause after an error.
1307 */
1308 else if (got_int || (did_emsg && force_abort))
1309 suppress_errthrow = TRUE;
1310 }
1311
1312 /*
1313 * The current cstack will be freed when do_cmdline() returns. An uncaught
1314 * exception will have to be rethrown in the previous cstack. If a function
1315 * has just returned or a script file was just finished and the previous
1316 * cstack belongs to the same function or, respectively, script file, it
1317 * will have to be checked for finally clauses to be executed due to the
1318 * ":return" or ":finish". This is done in do_one_cmd().
1319 */
1320 if (did_throw)
1321 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001324 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && ex_nesting_level > func_level(real_cookie) + 1))
1326 {
1327 if (!did_throw)
1328 check_cstack = TRUE;
1329 }
1330 else
1331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001332 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 --ex_nesting_level;
1335 /*
1336 * Go to debug mode when returning from a function in which we are
1337 * single-stepping.
1338 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if ((getline_equal(fgetline, cookie, getsourceline)
1340 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001342 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 ? (char_u *)_("End of sourced file")
1344 : (char_u *)_("End of function"));
1345 }
1346
1347 /*
1348 * Restore the exception environment (done after returning from the
1349 * debugger).
1350 */
1351 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001352 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001355
1356 // Cleanup if "cs_emsg_silent_list" remains.
1357 if (cstack.cs_emsg_silent_list != NULL)
1358 {
1359 eslist_T *elem, *temp;
1360
1361 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1362 {
1363 temp = elem->next;
1364 vim_free(elem);
1365 }
1366 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001367#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 /*
1370 * If there was too much output to fit on the command line, ask the user to
1371 * hit return before redrawing the screen. With the ":global" command we do
1372 * this only once after the command is finished.
1373 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001374 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001376 if (RedrawingDisabled > 0)
1377 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 --no_wait_return;
1379 msg_scroll = FALSE;
1380
1381 /*
1382 * When just finished an ":if"-":else" which was typed, no need to
1383 * wait for hit-return. Also for an error situation.
1384 */
1385 if (retval == FAIL
1386#ifdef FEAT_EVAL
1387 || (did_endif && KeyTyped && !did_emsg)
1388#endif
1389 )
1390 {
1391 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001392 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 else if (need_wait_return)
1395 {
1396 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001397 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * here should not overwrite the command that may be shown before
1399 * doing that.
1400 */
1401 msg_didout |= msg_didout_before_start;
1402 wait_return(FALSE);
1403 }
1404 }
1405
Bram Moolenaar2a942252012-11-28 23:03:07 +01001406#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001407 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001408#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /*
1410 * Reset if_level, in case a sourced script file contains more ":if" than
1411 * ":endif" (could be ":if x | foo | endif").
1412 */
1413 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001414#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 --call_depth;
1417 return retval;
1418}
1419
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001420#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001421/*
1422 * Handle when "did_throw" is set after executing commands.
1423 */
1424 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001425handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001426{
1427 char *p = NULL;
1428 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001429 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001430
1431 /*
1432 * If the uncaught exception is a user exception, report it as an
1433 * error. If it is an error exception, display the saved error
1434 * message now. For an interrupt exception, do nothing; the
1435 * interrupt message is given elsewhere.
1436 */
1437 switch (current_exception->type)
1438 {
1439 case ET_USER:
1440 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001441 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001442 current_exception->value);
1443 p = (char *)vim_strsave(IObuff);
1444 break;
1445 case ET_ERROR:
1446 messages = current_exception->messages;
1447 current_exception->messages = NULL;
1448 break;
1449 case ET_INTERRUPT:
1450 break;
1451 }
1452
1453 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1454 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001455 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001456 current_exception->throw_name = NULL;
1457
1458 discard_current_exception(); // uses IObuff if 'verbose'
1459 suppress_errthrow = TRUE;
1460 force_abort = TRUE;
1461
1462 if (messages != NULL)
1463 {
1464 do
1465 {
1466 msglist_T *next = messages->next;
1467 int save_compiling = estack_compiling;
1468
1469 estack_compiling = messages->msg_compiling;
1470 emsg(messages->msg);
1471 vim_free(messages->msg);
1472 vim_free(messages->sfile);
1473 vim_free(messages);
1474 messages = next;
1475 estack_compiling = save_compiling;
1476 }
1477 while (messages != NULL);
1478 }
1479 else if (p != NULL)
1480 {
1481 emsg(p);
1482 vim_free(p);
1483 }
1484 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001485 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001486 estack_pop();
1487}
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001490 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 */
1492 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001493get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 wcmd_T *wp;
1497 char_u *line;
1498
1499 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1500 {
1501 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001502 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001504 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001505 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001506 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001508 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001509 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 ++cp->current_line;
1511
1512 return line;
1513 }
1514
1515 KeyTyped = FALSE;
1516 ++cp->current_line;
1517 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001518 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 return vim_strsave(wp->line);
1520}
1521
1522/*
1523 * Store a line in "gap" so that a ":while" loop can execute it again.
1524 */
1525 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001526store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 if (ga_grow(gap, 1) == FAIL)
1529 return FAIL;
1530 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001531 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 return OK;
1534}
1535
1536/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 */
1539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001540free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
1542 while (gap->ga_len > 0)
1543 {
1544 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1545 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547}
1548#endif
1549
1550/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001551 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1552 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001555getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001556 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001557 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001558 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001561 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001562 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001564 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1565 // function that's originally used to obtain the lines. This may be
1566 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001567 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001568 cp = (struct loop_cookie *)cookie;
1569 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001571 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 cp = cp->cookie;
1573 }
1574 return gp == func;
1575#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001576 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577#endif
1578}
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001581 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 * getline function. Otherwise return "cookie".
1583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001585getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001586 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001587 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar13505972019-01-24 15:04:48 +01001589#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001590 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001591 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001593 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1594 // cookie that's originally used to obtain the lines. This may be nested
1595 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001596 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001597 cp = (struct loop_cookie *)cookie;
1598 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001600 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 cp = cp->cookie;
1602 }
1603 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001604#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001607}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001609#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001610/*
1611 * Get the next line source line without advancing.
1612 */
1613 char_u *
1614getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001615 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001616 void *cookie) // argument for fgetline()
1617{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001618 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001619 struct loop_cookie *cp;
1620 wcmd_T *wp;
1621
1622 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1623 // cookie that's originally used to obtain the lines. This may be nested
1624 // several levels.
1625 gp = fgetline;
1626 cp = (struct loop_cookie *)cookie;
1627 while (gp == get_loop_line)
1628 {
1629 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1630 {
1631 // executing lines a second time, use the stored copy
1632 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1633 return wp->line;
1634 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001635 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001636 cp = cp->cookie;
1637 }
1638 if (gp == getsourceline)
1639 return source_nextline(cp);
1640 return NULL;
1641}
1642#endif
1643
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001644
1645/*
1646 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1647 * ":bwipeout", etc.
1648 * Returns the buffer number.
1649 */
1650 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001651compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001652{
1653 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001654 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001655 int count = offset;
1656
1657 buf = firstbuf;
1658 while (buf->b_next != NULL && buf->b_fnum < lnum)
1659 buf = buf->b_next;
1660 while (count != 0)
1661 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001662 count += (offset < 0) ? 1 : -1;
1663 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1664 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001666 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001668 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001669 while (buf->b_ml.ml_mfp == NULL)
1670 {
1671 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1672 if (nextbuf == NULL)
1673 break;
1674 buf = nextbuf;
1675 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001676 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001677 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001678 if (addr_type == ADDR_LOADED_BUFFERS)
1679 while (buf->b_ml.ml_mfp == NULL)
1680 {
1681 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1682 if (nextbuf == NULL)
1683 break;
1684 buf = nextbuf;
1685 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 return buf->b_fnum;
1687}
1688
Bram Moolenaarb7316892019-05-01 18:08:42 +02001689/*
1690 * Return the window number of "win".
1691 * When "win" is NULL return the number of windows.
1692 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001693 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001694current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001695{
1696 win_T *wp;
1697 int nr = 0;
1698
Bram Moolenaar29323592016-07-24 22:04:11 +02001699 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001700 {
1701 ++nr;
1702 if (wp == win)
1703 break;
1704 }
1705 return nr;
1706}
1707
1708 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001709current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001710{
1711 tabpage_T *tp;
1712 int nr = 0;
1713
Bram Moolenaar29323592016-07-24 22:04:11 +02001714 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001715 {
1716 ++nr;
1717 if (tp == tab)
1718 break;
1719 }
1720 return nr;
1721}
1722
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001723 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001724comment_start(char_u *p, int starts_with_colon UNUSED)
1725{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001726 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001727 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001728 return *p == '"';
1729}
1730
Bram Moolenaarf240e182014-11-27 18:33:02 +01001731# define CURRENT_WIN_NR current_win_nr(curwin)
1732# define LAST_WIN_NR current_win_nr(NULL)
1733# define CURRENT_TAB_NR current_tab_nr(curtab)
1734# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001735
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736/*
1737 * Execute one Ex command.
1738 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001739 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1740 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 *
1742 * 1. skip comment lines and leading space
1743 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001744 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001745 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001746 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001747 * 6. parse arguments
1748 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001750 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 *
1752 * This function may be called recursively!
1753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001755do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001756 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001757 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001759 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001761 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001762 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001764 char_u *p;
1765 linenr_T lnum;
1766 long n;
1767 char *errormsg = NULL; // error message
1768 char_u *after_modifier = NULL;
1769 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001770 cmdmod_T save_cmdmod;
1771 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001772 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001773 int ni; // set when Not Implemented
1774 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001775 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001776 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001777#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001778 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001779#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001780 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001781 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaara80faa82020-04-12 19:37:17 +02001783 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 ea.line1 = 1;
1785 ea.line2 = 1;
1786#ifdef FEAT_EVAL
1787 ++ex_nesting_level;
1788#endif
1789
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001790 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (quitmore
1792#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001793 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001794 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001795#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001796 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001797 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 --quitmore;
1799
1800 /*
1801 * Reset browse, confirm, etc.. They are restored when returning, for
1802 * recursive calls.
1803 */
1804 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001806 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001807 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1808 goto doend;
1809
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001810/*
1811 * 1. Skip comment lines and leading white space and colons.
1812 * 2. Handle command modifiers.
1813 */
1814 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001816 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001817 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001818 ea.cookie = cookie;
1819#ifdef FEAT_EVAL
1820 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001821 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001823 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001824 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001825 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001826 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828#ifdef FEAT_EVAL
1829 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1830 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1831#else
1832 ea.skip = (if_level > 0);
1833#endif
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001836 * 3. Skip over the range to find the command. Let "p" point to after it.
1837 *
1838 * We need the command to know what kind of range it uses.
1839 */
1840 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001841
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001842 // In Vim9 script a colon is required before the range. This may also be
1843 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001844 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001845 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001846 {
1847 may_have_range = FALSE;
1848 for (p = ea.cmd; p >= *cmdlinep; --p)
1849 {
1850 if (*p == ':')
1851 may_have_range = TRUE;
1852 if (p < ea.cmd && !VIM_ISWHITE(*p))
1853 break;
1854 }
1855 }
1856 else
1857 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001858 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001859 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001861#ifdef FEAT_EVAL
1862 // Handle ":export" - it functions almost like a command modifier.
1863 // ":export var Name: type"
1864 // ":export def Name(..."
1865 // etc.
1866 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1867 is_export = TRUE;
1868#endif
1869
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001870 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001871 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001872 if (ea.cmd == cmd + 1 && *cmd == '$')
1873 // should be "$VAR = val"
1874 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001875#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001876 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001877#else
1878 p = find_ex_command(&ea, NULL, NULL, NULL);
1879#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001880 if (ea.cmdidx == CMD_SIZE)
1881 {
1882 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1883
1884 // If a ':' before the range is missing, give a clearer error
1885 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001886 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001887 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001888 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001889 goto doend;
1890 }
1891 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001895
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001896#ifdef FEAT_EVAL
1897# ifdef FEAT_PROFILE
1898 // Count this line for profiling if skip is TRUE.
1899 if (do_profiling == PROF_YES
1900 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1901 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1902 {
1903 int skip = did_emsg || got_int || did_throw;
1904
1905 if (ea.cmdidx == CMD_catch)
1906 skip = !skip && !(cstack->cs_idx >= 0
1907 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1908 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1909 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1910 skip = skip || !(cstack->cs_idx >= 0
1911 && !(cstack->cs_flags[cstack->cs_idx]
1912 & (CSF_ACTIVE | CSF_TRUE)));
1913 else if (ea.cmdidx == CMD_finally)
1914 skip = FALSE;
1915 else if (ea.cmdidx != CMD_endif
1916 && ea.cmdidx != CMD_endfor
1917 && ea.cmdidx != CMD_endtry
1918 && ea.cmdidx != CMD_endwhile)
1919 skip = ea.skip;
1920
1921 if (!skip)
1922 {
1923 if (getline_equal(fgetline, cookie, get_func_line))
1924 func_line_exec(getline_cookie(fgetline, cookie));
1925 else if (getline_equal(fgetline, cookie, getsourceline))
1926 script_line_exec();
1927 }
1928 }
1929# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001930#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001931
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001932 ea.cmd = cmd;
1933
1934#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001935 // May go to debug mode. If this happens and the ">quit" debug command is
1936 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001937 dbg_check_breakpoint(&ea);
1938 if (!ea.skip && got_int)
1939 {
1940 ea.skip = TRUE;
1941 (void)do_intthrow(cstack);
1942 }
1943#endif
1944
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001945/*
1946 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 *
1948 * where 'addr' is:
1949 *
1950 * % (entire file)
1951 * $ [+-NUM]
1952 * 'x [+-NUM] (where x denotes a currently defined mark)
1953 * . [+-NUM]
1954 * [+-NUM]..
1955 * NUM
1956 *
1957 * The ea.cmd pointer is updated to point to the first character following the
1958 * range spec. If an initial address is found, but no second, the upper bound
1959 * is equal to the lower.
1960 */
1961
Bram Moolenaar25190db2019-05-04 15:05:28 +02001962 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001963 if (!IS_USER_CMDIDX(ea.cmdidx))
1964 {
1965 if (ea.cmdidx != CMD_SIZE)
1966 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1967 else
1968 ea.addr_type = ADDR_LINES;
1969
Bram Moolenaar25190db2019-05-04 15:05:28 +02001970 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001971 if (ea.cmdidx == CMD_wincmd && p != NULL)
1972 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001973#ifdef FEAT_QUICKFIX
1974 // :.cc in quickfix window uses line number
1975 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1976 ea.addr_type = ADDR_OTHER;
1977#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001978 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001979
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001980 if (!may_have_range)
1981 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001982 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1983 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001986 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
1988
1989 /*
1990 * Skip ':' and any white space
1991 */
1992 ea.cmd = skipwhite(ea.cmd);
1993 while (*ea.cmd == ':')
1994 ea.cmd = skipwhite(ea.cmd + 1);
1995
1996 /*
1997 * If we got a line, but no command, then go to the line.
1998 * If we find a '|' or '\n' we set ea.nextcmd.
1999 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002000 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2001 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
2003 /*
2004 * strange vi behaviour:
2005 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002006 * ":3|..." prints line 3 (not in Vim9 script)
2007 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002009 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002011 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto doend;
2013 }
2014
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // If this looks like an undefined user command and there are CmdUndefined
2016 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002017 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2018 && ASCII_ISUPPER(*ea.cmd)
2019 && has_cmdundefined())
2020 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002021 int ret;
2022
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002023 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002024 while (ASCII_ISALNUM(*p))
2025 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002026 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002027 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2028 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002029 // If the autocommands did something and didn't cause an error, try
2030 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002031 p = (ret
2032#ifdef FEAT_EVAL
2033 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002034#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002036 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 if (p == NULL)
2039 {
2040 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002041 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 goto doend;
2043 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002044 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002045 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2046 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
2048 errormsg = uc_fun_cmd();
2049 goto doend;
2050 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (ea.cmdidx == CMD_SIZE)
2053 {
2054 if (!ea.skip)
2055 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002056 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002058 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002059 // If the modifier was parsed OK the error must be in the
2060 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002061 if (after_modifier != NULL)
2062 append_command(after_modifier);
2063 else
2064 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002065 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002066 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002067 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002068 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070 goto doend;
2071 }
2072
Bram Moolenaar958636c2014-10-21 20:01:58 +02002073 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2074 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002075#ifdef HAVE_EX_SCRIPT_NI
2076 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2077#endif
2078 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
2080#ifndef FEAT_EVAL
2081 /*
2082 * When the expression evaluation is disabled, recognize the ":if" and
2083 * ":endif" commands and ignore everything in between it.
2084 */
2085 if (ea.cmdidx == CMD_if)
2086 ++if_level;
2087 if (if_level)
2088 {
2089 if (ea.cmdidx == CMD_endif)
2090 --if_level;
2091 goto doend;
2092 }
2093
2094#endif
2095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002096 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002097 if (*p == '!' && ea.cmdidx != CMD_substitute
2098 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
2100 ++p;
2101 ea.forceit = TRUE;
2102 }
2103 else
2104 ea.forceit = FALSE;
2105
2106/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002107 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002109 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002110 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
2112 if (!ea.skip)
2113 {
2114#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002115 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002117 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002118 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 goto doend;
2120 }
2121#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002122 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002123 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002124 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002125 goto doend;
2126 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002127 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002129 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002130 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 goto doend;
2132 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002133
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002134 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002136 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2137 {
2138 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002139 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002140 goto doend;
2141 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002142 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2143 {
2144 // Command not allowed when text is locked
2145 errormsg = _(get_text_locked_msg());
2146 goto doend;
2147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002150 // Disallow editing another buffer when "curbuf_lock" is set.
2151 // Do allow ":checktime" (it is postponed).
2152 // Do allow ":edit" (check for an argument later).
2153 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002154 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002155 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002156 && ea.cmdidx != CMD_edit
2157 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002158 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159 && curbuf_locked())
2160 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002162 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002164 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 goto doend;
2166 }
2167 }
2168
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002169 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002171 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 goto doend;
2173 }
2174
2175 /*
2176 * Don't complain about the range if it is not used
2177 * (could happen if line_count is accidentally set to 0).
2178 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002179 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 /*
2182 * If the range is backwards, ask for confirmation and, if given, swap
2183 * ea.line1 & ea.line2 so it's forwards again.
2184 * When global command is busy, don't ask, will fail below.
2185 */
2186 if (!global_busy && ea.line1 > ea.line2)
2187 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002188 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002190 if (sourcing || exmode_active)
2191 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002192 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002193 goto doend;
2194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (ask_yesno((char_u *)
2196 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002197 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 }
2199 lnum = ea.line1;
2200 ea.line1 = ea.line2;
2201 ea.line2 = lnum;
2202 }
2203 if ((errormsg = invalid_range(&ea)) != NULL)
2204 goto doend;
2205 }
2206
Bram Moolenaarb7316892019-05-01 18:08:42 +02002207 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2208 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 ea.line2 = 1;
2210
2211 correct_range(&ea);
2212
2213#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002214 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002215 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002217 // Put the first line at the start of a closed fold, put the last line
2218 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 (void)hasFolding(ea.line1, &ea.line1, NULL);
2220 (void)hasFolding(ea.line2, NULL, &ea.line2);
2221 }
2222#endif
2223
2224#ifdef FEAT_QUICKFIX
2225 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002226 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * option here, so things like % get expanded.
2228 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002229 p = replace_makeprg(&ea, p, cmdlinep);
2230 if (p == NULL)
2231 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#endif
2233
2234 /*
2235 * Skip to start of argument.
2236 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2237 */
2238 if (ea.cmdidx == CMD_bang)
2239 ea.arg = p;
2240 else
2241 ea.arg = skipwhite(p);
2242
Bram Moolenaar379fb762018-08-30 15:58:28 +02002243 // ":file" cannot be run with an argument when "curbuf_lock" is set
2244 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2245 goto doend;
2246
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 /*
2248 * Check for "++opt=val" argument.
2249 * Must be first, allow ":w ++enc=utf8 !cmd"
2250 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002251 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2253 if (getargopt(&ea) == FAIL && !ni)
2254 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002255 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 goto doend;
2257 }
2258
2259 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2260 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002261 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002263 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002265 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 goto doend;
2267 }
2268 ea.arg = skipwhite(ea.arg + 1);
2269 ea.append = TRUE;
2270 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 ++ea.arg;
2274 ea.usefilter = TRUE;
2275 }
2276 }
2277
2278 if (ea.cmdidx == CMD_read)
2279 {
2280 if (ea.forceit)
2281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002282 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 ea.forceit = FALSE;
2284 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002285 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 ++ea.arg;
2288 ea.usefilter = TRUE;
2289 }
2290 }
2291
2292 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2293 {
2294 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002295 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 ++ea.arg;
2298 ++ea.amount;
2299 }
2300 ea.arg = skipwhite(ea.arg);
2301 }
2302
2303 /*
2304 * Check for "+command" argument, before checking for next command.
2305 * Don't do this for ":read !cmd" and ":write !cmd".
2306 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002307 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2309
2310 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002311 * For commands that do not use '|' inside their argument: Check for '|' to
2312 * separate commands and '"' or '#' to start comments.
2313 *
2314 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002315 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002316 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002317 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002319 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2320 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002321 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002322 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002323 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002324 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002325 || ea.cmdidx == CMD_global
2326 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002327 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002328#ifdef FEAT_EVAL
2329 || inside_block(&ea)
2330#endif
2331 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 for (p = ea.arg; *p; ++p)
2334 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002335 // Remove one backslash before a newline, so that it's possible to
2336 // pass a newline to the shell and also a newline that is preceded
2337 // with a backslash. This makes it impossible to end a shell
2338 // command in a backslash, but that doesn't appear useful.
2339 // Halving the number of backslashes is incompatible with previous
2340 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002342 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002343 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 ea.nextcmd = p + 1;
2346 *p = NUL;
2347 break;
2348 }
2349 }
2350 }
2351
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002352 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002353 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002355 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002356 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002358 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002359 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002362#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002364 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002366 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002367 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002370 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2371 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002372 {
2373 ea.regname = *ea.arg++;
2374#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002375 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002376 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2377 {
kuuote08d7b1c2021-10-04 22:17:36 +01002378 if (!ea.skip)
2379 {
2380 set_expr_line(vim_strsave(ea.arg), &ea);
2381 did_set_expr_line = TRUE;
2382 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002383 ea.arg += STRLEN(ea.arg);
2384 }
2385#endif
2386 ea.arg = skipwhite(ea.arg);
2387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389
2390 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002391 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * count, it's a buffer name.
2393 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002394 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002395 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002396 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002398 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002400 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002402 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 goto doend;
2404 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002405 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
2407 ea.line2 = n;
2408 if (ea.addr_count == 0)
2409 ea.addr_count = 1;
2410 }
2411 else
2412 {
2413 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002414 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002415 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002416 else
2417 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 ++ea.addr_count;
2419 /*
2420 * Be vi compatible: no error message for out of range.
2421 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002422 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 ea.line2 = curbuf->b_ml.ml_line_count;
2424 }
2425 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002426
2427 /*
2428 * Check for flags: 'l', 'p' and '#'.
2429 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002430 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002431 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002432 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2433 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002435 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002436 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 goto doend;
2438 }
2439
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002440 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002442 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 goto doend;
2444 }
2445
2446#ifdef FEAT_EVAL
2447 /*
2448 * Skip the command when it's not going to be executed.
2449 * The commands like :if, :endif, etc. always need to be executed.
2450 * Also make an exception for commands that handle a trailing command
2451 * themselves.
2452 */
2453 if (ea.skip)
2454 {
2455 switch (ea.cmdidx)
2456 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002457 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_while:
2459 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002460 case CMD_for:
2461 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_if:
2463 case CMD_elseif:
2464 case CMD_else:
2465 case CMD_endif:
2466 case CMD_try:
2467 case CMD_catch:
2468 case CMD_finally:
2469 case CMD_endtry:
2470 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002471 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 break;
2473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002474 // Commands that handle '|' themselves. Check: A command should
2475 // either have the EX_TRLBAR flag, appear in this list or appear in
2476 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 case CMD_aboveleft:
2478 case CMD_and:
2479 case CMD_belowright:
2480 case CMD_botright:
2481 case CMD_browse:
2482 case CMD_call:
2483 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002484 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_delfunction:
2486 case CMD_djump:
2487 case CMD_dlist:
2488 case CMD_dsearch:
2489 case CMD_dsplit:
2490 case CMD_echo:
2491 case CMD_echoerr:
2492 case CMD_echomsg:
2493 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002494 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002496 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002497 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_help:
2499 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002500 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 case CMD_ijump:
2502 case CMD_ilist:
2503 case CMD_isearch:
2504 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002505 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 case CMD_keepjumps:
2507 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002508 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_leftabove:
2510 case CMD_let:
2511 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002512 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002513 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002515 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002516 case CMD_noautocmd:
2517 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 case CMD_perl:
2519 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002520 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002521 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002522 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 case CMD_return:
2524 case CMD_rightbelow:
2525 case CMD_ruby:
2526 case CMD_silent:
2527 case CMD_smagic:
2528 case CMD_snomagic:
2529 case CMD_substitute:
2530 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002531 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 case CMD_tcl:
2533 case CMD_throw:
2534 case CMD_tilde:
2535 case CMD_topleft:
2536 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002537 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002538 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 case CMD_verbose:
2540 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002541 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 break;
2543
2544 default: goto doend;
2545 }
2546 }
2547#endif
2548
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002549 if ((ea.argt & EX_XFILE)
2550 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2551 goto doend;
2552
2553#ifdef FEAT_EVAL
2554 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002556 emsg(_(e_invalid_command_after_export));
2557 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 /*
2562 * Accept buffer name. Cannot be used at the same time with a buffer
2563 * number. Don't do this for a user command.
2564 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002565 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002566 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 /*
2569 * :bdelete, :bwipeout and :bunload take several arguments, separated
2570 * by spaces: find next space (skipping over escaped characters).
2571 * The others take one argument: ignore trailing spaces.
2572 */
2573 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2574 || ea.cmdidx == CMD_bunload)
2575 p = skiptowhite_esc(ea.arg);
2576 else
2577 {
2578 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002579 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 --p;
2581 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002582 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002583 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002584 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 goto doend;
2586 ea.addr_count = 1;
2587 ea.arg = skipwhite(p);
2588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002590 // The :try command saves the emsg_silent flag, reset it here when
2591 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002592 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002593 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002594 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002595 if (emsg_silent < 0)
2596 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002597 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002598 }
2599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002601 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
Bram Moolenaar958636c2014-10-21 20:01:58 +02002604 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 /*
2607 * Execute a user-defined command.
2608 */
2609 do_ucmd(&ea);
2610 }
2611 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002614 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2617 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002618 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620
2621#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002622 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002623 // set something went wrong or the command was never executed.
2624 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002625 {
2626 if (errormsg == NULL)
2627 errormsg = _(e_export_with_invalid_argument);
2628 is_export = FALSE;
2629 }
2630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002632 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002633 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002634 && current_sctx.sc_sid > 0
2635 && ea.cmdidx != CMD_endif
2636 && (cstack->cs_idx < 0
2637 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002638 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 /*
2641 * If the command just executed called do_cmdline(), any throw or ":return"
2642 * or ":finish" encountered there must also check the cstack of the still
2643 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2644 * exception, or reanimate a returned function or finished script file and
2645 * return or finish it again.
2646 */
2647 if (need_rethrow)
2648 do_throw(cstack);
2649 else if (check_cstack)
2650 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002651 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002653 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 && current_func_returned())
2655 do_return(&ea, TRUE, FALSE, NULL);
2656 }
2657 need_rethrow = check_cstack = FALSE;
2658#endif
2659
2660doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002661 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002664 curwin->w_cursor.col = 0;
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2668 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002669 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002671 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
2673 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002674 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002676 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678 emsg(errormsg);
2679 }
2680#ifdef FEAT_EVAL
2681 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002682 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2683 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002684
2685 if (did_set_expr_line)
2686 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002687 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688#endif
2689
Bram Moolenaare1004402020-10-24 20:49:43 +02002690 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002692 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002693 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002695 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 ea.nextcmd = NULL;
2697
2698#ifdef FEAT_EVAL
2699 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002700 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701#endif
2702
2703 return ea.nextcmd;
2704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002706static char ex_error_buf[MSG_BUF_LEN];
2707
2708/*
2709 * Return an error message with argument included.
2710 * Uses a static buffer, only the last error will be kept.
2711 * "msg" will be translated, caller should use N_().
2712 */
2713 char *
2714ex_errmsg(char *msg, char_u *arg)
2715{
2716 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2717 return ex_error_buf;
2718}
2719
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002721 * Handle a range without a command.
2722 * Returns an error message on failure.
2723 */
2724 char *
2725ex_range_without_command(exarg_T *eap)
2726{
2727 char *errormsg = NULL;
2728
2729 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2730#ifdef FEAT_EVAL
2731 && !in_vim9script()
2732#endif
2733 )
2734 {
2735 eap->cmdidx = CMD_print;
2736 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2737 if ((errormsg = invalid_range(eap)) == NULL)
2738 {
2739 correct_range(eap);
2740 ex_print(eap);
2741 }
2742 }
2743 else if (eap->addr_count != 0)
2744 {
2745 if (eap->line2 > curbuf->b_ml.ml_line_count)
2746 {
2747 // With '-' in 'cpoptions' a line number past the file is an
2748 // error, otherwise put it at the end of the file.
2749 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2750 eap->line2 = -1;
2751 else
2752 eap->line2 = curbuf->b_ml.ml_line_count;
2753 }
2754
2755 if (eap->line2 < 0)
2756 errormsg = _(e_invalid_range);
2757 else
2758 {
2759 if (eap->line2 == 0)
2760 curwin->w_cursor.lnum = 1;
2761 else
2762 curwin->w_cursor.lnum = eap->line2;
2763 beginline(BL_SOL | BL_FIX);
2764 }
2765 }
2766 return errormsg;
2767}
2768
2769/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002770 * Check for an Ex command with optional tail.
2771 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002772 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002773 */
2774 static int
2775checkforcmd_opt(
2776 char_u **pp, // start of command
2777 char *cmd, // name of command
2778 int len, // required length
2779 int noparen)
2780{
2781 int i;
2782
2783 for (i = 0; cmd[i] != NUL; ++i)
2784 if (((char_u *)cmd)[i] != (*pp)[i])
2785 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002786 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002787 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002788 {
2789 *pp = skipwhite(*pp + i);
2790 return TRUE;
2791 }
2792 return FALSE;
2793}
2794
2795/*
2796 * Check for an Ex command with optional tail.
2797 * If there is a match advance "pp" to the argument and return TRUE.
2798 */
2799 int
2800checkforcmd(
2801 char_u **pp, // start of command
2802 char *cmd, // name of command
2803 int len) // required length
2804{
2805 return checkforcmd_opt(pp, cmd, len, FALSE);
2806}
2807
2808/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002809 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002810 * If there is a match advance "pp" to the argument and return TRUE.
2811 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002812 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002813checkforcmd_noparen(
2814 char_u **pp, // start of command
2815 char *cmd, // name of command
2816 int len) // required length
2817{
2818 return checkforcmd_opt(pp, cmd, len, TRUE);
2819}
2820
2821/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 * Parse and skip over command modifiers:
2823 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002824 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002826 * When "skip_only" is TRUE the global variables are not changed, except for
2827 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002828 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2829 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002830 * Call apply_cmdmod() to get the side effects of the modifiers:
2831 * - Increment "sandbox" for ":sandbox"
2832 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002833 * - set msg_silent for ":silent"
2834 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 * Return FAIL when the command is not to be executed.
2836 * May set "errormsg" to an error message.
2837 */
2838 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002839parse_command_modifiers(
2840 exarg_T *eap,
2841 char **errormsg,
2842 cmdmod_T *cmod,
2843 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002844{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002845 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002846 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002847 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002848 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002849 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002850 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002851
Bram Moolenaare1004402020-10-24 20:49:43 +02002852 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002853 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002854
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002855 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2856 {
2857 // The automatically inserted Visual area range is skipped, so that
2858 // typing ":cmdmod cmd" in Visual mode works without having to move the
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002859 // range to after the modififiers. The command will be
2860 // "'<,'>cmdmod cmd", parse "cmdmod cmd" and then put back "'<,'>"
2861 // before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002862 eap->cmd += 5;
2863 cmd_start = eap->cmd;
2864 has_visual_range = TRUE;
2865 }
2866
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002867 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002868 for (;;)
2869 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002870 char_u *p;
2871
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002873 {
2874 if (*eap->cmd == ':')
2875 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002877 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002878
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002879 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002881 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2882 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2884 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002885 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002886 if (!skip_only)
2887 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002888 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 }
2890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002891 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002892 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002893 {
2894 // a comment ends at a NL
2895 if (eap->nextcmd == NULL)
2896 {
2897 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2898 if (eap->nextcmd != NULL)
2899 ++eap->nextcmd;
2900 }
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002901 if (vim9script)
2902 {
2903 if (has_cmdmod(cmod, FALSE))
2904 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002905#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002906 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2907 && eap->cmd[2] != '{')
2908 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002909#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002910 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002912 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002913 if (*eap->cmd == NUL)
2914 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002915 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002916 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002917 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002918 if (vim9script && has_cmdmod(cmod, FALSE))
2919 *errormsg = _(e_command_modifier_without_command);
2920 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 return FAIL;
2922 }
2923
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002924 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002925
2926 // In Vim9 script a variable can shadow a command modifier:
2927 // verbose = 123
2928 // verbose += 123
2929 // silent! verbose = func()
2930 // verbose.member = 2
2931 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002932 // But not:
2933 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002934 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002935 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002936 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002937
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002938 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002939 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002940 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002941 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2942 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002943 break;
2944 }
2945
Bram Moolenaareffed932018-08-14 13:38:17 +02002946 switch (*p)
2947 {
Bram Moolenaar91324262022-09-09 13:27:59 +01002948 // When adding an entry, also modify cmdmods[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002949 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002950 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002951 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002952 continue;
2953
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002954 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002955 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002956 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002957 continue;
2958 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002959 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 {
2961#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002962 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002963#endif
2964 continue;
2965 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002966 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002967 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002968 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002969 continue;
2970
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002971 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 break;
2973#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002974 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002975#endif
2976 continue;
2977
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002978 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002980 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002981 continue;
2982 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002983 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002985 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002986 continue;
2987 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002988 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002990 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002991 continue;
2992 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002993 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002995 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 continue;
2997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002998 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003000 char_u *reg_pat;
3001 char_u *nulp = NULL;
3002 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003003
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003004 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003005 || *p == NUL
3006 || (ends_excmd(*p)
3007#ifdef FEAT_EVAL
3008 // in ":filter #pat# cmd" # does not
3009 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003010 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003011#endif
3012 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003013 break;
3014 if (*p == '!')
3015 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003016 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003017 p = skipwhite(p + 1);
3018 if (*p == NUL || ends_excmd(*p))
3019 break;
3020 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003021#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003022 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003023 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003024 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003025#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003026 if (skip_only)
3027 p = skip_vimgrep_pat(p, NULL, NULL);
3028 else
3029 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003030 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3031 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003032 if (p == NULL || *p == NUL)
3033 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003034 if (!skip_only)
3035 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003036 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003037 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003038 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003039 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003040 // restore the character overwritten by NUL
3041 if (nulp != NULL)
3042 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003043 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003044 eap->cmd = p;
3045 continue;
3046 }
3047
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003048 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3049 {
3050 cmod->cmod_split |= WSP_HOR;
3051 continue;
3052 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003053 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003054 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003055 || *p == NUL || ends_excmd(*p))
3056 break;
3057 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003058 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003059 continue;
3060
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003061 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003062 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003063 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003064 continue;
3065 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003066 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3067 {
3068 if (ends_excmd2(p, eap->cmd))
3069 {
3070 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003071 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003072 return FAIL;
3073 }
3074 cmod->cmod_flags |= CMOD_LEGACY;
3075 continue;
3076 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003077
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003078 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003079 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003080 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003081 continue;
3082
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003083 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003084 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003085 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003086 continue;
3087 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003088 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003089 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003090 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003091 continue;
3092
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003093 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003095 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003096 continue;
3097
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003098 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003099 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003100 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003101 continue;
3102 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003103 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003104 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003105 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003106 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003108 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003109 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003110 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003111 }
3112 continue;
3113
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003114 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003115 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003116 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003117 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003118 long tabnr = get_address(eap, &eap->cmd,
3119 ADDR_TABS, eap->skip,
3120 skip_only, FALSE, 1);
3121 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003122 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003123 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003124 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003125 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3126 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003127 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003128 return FAIL;
3129 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003130 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003131 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003132 }
3133 eap->cmd = p;
3134 continue;
3135 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003136 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003137 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003138 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003139 continue;
3140
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003141 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003142 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003143 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003144 continue;
3145
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003146 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003147 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003148 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003149 continue;
3150 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003151 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003152 {
3153 if (ends_excmd2(p, eap->cmd))
3154 {
3155 *errormsg =
3156 _(e_vim9cmd_must_be_followed_by_command);
3157 return FAIL;
3158 }
3159 cmod->cmod_flags |= CMOD_VIM9CMD;
3160 continue;
3161 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003162 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003163 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003164 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003165 {
zeertzjqcd749632022-06-14 13:30:35 +01003166 // zero means not set, one is verbose == 0, etc.
3167 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003168 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003169 else
zeertzjqcd749632022-06-14 13:30:35 +01003170 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003171 eap->cmd = p;
3172 continue;
3173 }
3174 break;
3175 }
3176
Bram Moolenaar1501b632022-03-27 16:56:21 +01003177 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003178 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003179 if (eap->cmd > cmd_start)
3180 {
3181 // Move the '<,'> range to after the modifiers and insert a colon.
3182 // Since the modifiers have been parsed put the colon on top of the
3183 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3184 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003185 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003186 {
3187 size_t len = STRLEN(cmd_start);
3188
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003189 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003190 // "'<,'>mods" -> "mods *+
3191 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003192 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003193 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003194 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003195 }
3196 else
3197 {
3198 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3199 eap->cmd -= 5;
3200 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3201 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003202 }
3203 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003204 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003205 // Special case: change empty command to "+".
3206 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003207 eap->cmd = (char_u *)"'<,'>+";
3208 else
3209 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003210 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003211 else if (use_plus_cmd)
3212 eap->cmd = (char_u *)"+";
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003213
Bram Moolenaareffed932018-08-14 13:38:17 +02003214 return OK;
3215}
3216
3217/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003218 * Return TRUE if "cmod" has anything set.
3219 */
3220 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003221has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003222{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003223 return (cmod->cmod_flags != 0 && (!ignore_silent
3224 || (cmod->cmod_flags
3225 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003226 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003227 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003228 || cmod->cmod_tab != 0
3229 || cmod->cmod_filter_regmatch.regprog != NULL;
3230}
3231
Bram Moolenaar8991be22022-02-14 21:51:46 +00003232#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003233/*
3234 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3235 */
3236 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003237cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003238{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003239 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003240 {
3241 emsg(_(e_misplaced_command_modifier));
3242 return TRUE;
3243 }
3244 return FALSE;
3245}
Dominique Pelle748b3082022-01-08 12:41:16 +00003246#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003247
3248/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003249 * Apply the command modifiers. Saves current state in "cmdmod", call
3250 * undo_cmdmod() later.
3251 */
3252 void
3253apply_cmdmod(cmdmod_T *cmod)
3254{
3255#ifdef HAVE_SANDBOX
3256 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3257 {
3258 ++sandbox;
3259 cmod->cmod_did_sandbox = TRUE;
3260 }
3261#endif
zeertzjqcd749632022-06-14 13:30:35 +01003262 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003263 {
3264 if (cmod->cmod_verbose_save == 0)
3265 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003266 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003267 }
3268
3269 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3270 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003271 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003272 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003273 cmod->cmod_save_msg_scroll = msg_scroll;
3274 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003275 if (cmod->cmod_flags & CMOD_SILENT)
3276 ++msg_silent;
3277 if (cmod->cmod_flags & CMOD_UNSILENT)
3278 msg_silent = 0;
3279
3280 if (cmod->cmod_flags & CMOD_ERRSILENT)
3281 {
3282 ++emsg_silent;
3283 ++cmod->cmod_did_esilent;
3284 }
3285
Bram Moolenaare1004402020-10-24 20:49:43 +02003286 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003287 {
3288 // Set 'eventignore' to "all".
3289 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003290 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003291 set_string_option_direct((char_u *)"ei", -1,
3292 (char_u *)"all", OPT_FREE, SID_NONE);
3293 }
3294}
3295
3296/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003297 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003298 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003299 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003300undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003301{
Bram Moolenaare1004402020-10-24 20:49:43 +02003302 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003303 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003304 p_verbose = cmod->cmod_verbose_save - 1;
3305 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003306 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003307
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003308#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003309 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003310 {
3311 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003312 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003313 }
3314#endif
3315
Bram Moolenaare1004402020-10-24 20:49:43 +02003316 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003317 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003318 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003319 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3320 OPT_FREE, SID_NONE);
3321 free_string_option(cmod->cmod_save_ei);
3322 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003323 }
3324
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003325 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003326
Bram Moolenaare1004402020-10-24 20:49:43 +02003327 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003328 {
3329 // messages could be enabled for a serious error, need to check if the
3330 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003331 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3332 msg_silent = cmod->cmod_save_msg_silent - 1;
3333 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003334 if (emsg_silent < 0)
3335 emsg_silent = 0;
3336 // Restore msg_scroll, it's set by file I/O commands, even when no
3337 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003338 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003339
3340 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3341 // somewhere in the line. Put it back in the first column.
3342 if (redirecting())
3343 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003344
Bram Moolenaare1004402020-10-24 20:49:43 +02003345 cmod->cmod_save_msg_silent = 0;
3346 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003347 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003348}
3349
3350/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003351 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003352 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003353 * Return FAIL and set "errormsg" or return OK.
3354 */
3355 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003356parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003357{
3358 int address_count = 1;
3359 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003360 int need_check_cursor = FALSE;
3361 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003362
3363 // Repeat for all ',' or ';' separated addresses.
3364 for (;;)
3365 {
3366 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003367 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003368 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003369 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003370 eap->addr_count == 0, address_count++);
3371 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003372 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003373 if (lnum == MAXLNUM)
3374 {
3375 if (*eap->cmd == '%') // '%' - all lines
3376 {
3377 ++eap->cmd;
3378 switch (eap->addr_type)
3379 {
3380 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003381 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003382 eap->line1 = 1;
3383 eap->line2 = curbuf->b_ml.ml_line_count;
3384 break;
3385 case ADDR_LOADED_BUFFERS:
3386 {
3387 buf_T *buf = firstbuf;
3388
3389 while (buf->b_next != NULL
3390 && buf->b_ml.ml_mfp == NULL)
3391 buf = buf->b_next;
3392 eap->line1 = buf->b_fnum;
3393 buf = lastbuf;
3394 while (buf->b_prev != NULL
3395 && buf->b_ml.ml_mfp == NULL)
3396 buf = buf->b_prev;
3397 eap->line2 = buf->b_fnum;
3398 break;
3399 }
3400 case ADDR_BUFFERS:
3401 eap->line1 = firstbuf->b_fnum;
3402 eap->line2 = lastbuf->b_fnum;
3403 break;
3404 case ADDR_WINDOWS:
3405 case ADDR_TABS:
3406 if (IS_USER_CMDIDX(eap->cmdidx))
3407 {
3408 eap->line1 = 1;
3409 eap->line2 = eap->addr_type == ADDR_WINDOWS
3410 ? LAST_WIN_NR : LAST_TAB_NR;
3411 }
3412 else
3413 {
3414 // there is no Vim command which uses '%' and
3415 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003416 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003417 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003418 }
3419 break;
3420 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003421 case ADDR_UNSIGNED:
3422 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003423 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003424 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003425 case ADDR_ARGUMENTS:
3426 if (ARGCOUNT == 0)
3427 eap->line1 = eap->line2 = 0;
3428 else
3429 {
3430 eap->line1 = 1;
3431 eap->line2 = ARGCOUNT;
3432 }
3433 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003434 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003435#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003436 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003437 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003438 if (eap->line2 == 0)
3439 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003440#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003441 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003442 case ADDR_NONE:
3443 // Will give an error later if a range is found.
3444 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003445 }
3446 ++eap->addr_count;
3447 }
3448 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3449 {
3450 pos_T *fp;
3451
3452 // '*' - visual area
3453 if (eap->addr_type != ADDR_LINES)
3454 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003455 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003456 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003457 }
3458
3459 ++eap->cmd;
3460 if (!eap->skip)
3461 {
3462 fp = getmark('<', FALSE);
3463 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003464 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003465 eap->line1 = fp->lnum;
3466 fp = getmark('>', FALSE);
3467 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003468 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003469 eap->line2 = fp->lnum;
3470 ++eap->addr_count;
3471 }
3472 }
3473 }
3474 else
3475 eap->line2 = lnum;
3476 eap->addr_count++;
3477
3478 if (*eap->cmd == ';')
3479 {
3480 if (!eap->skip)
3481 {
3482 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003483
Bram Moolenaar0e717042020-04-27 19:29:01 +02003484 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003485 // accept zero as address, so 0;/PATTERN/ works correctly
3486 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003487 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003488 if (eap->line2 > 0)
3489 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003490 else
3491 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003492 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003493 }
3494 }
3495 else if (*eap->cmd != ',')
3496 break;
3497 ++eap->cmd;
3498 }
3499
3500 // One address given: set start and end lines.
3501 if (eap->addr_count == 1)
3502 {
3503 eap->line1 = eap->line2;
3504 // ... but only implicit: really no address given
3505 if (lnum == MAXLNUM)
3506 eap->addr_count = 0;
3507 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003508 ret = OK;
3509
3510theend:
3511 if (need_check_cursor)
3512 check_cursor();
3513 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003514}
3515
3516/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003517 * Append "cmd" to the error message in IObuff.
3518 * Takes care of limiting the length and handling 0xa0, which would be
3519 * invisible otherwise.
3520 */
3521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003522append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003523{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003524 size_t len = STRLEN(IObuff);
3525 char_u *s = cmd;
3526 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003527
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003528 if (len > IOSIZE - 100)
3529 {
3530 // Not enough space, truncate and put in "...".
3531 d = IObuff + IOSIZE - 100;
3532 d -= mb_head_off(IObuff, d);
3533 STRCPY(d, "...");
3534 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003535 STRCAT(IObuff, ": ");
3536 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003537 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003538 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003539 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003540 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003541 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003542 STRCPY(d, "<a0>");
3543 d += 4;
3544 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003545 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3546 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003547 else
3548 MB_COPY_CHAR(s, d);
3549 }
3550 *d = NUL;
3551}
3552
Dominique Pelle748b3082022-01-08 12:41:16 +00003553#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003554/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003555 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3556 * the name. Otherwise just return "start".
3557 */
3558 char_u *
3559skip_option_env_lead(char_u *start)
3560{
3561 char_u *name = start;
3562
3563 if (*start == '&')
3564 {
3565 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3566 name += 3;
3567 else
3568 name += 1;
3569 }
3570 else if (*start == '$')
3571 name += 1;
3572 return name;
3573}
Dominique Pelle748b3082022-01-08 12:41:16 +00003574#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003575
3576/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003577 * Return TRUE and set "*idx" if "p" points to a one letter command.
3578 * If not in Vim9 script:
3579 * - The 'k' command can directly be followed by any character.
3580 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3581 * but :sre[wind] is another command, as are :scr[iptnames],
3582 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3583 */
3584 static int
3585one_letter_cmd(char_u *p, cmdidx_T *idx)
3586{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003587 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003588 return FALSE;
3589 if (*p == 'k')
3590 {
3591 *idx = CMD_k;
3592 return TRUE;
3593 }
3594 if (p[0] == 's'
3595 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3596 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3597 || p[1] == 'g'
3598 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3599 || p[1] == 'I'
3600 || (p[1] == 'r' && p[2] != 'e')))
3601 {
3602 *idx = CMD_substitute;
3603 return TRUE;
3604 }
3605 return FALSE;
3606}
3607
Dominique Pellee764d1b2023-03-12 21:20:59 +00003608#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003609/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003610 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3611 * call.
3612 */
3613 int
3614number_method(char_u *cmd)
3615{
3616 char_u *p = skipdigits(cmd);
3617
3618 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3619}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003620#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003621
3622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003624 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003625 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003626 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 *
3628 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3629 * assignment without "let". Sets eap->cmdidx to the command while returning
3630 * "eap->cmd".
3631 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * Returns NULL for an ambiguous user command.
3633 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 char_u *
3635find_ex_command(
3636 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003637 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003638 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003639 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
3641 int len;
3642 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003643 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003644#ifndef FEAT_EVAL
3645 int vim9 = FALSE;
3646#else
3647 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 /*
3650 * Recognize a Vim9 script function/method call and assignment:
3651 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3652 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003653 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003654 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003656 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003658 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003659 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3660 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003662 int oplen;
3663 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003664 char_u *swp;
3665
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003666 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003667 || (eap->cmd[0] == '$'
3668 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003669 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003670 && (valid_yank_reg(eap->cmd[1], FALSE)
3671 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003672 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003673 if (*eap->cmd == '&')
3674 {
3675 p = eap->cmd + 1;
3676 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3677 p += 2;
3678 p = to_name_end(p, FALSE);
3679 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003680 else if (*eap->cmd == '$')
3681 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003682 else
3683 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003684 if (ends_excmd(*skipwhite(p)))
3685 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003686 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3687 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003688 eap->cmdidx = CMD_eval;
3689 return eap->cmd;
3690 }
3691 // "&option" can be followed by "->" or "=", check below
3692 }
3693
3694 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003696 if (
3697 // "(..." is an expression.
3698 // "funcname(" is always a function call.
3699 *p == '('
3700 || (p == eap->cmd
3701 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003702 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003703 *eap->cmd == '{'
3704 // "'string'->func()" is an expression.
3705 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003706 // '"string"->func()' is an expression.
3707 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003708 // '$"string"->func()' is an expression.
3709 // "$'string'->func()" is an expression.
3710 || (eap->cmd[0] == '$'
3711 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3712 // '0z1234->func()' is an expression.
3713 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003714 // "g:varname" is an expression.
3715 || eap->cmd[1] == ':'
3716 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003717 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003718 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003719 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003720 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3721 {
3722 // "{" by itself is the start of a block.
3723 eap->cmdidx = CMD_block;
3724 return eap->cmd + 1;
3725 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003726 eap->cmdidx = CMD_eval;
3727 return eap->cmd;
3728 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003729
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003730 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003731 // "varname[]" is an expression.
3732 *p == '['
3733 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003734 || (*p == '.'
3735 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3736 // g:[key] is an expression
3737 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003738 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003739 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003740
3741 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003742 // Skip over the whole thing, it can be:
3743 // name.member = val
3744 // name[a : b] = val
3745 // name[idx] = val
3746 // name[idx].member = val
3747 // etc.
3748 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003749 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003750 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003751 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003752 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003753 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003754 || (after[0] == '.' && after[1] == '.'
3755 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003756 eap->cmdidx = CMD_var;
3757 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003758 --emsg_silent;
3759 return eap->cmd;
3760 }
3761
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003762 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3763 // an assignment.
3764 // If there is no line break inside the "[...]" then "p" is
3765 // advanced to after the "]" by to_name_const_end(): check if a "="
3766 // follows.
3767 // If "[...]" has a line break "p" still points at the "[" and it
3768 // can't be an assignment.
3769 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003770 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003771 char_u *eq;
3772
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003773 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003774 if (p == eap->cmd && *p == '[')
3775 {
3776 int count = 0;
3777 int semicolon = FALSE;
3778
3779 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3780 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003781 eq = p;
3782 if (eq != NULL)
3783 {
3784 eq = skipwhite(eq);
3785 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3786 ++eq;
3787 }
3788 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003789 {
3790 eap->cmdidx = CMD_eval;
3791 return eap->cmd;
3792 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003793 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003794 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003795 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003796 return eap->cmd;
3797 }
3798 }
3799
3800 // Recognize an assignment if we recognize the variable name:
3801 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003802 // "@r = expr"
3803 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003804 // "var = expr" where "var" is a variable name or we are skipping
3805 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003806 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003807 oplen = assignment_len(skipwhite(p), &heredoc);
3808 if (oplen > 0)
3809 {
3810 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3811 || *eap->cmd == '&'
3812 || *eap->cmd == '$'
3813 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003814 || (eap->skip && IS_WHITE_OR_NUL(
3815 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003816 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003817 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003818 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003819 return eap->cmd;
3820 }
3821 }
3822
Bram Moolenaar9510d222022-09-11 15:14:05 +01003823 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003824 // "w:varname: number = 123".
3825 if (eap->cmd[1] == ':' && *p == ':')
3826 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003827 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003828 return eap->cmd;
3829 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003830 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003831
Bram Moolenaar31d99482022-05-26 22:24:43 +01003832 // 1234->func() is a method call
3833 if (number_method(eap->cmd))
3834 {
3835 eap->cmdidx = CMD_eval;
3836 return eap->cmd;
3837 }
3838
Bram Moolenaar7b829262021-10-13 15:04:34 +01003839 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3840 // an expression. A global/substitute/list command needs to use a
3841 // longer name.
3842 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3843 {
3844 eap->cmdidx = CMD_eval;
3845 return eap->cmd;
3846 }
3847
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003848 // If it is an ID it might be a variable with an operator on the next
3849 // line, if the variable exists it can't be an Ex command.
3850 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003851 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003852 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3853 {
3854 eap->cmdidx = CMD_eval;
3855 return eap->cmd;
3856 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003857
3858 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003859 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3860 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003861 {
3862 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3863 return eap->cmd + 2;
3864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 }
3866#endif
3867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 /*
3869 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 */
3871 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003872 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 ++p;
3875 }
3876 else
3877 {
3878 while (ASCII_ISALPHA(*p))
3879 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003880 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003881 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003883 while (ASCII_ISALNUM(*p))
3884 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 }
3886 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3887 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003888 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 ++p;
3890 while (ASCII_ISALPHA(*p))
3891 ++p;
3892 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003894 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003895 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 ++p;
3897 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003898 // The "d" command can directly be followed by 'l' or 'p' flag, when
3899 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003900 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003901 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003902 // Check for ":dl", ":dell", etc. to ":deletel": that's
3903 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003904 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003905 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003906 break;
3907 if (i == len - 1)
3908 {
3909 --len;
3910 if (p[-1] == 'l')
3911 eap->flags |= EXFLAG_LIST;
3912 else
3913 eap->flags |= EXFLAG_PRINT;
3914 }
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003917 if (ASCII_ISLOWER(eap->cmd[0]))
3918 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003919 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003920 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003921
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003922 if (command_count != (int)CMD_SIZE)
3923 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003924 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003925 getout(1);
3926 }
3927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003928 // Use a precomputed index for fast look-up in cmdnames[]
3929 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003930 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3931 if (ASCII_ISLOWER(c2))
3932 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3933 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003934 else if (ASCII_ISUPPER(eap->cmd[0]))
3935 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003937 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3940 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3941 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3942 (size_t)len) == 0)
3943 {
3944#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003945 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 *full = TRUE;
3947#endif
3948 break;
3949 }
3950
Bram Moolenaar204852a2022-03-05 12:56:44 +00003951 // :Print and :mode are not supported in Vim9 script.
3952 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003953 if (vim9 && eap->cmdidx != CMD_SIZE)
3954 {
3955 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3956 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003957 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003958 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003959 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003960 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003961 eap->cmdidx = CMD_SIZE;
3962 }
3963 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003964
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003965 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003966 // 'cpoptions'.
3967 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3968 p = eap->cmd;
3969
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003970 // Look for a user defined command as a last resort. Let ":Print" be
3971 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003972 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3973 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003975 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 while (ASCII_ISALNUM(*p))
3977 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003978 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003980 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 eap->cmdidx = CMD_SIZE;
3982 }
3983
Bram Moolenaar204852a2022-03-05 12:56:44 +00003984 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3985 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003986 eap->cmdidx = CMD_finally;
3987
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003988#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003989 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003990 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02003991 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003992 && (eap->cmdidx < 0 ||
3993 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003994 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003995 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3996
3997 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003998 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003999 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004000 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004001#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 return p;
4004}
4005
4006#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004007static struct cmdmod
4008{
4009 char *name;
4010 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004011 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00004012} cmdmods[] = {
4013 {"aboveleft", 3, FALSE},
4014 {"belowright", 3, FALSE},
4015 {"botright", 2, FALSE},
4016 {"browse", 3, FALSE},
4017 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004018 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004019 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004020 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004021 {"keepalt", 5, FALSE},
4022 {"keepjumps", 5, FALSE},
4023 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004024 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004025 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004026 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004027 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004028 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004029 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004030 {"rightbelow", 6, FALSE},
4031 {"sandbox", 3, FALSE},
4032 {"silent", 3, FALSE},
4033 {"tab", 3, TRUE},
4034 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004035 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004036 {"verbose", 4, TRUE},
4037 {"vertical", 4, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004038 {"vim9cmd", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004039};
4040
4041/*
4042 * Return length of a command modifier (including optional count).
4043 * Return zero when it's not a modifier.
4044 */
4045 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004046modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004047{
4048 int i, j;
4049 char_u *p = cmd;
4050
4051 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004052 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02004053 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004054 {
4055 for (j = 0; p[j] != NUL; ++j)
4056 if (p[j] != cmdmods[i].name[j])
4057 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02004058 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00004059 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00004060 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004061 }
4062 return 0;
4063}
4064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065/*
4066 * Return > 0 if an Ex command "name" exists.
4067 * Return 2 if there is an exact match.
4068 * Return 3 if there is an ambiguous match.
4069 */
4070 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004071cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
4073 exarg_T ea;
4074 int full = FALSE;
4075 int i;
4076 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004077 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004079 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02004080 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
4082 for (j = 0; name[j] != NUL; ++j)
4083 if (name[j] != cmdmods[i].name[j])
4084 break;
4085 if (name[j] == NUL && j >= cmdmods[i].minlen)
4086 return (cmdmods[i].name[j] == NUL ? 2 : 1);
4087 }
4088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004089 // Check built-in commands and user defined commands.
4090 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004091 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004093 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004095 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004097 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4098 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004099 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004100 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4102}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004103
4104/*
4105 * "fullcommand" function
4106 */
4107 void
4108f_fullcommand(typval_T *argvars, typval_T *rettv)
4109{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004110 exarg_T ea;
4111 char_u *name;
4112 char_u *p;
4113 int vim9script = in_vim9script();
4114 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004115
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004116 rettv->v_type = VAR_STRING;
4117 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004118
Bram Moolenaaraa534142022-09-15 21:46:02 +01004119 if (in_vim9script()
4120 && (check_for_string_arg(argvars, 0) == FAIL
4121 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004122 return;
4123
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004124 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004125 if (name == NULL)
4126 return;
4127
Bram Moolenaaraa534142022-09-15 21:46:02 +01004128 if (argvars[1].v_type != VAR_UNKNOWN)
4129 {
4130 vim9script = tv_get_bool(&argvars[1]);
4131 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4132 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4133 }
4134
zeertzjqc024ed92022-01-04 16:22:52 +00004135 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004136 name++;
4137 name = skip_range(name, TRUE, NULL);
4138
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004139 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4140 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004141 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004142 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004143 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004144 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004145 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004146 goto theend;
4147
4148 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004149 {
4150 int res;
4151
4152 ++emsg_silent;
4153 res = not_in_vim9(&ea);
4154 --emsg_silent;
4155
4156 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004157 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004158 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004159
4160 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004161 ? get_user_command_name(ea.useridx, ea.cmdidx)
4162 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004163theend:
4164 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004165}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166#endif
4167
Bram Moolenaard0190392019-08-23 21:17:35 +02004168 cmdidx_T
4169excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
Bram Moolenaard0190392019-08-23 21:17:35 +02004171 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004173 if (!one_letter_cmd(cmd, &idx))
4174 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4175 idx = (cmdidx_T)((int)idx + 1))
4176 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4177 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
Bram Moolenaard0190392019-08-23 21:17:35 +02004179 return idx;
4180}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Bram Moolenaard0190392019-08-23 21:17:35 +02004182 long
4183excmd_get_argt(cmdidx_T idx)
4184{
4185 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186}
4187
4188/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004189 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 *
4191 * Backslashed delimiters after / or ? will be skipped, and commands will
4192 * not be expanded between /'s and ?'s or after "'".
4193 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004194 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 * Returns the "cmd" pointer advanced to beyond the range.
4196 */
4197 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004198skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004199 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004200 int skip_star, // skip "*" used for Visual range
4201 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004203 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004204 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004206 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004208 if (*cmd == '\\')
4209 {
4210 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4211 ++cmd;
4212 else
4213 break;
4214 }
4215 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004217 char_u *p = cmd;
4218
4219 // a quote is only valid at the start or after a separator
4220 while (p > cmd_start)
4221 {
4222 --p;
4223 if (!VIM_ISWHITE(*p))
4224 break;
4225 }
4226 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4227 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (*++cmd == NUL && ctx != NULL)
4229 *ctx = EXPAND_NOTHING;
4230 }
4231 else if (*cmd == '/' || *cmd == '?')
4232 {
4233 delim = *cmd++;
4234 while (*cmd != NUL && *cmd != delim)
4235 if (*cmd++ == '\\' && *cmd != NUL)
4236 ++cmd;
4237 if (*cmd == NUL && ctx != NULL)
4238 *ctx = EXPAND_NOTHING;
4239 }
4240 if (*cmd != NUL)
4241 ++cmd;
4242 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004244 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004245 while (*cmd == ':')
4246 cmd = skipwhite(cmd + 1);
4247
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004248 // Skip "*" used for Visual range.
4249 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4250 cmd = skipwhite(cmd + 1);
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return cmd;
4253}
4254
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004255 static void
4256addr_error(cmd_addr_T addr_type)
4257{
4258 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004259 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004260 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004261 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004262}
4263
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004265 * Return the default address for an address type.
4266 */
4267 static linenr_T
4268default_address(exarg_T *eap)
4269{
4270 linenr_T lnum = 0;
4271
4272 switch (eap->addr_type)
4273 {
4274 case ADDR_LINES:
4275 case ADDR_OTHER:
4276 // Default is the cursor line number. Avoid using an invalid
4277 // line number though.
4278 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4279 lnum = curbuf->b_ml.ml_line_count;
4280 else
4281 lnum = curwin->w_cursor.lnum;
4282 break;
4283 case ADDR_WINDOWS:
4284 lnum = CURRENT_WIN_NR;
4285 break;
4286 case ADDR_ARGUMENTS:
4287 lnum = curwin->w_arg_idx + 1;
4288 if (lnum > ARGCOUNT)
4289 lnum = ARGCOUNT;
4290 break;
4291 case ADDR_LOADED_BUFFERS:
4292 case ADDR_BUFFERS:
4293 lnum = curbuf->b_fnum;
4294 break;
4295 case ADDR_TABS:
4296 lnum = CURRENT_TAB_NR;
4297 break;
4298 case ADDR_TABS_RELATIVE:
4299 case ADDR_UNSIGNED:
4300 lnum = 1;
4301 break;
4302 case ADDR_QUICKFIX:
4303#ifdef FEAT_QUICKFIX
4304 lnum = qf_get_cur_idx(eap);
4305#endif
4306 break;
4307 case ADDR_QUICKFIX_VALID:
4308#ifdef FEAT_QUICKFIX
4309 lnum = qf_get_cur_valid_idx(eap);
4310#endif
4311 break;
4312 case ADDR_NONE:
4313 // Will give an error later if a range is found.
4314 break;
4315 }
4316 return lnum;
4317}
4318
4319/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004320 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 *
4322 * Set ptr to the next character after the part that was interpreted.
4323 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004324 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 *
4326 * Return MAXLNUM when no Ex address was found.
4327 */
4328 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004329get_address(
4330 exarg_T *eap UNUSED,
4331 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004332 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004333 int skip, // only skip the address, don't use it
4334 int silent, // no errors or side effects
4335 int to_other_file, // flag: may jump to other file
4336 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337{
4338 int c;
4339 int i;
4340 long n;
4341 char_u *cmd;
4342 pos_T pos;
4343 pos_T *fp;
4344 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004345 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 cmd = skipwhite(*ptr);
4348 lnum = MAXLNUM;
4349 do
4350 {
4351 switch (*cmd)
4352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004353 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004354 ++cmd;
4355 switch (addr_type)
4356 {
4357 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004358 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 lnum = curwin->w_cursor.lnum;
4360 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004361 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004362 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004363 break;
4364 case ADDR_ARGUMENTS:
4365 lnum = curwin->w_arg_idx + 1;
4366 break;
4367 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004368 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004369 lnum = curbuf->b_fnum;
4370 break;
4371 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004372 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004373 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004374 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004375 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004376 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004377 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004378 cmd = NULL;
4379 goto error;
4380 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004381 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004382#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004383 lnum = qf_get_cur_idx(eap);
4384#endif
4385 break;
4386 case ADDR_QUICKFIX_VALID:
4387#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004388 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004389#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004390 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 }
4392 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004394 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 ++cmd;
4396 switch (addr_type)
4397 {
4398 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004399 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 lnum = curbuf->b_ml.ml_line_count;
4401 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004403 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 break;
4405 case ADDR_ARGUMENTS:
4406 lnum = ARGCOUNT;
4407 break;
4408 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004409 buf = lastbuf;
4410 while (buf->b_ml.ml_mfp == NULL)
4411 {
4412 if (buf->b_prev == NULL)
4413 break;
4414 buf = buf->b_prev;
4415 }
4416 lnum = buf->b_fnum;
4417 break;
4418 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 lnum = lastbuf->b_fnum;
4420 break;
4421 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004422 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004424 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004425 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004426 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004427 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004428 cmd = NULL;
4429 goto error;
4430 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004431 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004432#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004433 lnum = qf_get_size(eap);
4434 if (lnum == 0)
4435 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004436#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004437 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004438 case ADDR_QUICKFIX_VALID:
4439#ifdef FEAT_QUICKFIX
4440 lnum = qf_get_valid_size(eap);
4441 if (lnum == 0)
4442 lnum = 1;
4443#endif
4444 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004445 }
4446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004448 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 if (*++cmd == NUL)
4450 {
4451 cmd = NULL;
4452 goto error;
4453 }
4454 if (addr_type != ADDR_LINES)
4455 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004456 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004457 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004458 goto error;
4459 }
4460 if (skip)
4461 ++cmd;
4462 else
4463 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004464 // Only accept a mark in another file when it is
4465 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4467 ++cmd;
4468 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004469 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 lnum = curwin->w_cursor.lnum;
4471 else
4472 {
4473 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 {
4475 cmd = NULL;
4476 goto error;
4477 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 lnum = fp->lnum;
4479 }
4480 }
4481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
4483 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004484 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 c = *cmd++;
4486 if (addr_type != ADDR_LINES)
4487 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004488 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004489 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 goto error;
4491 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004492 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004494 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 if (*cmd == c)
4496 ++cmd;
4497 }
4498 else
4499 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004500 int flags;
4501
4502 pos = curwin->w_cursor; // save curwin->w_cursor
4503
4504 // When '/' or '?' follows another address, start from
4505 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004506 if (lnum > 0 && lnum != MAXLNUM)
4507 curwin->w_cursor.lnum =
4508 lnum > curbuf->b_ml.ml_line_count
4509 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004510
4511 // Start a forward search at the end of the line (unless
4512 // before the first line).
4513 // Start a backward search at the start of the line.
4514 // This makes sure we never match in the current
4515 // line, and can match anywhere in the
4516 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004517 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 curwin->w_cursor.col = MAXCOL;
4519 else
4520 curwin->w_cursor.col = 0;
4521 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004522 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004523 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004524 {
4525 curwin->w_cursor = pos;
4526 cmd = NULL;
4527 goto error;
4528 }
4529 lnum = curwin->w_cursor.lnum;
4530 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004531 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 cmd += searchcmdlen;
4533 }
4534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004536 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004537 ++cmd;
4538 if (addr_type != ADDR_LINES)
4539 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004540 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004541 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004542 goto error;
4543 }
4544 if (*cmd == '&')
4545 i = RE_SUBST;
4546 else if (*cmd == '?' || *cmd == '/')
4547 i = RE_SEARCH;
4548 else
4549 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004550 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004551 cmd = NULL;
4552 goto error;
4553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004555 if (!skip)
4556 {
4557 /*
4558 * When search follows another address, start from
4559 * there.
4560 */
4561 if (lnum != MAXLNUM)
4562 pos.lnum = lnum;
4563 else
4564 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004566 /*
4567 * Start the search just like for the above
4568 * do_search().
4569 */
4570 if (*cmd != '?')
4571 pos.col = MAXCOL;
4572 else
4573 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004574 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004575 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004577 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 lnum = pos.lnum;
4579 else
4580 {
4581 cmd = NULL;
4582 goto error;
4583 }
4584 }
4585 ++cmd;
4586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004589 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004590 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592
4593 for (;;)
4594 {
4595 cmd = skipwhite(cmd);
4596 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4597 break;
4598
4599 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004600 {
4601 switch (addr_type)
4602 {
4603 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004604 case ADDR_OTHER:
4605 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004606 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004607 break;
4608 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004609 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004610 break;
4611 case ADDR_ARGUMENTS:
4612 lnum = curwin->w_arg_idx + 1;
4613 break;
4614 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004615 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004616 lnum = curbuf->b_fnum;
4617 break;
4618 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004619 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004620 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004621 case ADDR_TABS_RELATIVE:
4622 lnum = 1;
4623 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004624 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004625#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004626 lnum = qf_get_cur_idx(eap);
4627#endif
4628 break;
4629 case ADDR_QUICKFIX_VALID:
4630#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004631 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004632#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004633 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004634 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004635 case ADDR_UNSIGNED:
4636 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004637 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004638 }
4639 }
4640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004642 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 else
4644 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004645 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 n = 1;
4647 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004648 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004649 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004651 if (n == MAXLNUM)
4652 {
4653 emsg(_(e_line_number_out_of_range));
4654 goto error;
4655 }
4656 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004657
4658 if (addr_type == ADDR_TABS_RELATIVE)
4659 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004660 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004661 cmd = NULL;
4662 goto error;
4663 }
4664 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004665 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004666 lnum = compute_buffer_local_count(
4667 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004669 {
4670#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004671 // Relative line addressing: need to adjust for lines in a
4672 // closed fold after the first address.
4673 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4674 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004675 (void)hasFolding(lnum, NULL, &lnum);
4676#endif
4677 if (i == '-')
4678 lnum -= n;
4679 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004680 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004681 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004682 {
4683 emsg(_(e_line_number_out_of_range));
4684 goto error;
4685 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004686 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004687 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
4690 } while (*cmd == '/' || *cmd == '?');
4691
4692error:
4693 *ptr = cmd;
4694 return lnum;
4695}
4696
4697/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004698 * Set eap->line1 and eap->line2 to the whole range.
4699 * Used for commands with the EX_DFLALL flag and no range given.
4700 */
4701 static void
4702address_default_all(exarg_T *eap)
4703{
4704 eap->line1 = 1;
4705 switch (eap->addr_type)
4706 {
4707 case ADDR_LINES:
4708 case ADDR_OTHER:
4709 eap->line2 = curbuf->b_ml.ml_line_count;
4710 break;
4711 case ADDR_LOADED_BUFFERS:
4712 {
4713 buf_T *buf = firstbuf;
4714
4715 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4716 buf = buf->b_next;
4717 eap->line1 = buf->b_fnum;
4718 buf = lastbuf;
4719 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4720 buf = buf->b_prev;
4721 eap->line2 = buf->b_fnum;
4722 }
4723 break;
4724 case ADDR_BUFFERS:
4725 eap->line1 = firstbuf->b_fnum;
4726 eap->line2 = lastbuf->b_fnum;
4727 break;
4728 case ADDR_WINDOWS:
4729 eap->line2 = LAST_WIN_NR;
4730 break;
4731 case ADDR_TABS:
4732 eap->line2 = LAST_TAB_NR;
4733 break;
4734 case ADDR_TABS_RELATIVE:
4735 eap->line2 = 1;
4736 break;
4737 case ADDR_ARGUMENTS:
4738 if (ARGCOUNT == 0)
4739 eap->line1 = eap->line2 = 0;
4740 else
4741 eap->line2 = ARGCOUNT;
4742 break;
4743 case ADDR_QUICKFIX_VALID:
4744#ifdef FEAT_QUICKFIX
4745 eap->line2 = qf_get_valid_size(eap);
4746 if (eap->line2 == 0)
4747 eap->line2 = 1;
4748#endif
4749 break;
4750 case ADDR_NONE:
4751 case ADDR_UNSIGNED:
4752 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004753 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004754 break;
4755 }
4756}
4757
4758
4759/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004760 * Get flags from an Ex command argument.
4761 */
4762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004763get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004764{
4765 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4766 {
4767 if (*eap->arg == 'l')
4768 eap->flags |= EXFLAG_LIST;
4769 else if (*eap->arg == 'p')
4770 eap->flags |= EXFLAG_PRINT;
4771 else
4772 eap->flags |= EXFLAG_NR;
4773 eap->arg = skipwhite(eap->arg + 1);
4774 }
4775}
4776
4777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 * Function called for command which is Not Implemented. NI!
4779 */
4780 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004781ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004784 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004785 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786}
4787
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004788#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789/*
4790 * Function called for script command which is Not Implemented. NI!
4791 * Skips over ":perl <<EOF" constructs.
4792 */
4793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004794ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
4796 if (!eap->skip)
4797 ex_ni(eap);
4798 else
4799 vim_free(script_get(eap, eap->arg));
4800}
4801#endif
4802
4803/*
4804 * Check range in Ex command for validity.
4805 * Return NULL when valid, error message when invalid.
4806 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004807 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004808invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004810 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004811
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 if ( eap->line1 < 0
4813 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004814 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004815 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004816
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004817 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004818 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004819 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004820 {
4821 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004822 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004823#ifdef FEAT_DIFF
4824 + (eap->cmdidx == CMD_diffget)
4825#endif
4826 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004827 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004828 break;
4829 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004830 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004831 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004832 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004833 break;
4834 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004835 // Only a boundary check, not whether the buffers actually
4836 // exist.
4837 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004838 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004839 break;
4840 case ADDR_LOADED_BUFFERS:
4841 buf = firstbuf;
4842 while (buf->b_ml.ml_mfp == NULL)
4843 {
4844 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004845 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004846 buf = buf->b_next;
4847 }
4848 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004849 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004850 buf = lastbuf;
4851 while (buf->b_ml.ml_mfp == NULL)
4852 {
4853 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004854 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004855 buf = buf->b_prev;
4856 }
4857 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004858 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004859 break;
4860 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004861 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004862 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004863 break;
4864 case ADDR_TABS:
4865 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004866 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004867 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004868 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004869 case ADDR_OTHER:
4870 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004871 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004872 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004873#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004874 // No error for value that is too big, will use the last entry.
4875 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004876 {
4877 if (eap->addr_count == 0)
4878 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004879 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004880 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004881#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004882 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004883 case ADDR_QUICKFIX_VALID:
4884#ifdef FEAT_QUICKFIX
4885 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4886 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004887 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004888#endif
4889 break;
4890 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004891 case ADDR_NONE:
4892 // Will give an error elsewhere.
4893 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004894 }
4895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 return NULL;
4897}
4898
4899/*
4900 * Correct the range for zero line number, if required.
4901 */
4902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004903correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004905 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
4907 if (eap->line1 == 0)
4908 eap->line1 = 1;
4909 if (eap->line2 == 0)
4910 eap->line2 = 1;
4911 }
4912}
4913
Bram Moolenaar748bf032005-02-02 23:04:36 +00004914#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004915/*
4916 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4917 * pattern. Otherwise return eap->arg.
4918 */
4919 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004920skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004921{
4922 char_u *p = eap->arg;
4923
Bram Moolenaara37420f2006-02-04 22:37:47 +00004924 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4925 || eap->cmdidx == CMD_vimgrepadd
4926 || eap->cmdidx == CMD_lvimgrepadd
4927 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004929 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004930 if (p == NULL)
4931 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004932 }
4933 return p;
4934}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004935
4936/*
4937 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4938 * in the command line, so that things like % get expanded.
4939 */
4940 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004941replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004942{
4943 char_u *new_cmdline;
4944 char_u *program;
4945 char_u *pos;
4946 char_u *ptr;
4947 int len;
4948 int i;
4949
4950 /*
4951 * Don't do it when ":vimgrep" is used for ":grep".
4952 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004953 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4954 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4955 || eap->cmdidx == CMD_grepadd
4956 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004957 && !grep_internal(eap->cmdidx))
4958 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004959 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4960 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004961 {
4962 if (*curbuf->b_p_gp == NUL)
4963 program = p_gp;
4964 else
4965 program = curbuf->b_p_gp;
4966 }
4967 else
4968 {
4969 if (*curbuf->b_p_mp == NUL)
4970 program = p_mp;
4971 else
4972 program = curbuf->b_p_mp;
4973 }
4974
4975 p = skipwhite(p);
4976
4977 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4978 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004979 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004980 i = 1;
4981 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4982 ++i;
4983 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004984 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004985 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004986 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004987 ptr = new_cmdline;
4988 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4989 {
4990 i = (int)(pos - program);
4991 STRNCPY(ptr, program, i);
4992 STRCPY(ptr += i, p);
4993 ptr += len;
4994 program = pos + 2;
4995 }
4996 STRCPY(ptr, program);
4997 }
4998 else
4999 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02005000 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005001 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005002 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005003 STRCPY(new_cmdline, program);
5004 STRCAT(new_cmdline, " ");
5005 STRCAT(new_cmdline, p);
5006 }
5007 msg_make(p);
5008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005009 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005010 vim_free(*cmdlinep);
5011 *cmdlinep = new_cmdline;
5012 p = new_cmdline;
5013 }
5014 return p;
5015}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005016#endif
5017
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018/*
5019 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005020 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 * Return FAIL for failure, OK otherwise.
5022 */
5023 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005024expand_filename(
5025 exarg_T *eap,
5026 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005027 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005029 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 char_u *repl;
5031 int srclen;
5032 char_u *p;
5033 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005034 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
Bram Moolenaar748bf032005-02-02 23:04:36 +00005036#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005037 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005038 p = skip_grep_pat(eap);
5039#else
5040 p = eap->arg;
5041#endif
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 /*
5044 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5045 * the file name contains a wildcard it should not cause expanding.
5046 * (it will be expanded anyway if there is a wildcard before replacing).
5047 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005048 has_wildcards = mch_has_wildcard(p);
5049 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005051#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005052 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005053 if (p[0] == '`' && p[1] == '=')
5054 {
5055 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005056 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005057 if (*p == '`')
5058 ++p;
5059 continue;
5060 }
5061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 /*
5063 * Quick check if this cannot be the start of a special string.
5064 * Also removes backslash before '%', '#' and '<'.
5065 */
5066 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5067 {
5068 ++p;
5069 continue;
5070 }
5071
5072 /*
5073 * Try to find a match at this position.
5074 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005075 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005076 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005077 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005079 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 p += srclen;
5082 continue;
5083 }
5084
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005085 // Wildcards won't be expanded below, the replacement is taken
5086 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005087 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5088 {
5089 char_u *l = repl;
5090
5091 repl = expand_env_save(repl);
5092 vim_free(l);
5093 }
5094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005095 // Need to escape white space et al. with a backslash.
5096 // Don't do this for:
5097 // - replacement that already has been escaped: "##"
5098 // - shell commands (may have to use quotes instead).
5099 // - non-unix systems when there is a single argument (spaces don't
5100 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005102 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 && eap->cmdidx != CMD_grep
5105 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005106 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005107 && eap->cmdidx != CMD_lgrep
5108 && eap->cmdidx != CMD_lgrepadd
5109 && eap->cmdidx != CMD_lmake
5110 && eap->cmdidx != CMD_make
5111 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005113 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114#endif
5115 )
5116 {
5117 char_u *l;
5118#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005119 // Don't escape a backslash here, because rem_backslash() doesn't
5120 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 static char_u *nobslash = (char_u *)" \t\"|";
5122# define ESCAPE_CHARS nobslash
5123#else
5124# define ESCAPE_CHARS escape_chars
5125#endif
5126
5127 for (l = repl; *l; ++l)
5128 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5129 {
5130 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5131 if (l != NULL)
5132 {
5133 vim_free(repl);
5134 repl = l;
5135 }
5136 break;
5137 }
5138 }
5139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005140 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005141 if ((eap->usefilter || eap->cmdidx == CMD_bang
5142 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005143 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 char_u *l;
5146
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005147 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (l != NULL)
5149 {
5150 vim_free(repl);
5151 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153 }
5154
5155 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
5156 vim_free(repl);
5157 if (p == NULL)
5158 return FAIL;
5159 }
5160
5161 /*
5162 * One file argument: Expand wildcards.
5163 * Don't do this with ":r !command" or ":w !command".
5164 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005165 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
5167 /*
5168 * May do this twice:
5169 * 1. Replace environment variables.
5170 * 2. Replace any other wildcards, remove backslashes.
5171 */
5172 for (n = 1; n <= 2; ++n)
5173 {
5174 if (n == 2)
5175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 /*
5177 * Halve the number of backslashes (this is Vi compatible).
5178 * For Unix and OS/2, when wildcards are expanded, this is
5179 * done by ExpandOne() below.
5180 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005181#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (!has_wildcards)
5183#endif
5184 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186
5187 if (has_wildcards)
5188 {
5189 if (n == 1)
5190 {
5191 /*
5192 * First loop: May expand environment variables. This
5193 * can be done much faster with expand_env() than with
5194 * something else (e.g., calling a shell).
5195 * After expanding environment variables, check again
5196 * if there are still wildcards present.
5197 */
5198 if (vim_strchr(eap->arg, '$') != NULL
5199 || vim_strchr(eap->arg, '~') != NULL)
5200 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005201 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005202 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 has_wildcards = mch_has_wildcard(NameBuff);
5204 p = NameBuff;
5205 }
5206 else
5207 p = NULL;
5208 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005209 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
5211 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005212 int options = WILD_LIST_NOTFOUND
5213 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
5215 ExpandInit(&xpc);
5216 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005217 if (p_wic)
5218 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005220 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 if (p == NULL)
5222 return FAIL;
5223 }
5224 if (p != NULL)
5225 {
5226 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5227 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005228 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 vim_free(p);
5230 }
5231 }
5232 }
5233 }
5234 return OK;
5235}
5236
5237/*
5238 * Replace part of the command line, keeping eap->cmd, eap->arg and
5239 * eap->nextcmd correct.
5240 * "src" points to the part that is to be replaced, of length "srclen".
5241 * "repl" is the replacement string.
5242 * Returns a pointer to the character after the replaced string.
5243 * Returns NULL for failure.
5244 */
5245 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005246repl_cmdline(
5247 exarg_T *eap,
5248 char_u *src,
5249 int srclen,
5250 char_u *repl,
5251 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 int len;
5254 int i;
5255 char_u *new_cmdline;
5256
5257 /*
5258 * The new command line is build in new_cmdline[].
5259 * First allocate it.
5260 * Careful: a "+cmd" argument may have been NUL terminated.
5261 */
5262 len = (int)STRLEN(repl);
5263 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005264 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005265 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005266 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005267 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268
5269 /*
5270 * Copy the stuff before the expanded part.
5271 * Copy the expanded stuff.
5272 * Copy what came after the expanded part.
5273 * Copy the next commands, if there are any.
5274 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005275 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005279 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005281 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005283 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
5285 i = (int)STRLEN(new_cmdline) + 1;
5286 STRCPY(new_cmdline + i, eap->nextcmd);
5287 eap->nextcmd = new_cmdline + i;
5288 }
5289 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5290 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5291 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5292 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5293 vim_free(*cmdlinep);
5294 *cmdlinep = new_cmdline;
5295
5296 return src;
5297}
5298
5299/*
5300 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005301 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 */
5303 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005304separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305{
5306 char_u *p;
5307
Bram Moolenaar86b68352004-12-27 21:59:20 +00005308#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005309 p = skip_grep_pat(eap);
5310#else
5311 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005312#endif
5313
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005314 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 {
5316 if (*p == Ctrl_V)
5317 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005318 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005319 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005321 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005322 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005323 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 break;
5325 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005326
5327#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005328 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005329 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005330 {
5331 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005332 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005333 if (*p == NUL) // stop at NUL after CTRL-V
5334 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005335 }
5336#endif
5337
Bram Moolenaareda29c92022-10-02 12:59:00 +01005338 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005339 // :@" and :*" do not start a comment!
5340 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005341 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005342 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005343 && !(eap->argt & EX_NOTRLCOM)
5344 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5345 || p != eap->arg)
5346 && (eap->cmdidx != CMD_redir
5347 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005348 || (*p == '#'
5349 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005350 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005351 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 || *p == '|' || *p == '\n')
5353 {
5354 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005355 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 * AND 'b' is present in 'cpoptions'.
5357 */
5358 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005359 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005361 if (!keep_backslash)
5362 {
5363 STRMOVE(p - 1, p); // remove the '\'
5364 --p;
5365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367 else
5368 {
5369 eap->nextcmd = check_nextcmd(p);
5370 *p = NUL;
5371 break;
5372 }
5373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005375
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 del_trailing_spaces(eap->arg);
5378}
5379
5380/*
5381 * get + command from ex argument
5382 */
5383 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005384getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
5386 char_u *arg = *argp;
5387 char_u *command = NULL;
5388
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005389 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
5391 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005392 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 command = dollar_command;
5394 else
5395 {
5396 command = arg;
5397 arg = skip_cmd_arg(command, TRUE);
5398 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005399 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
5401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005402 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 *argp = arg;
5404 }
5405 return command;
5406}
5407
5408/*
5409 * Find end of "+command" argument. Skip over "\ " and "\\".
5410 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005411 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005412skip_cmd_arg(
5413 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005414 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 while (*p && !vim_isspace(*p))
5417 {
5418 if (*p == '\\' && p[1] != NUL)
5419 {
5420 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005421 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 else
5423 ++p;
5424 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005425 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 }
5427 return p;
5428}
5429
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005430 int
5431get_bad_opt(char_u *p, exarg_T *eap)
5432{
5433 if (STRICMP(p, "keep") == 0)
5434 eap->bad_char = BAD_KEEP;
5435 else if (STRICMP(p, "drop") == 0)
5436 eap->bad_char = BAD_DROP;
5437 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5438 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005439 else
5440 return FAIL;
5441 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005442}
5443
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005445 * Function given to ExpandGeneric() to obtain the list of bad= names.
5446 */
5447 static char_u *
5448get_bad_name(expand_T *xp UNUSED, int idx)
5449{
5450 // Note: Keep this in sync with getargopt.
5451 static char *(p_bad_values[]) =
5452 {
5453 "?",
5454 "keep",
5455 "drop",
5456 };
5457
5458 if (idx < (int)ARRAY_LENGTH(p_bad_values))
5459 return (char_u*)p_bad_values[idx];
5460 return NULL;
5461}
5462
5463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 * Get "++opt=arg" argument.
5465 * Return FAIL or OK.
5466 */
5467 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
5470 char_u *arg = eap->arg + 2;
5471 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005472 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005475 // Note: Keep this in sync with get_argopt_name.
5476
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005477 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5479 {
5480 if (*arg == 'n')
5481 {
5482 arg += 2;
5483 eap->force_bin = FORCE_NOBIN;
5484 }
5485 else
5486 eap->force_bin = FORCE_BIN;
5487 if (!checkforcmd(&arg, "binary", 3))
5488 return FAIL;
5489 eap->arg = skipwhite(arg);
5490 return OK;
5491 }
5492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005493 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005494 if (STRNCMP(arg, "edit", 4) == 0)
5495 {
5496 eap->read_edit = TRUE;
5497 eap->arg = skipwhite(arg + 4);
5498 return OK;
5499 }
5500
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (STRNCMP(arg, "ff", 2) == 0)
5502 {
5503 arg += 2;
5504 pp = &eap->force_ff;
5505 }
5506 else if (STRNCMP(arg, "fileformat", 10) == 0)
5507 {
5508 arg += 10;
5509 pp = &eap->force_ff;
5510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 else if (STRNCMP(arg, "enc", 3) == 0)
5512 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005513 if (STRNCMP(arg, "encoding", 8) == 0)
5514 arg += 8;
5515 else
5516 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 pp = &eap->force_enc;
5518 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005519 else if (STRNCMP(arg, "bad", 3) == 0)
5520 {
5521 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005522 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525 if (pp == NULL || *arg != '=')
5526 return FAIL;
5527
5528 ++arg;
5529 *pp = (int)(arg - eap->cmd);
5530 arg = skip_cmd_arg(arg, FALSE);
5531 eap->arg = skipwhite(arg);
5532 *arg = NUL;
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (pp == &eap->force_ff)
5535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5537 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005538 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005540 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005542 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5544 *p = TOLOWER_ASC(*p);
5545 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005546 else
5547 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005548 // Check ++bad= argument. Must be a single-byte character, "keep" or
5549 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005550 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005551 return FAIL;
5552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
5554 return OK;
5555}
5556
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005557/*
5558 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5559 */
5560 static char_u *
5561get_argopt_name(expand_T *xp UNUSED, int idx)
5562{
5563 // Note: Keep this in sync with getargopt.
5564 static char *(p_opt_values[]) =
5565 {
5566 "fileformat=",
5567 "encoding=",
5568 "binary",
5569 "nobinary",
5570 "bad=",
5571 "edit",
5572 };
5573
5574 if (idx < (int)ARRAY_LENGTH(p_opt_values))
5575 return (char_u*)p_opt_values[idx];
5576 return NULL;
5577}
5578
5579/*
5580 * Command-line expansion for ++opt=name.
5581 */
5582 int
5583expand_argopt(
5584 char_u *pat,
5585 expand_T *xp,
5586 regmatch_T *rmp,
5587 char_u ***matches,
5588 int *numMatches)
5589{
5590 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5591 {
5592 char_u *(*cb)(expand_T *, int) = NULL;
5593
5594 char_u *name_end = xp->xp_pattern - 1;
5595 if (name_end - xp->xp_line >= 2
5596 && STRNCMP(name_end - 2, "ff", 2) == 0)
5597 cb = get_fileformat_name;
5598 else if (name_end - xp->xp_line >= 10
5599 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5600 cb = get_fileformat_name;
5601 else if (name_end - xp->xp_line >= 3
5602 && STRNCMP(name_end - 3, "enc", 3) == 0)
5603 cb = get_encoding_name;
5604 else if (name_end - xp->xp_line >= 8
5605 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5606 cb = get_encoding_name;
5607 else if (name_end - xp->xp_line >= 3
5608 && STRNCMP(name_end - 3, "bad", 3) == 0)
5609 cb = get_bad_name;
5610
5611 if (cb != NULL)
5612 {
5613 return ExpandGeneric(
5614 pat,
5615 xp,
5616 rmp,
5617 matches,
5618 numMatches,
5619 cb,
5620 FALSE);
5621 }
5622 return FAIL;
5623 }
5624
5625 // Special handling of "ff" which acts as a short form of
5626 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005627 if (xp->xp_pattern_len == 2
5628 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005629 {
5630 *matches = ALLOC_MULT(char_u *, 1);
5631 if (*matches == NULL)
5632 return FAIL;
5633 *numMatches = 1;
5634 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5635 return OK;
5636 }
5637
5638 return ExpandGeneric(
5639 pat,
5640 xp,
5641 rmp,
5642 matches,
5643 numMatches,
5644 get_argopt_name,
5645 FALSE);
5646}
5647
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005649ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
5651 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005652 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 * directory for security reasons.
5654 */
5655 if (secure)
5656 {
5657 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005658 eap->errmsg =
5659 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005662 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 else
5664 do_augroup(eap->arg, eap->forceit);
5665}
5666
5667/*
5668 * ":doautocmd": Apply the automatic commands to the current buffer.
5669 */
5670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005673 char_u *arg = eap->arg;
5674 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005675 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005676
Bram Moolenaar1610d052016-06-09 22:53:01 +02005677 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005678 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005679 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005680 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683/*
5684 * :[N]bunload[!] [N] [bufname] unload buffer
5685 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5686 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5687 */
5688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005691 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005692 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 eap->errmsg = do_bufdel(
5694 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5695 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5696 : DOBUF_UNLOAD, eap->arg,
5697 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5698}
5699
5700/*
5701 * :[N]buffer [N] to buffer N
5702 * :[N]sbuffer [N] to buffer N
5703 */
5704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005705ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005707 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005708 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005710 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 else
5712 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005713 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5715 else
5716 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005717 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005718 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720}
5721
5722/*
5723 * :[N]bmodified [N] to next mod. buffer
5724 * :[N]sbmodified [N] to next mod. buffer
5725 */
5726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005727ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005730 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005731 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732}
5733
5734/*
5735 * :[N]bnext [N] to next buffer
5736 * :[N]sbnext [N] split and to next buffer
5737 */
5738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005741 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005742 return;
5743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005745 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005746 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747}
5748
5749/*
5750 * :[N]bNext [N] to previous buffer
5751 * :[N]bprevious [N] to previous buffer
5752 * :[N]sbNext [N] split and to previous buffer
5753 * :[N]sbprevious [N] split and to previous buffer
5754 */
5755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005756ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005758 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005759 return;
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005762 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005763 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764}
5765
5766/*
5767 * :brewind to first buffer
5768 * :bfirst to first buffer
5769 * :sbrewind split and to first buffer
5770 * :sbfirst split and to first buffer
5771 */
5772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005773ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005775 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005776 return;
5777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005779 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005780 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781}
5782
5783/*
5784 * :blast to last buffer
5785 * :sblast split and to last buffer
5786 */
5787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005788ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005790 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005791 return;
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005794 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005795 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaar7a092242020-04-16 22:10:49 +02005798/*
5799 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005800 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005803ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005805 int comment_char = '"';
5806
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005807 if (in_vim9script())
5808 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005809 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005810}
5811
5812/*
5813 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5814 * to "cmd_start" or has a white space character before it.
5815 */
5816 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005817ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005818{
5819 int c = *cmd;
5820
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005821 if (c == NUL || c == '|' || c == '\n')
5822 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005823 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005824 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5825 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005826 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005827 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828}
5829
5830#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5831 || defined(PROTO)
5832/*
5833 * Return the next command, after the first '|' or '\n'.
5834 * Return NULL if not found.
5835 */
5836 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005837find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 while (*p != '|' && *p != '\n')
5840 {
5841 if (*p == NUL)
5842 return NULL;
5843 ++p;
5844 }
5845 return (p + 1);
5846}
5847#endif
5848
5849/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005850 * Check if *p is a separator between Ex commands, skipping over white space.
5851 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 */
5853 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005854check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005856 char_u *s = skipwhite(p);
5857
5858 if (*s == '|' || *s == '\n')
5859 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 else
5861 return NULL;
5862}
5863
5864/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005865 * If "eap->nextcmd" is not set, check for a next command at "p".
5866 */
5867 void
5868set_nextcmd(exarg_T *eap, char_u *arg)
5869{
5870 char_u *p = check_nextcmd(arg);
5871
5872 if (eap->nextcmd == NULL)
5873 eap->nextcmd = p;
5874 else if (p != NULL)
5875 // cannot use "| command" inside a {} block
5876 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5877}
5878
5879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 * - if there are more files to edit
5881 * - and this is the last window
5882 * - and forceit not used
5883 * - and not repeated twice on a row
5884 * return FAIL and give error message if 'message' TRUE
5885 * return OK otherwise
5886 */
5887 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005888check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005889 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005890 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5893
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005894 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005895 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {
5897 if (message)
5898 {
5899#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005900 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5901 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005903 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005905 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5906 NGETTEXT("%d more file to edit. Quit anyway?",
5907 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5909 return OK;
5910 return FAIL;
5911 }
5912#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005913 semsg(NGETTEXT(e_nr_more_file_to_edit,
5914 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005915 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917 return FAIL;
5918 }
5919 return OK;
5920}
5921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922/*
5923 * Function given to ExpandGeneric() to obtain the list of command names.
5924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005926get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927{
5928 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005929 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 return cmdnames[idx].cmd_name;
5931}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005934ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935{
Bram Moolenaare6850792010-05-14 15:28:44 +02005936 if (*eap->arg == NUL)
5937 {
5938#ifdef FEAT_EVAL
5939 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5940 char_u *p = NULL;
5941
5942 if (expr != NULL)
5943 {
5944 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005945 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005946 --emsg_off;
5947 vim_free(expr);
5948 }
5949 if (p != NULL)
5950 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005951 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005952 vim_free(p);
5953 }
5954 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005955 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005956#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005957 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005958#endif
5959 }
5960 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005961 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005962
5963#ifdef FEAT_VTP
5964 else if (has_vtp_working())
5965 {
5966 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005967 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005968 redrawcmd();
5969 }
5970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971}
5972
5973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005974ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
5976 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005977 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 do_highlight(eap->arg, eap->forceit, FALSE);
5979}
5980
5981
5982/*
5983 * Call this function if we thought we were going to exit, but we won't
5984 * (because of an error). May need to restore the terminal mode.
5985 */
5986 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005987not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 exiting = FALSE;
5990 settmode(TMODE_RAW);
5991}
5992
Bram Moolenaar3552e742021-05-29 12:21:58 +02005993 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005994before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5995{
5996 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5997
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005998 // Bail out when autocommands closed the window.
5999 // Refuse to quit when the buffer in the last window is being closed (can
6000 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006001 if (!win_valid(wp)
6002 || curbuf_locked()
6003 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6004 return TRUE;
6005
6006 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6007 {
6008 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006009 // Refuse to quit when locked or when the window was closed or the
6010 // buffer in the last window is being closed (can only happen in
6011 // autocommands).
6012 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006013 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6014 return TRUE;
6015 }
6016
6017 return FALSE;
6018}
6019
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006021 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006022 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006023 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006025 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006026ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006028 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006029
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 if (cmdwin_type != 0)
6031 {
6032 cmdwin_result = Ctrl_C;
6033 return;
6034 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006035 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006036 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006037 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006038 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006039 return;
6040 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006041 if (eap->addr_count > 0)
6042 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006043 int wnr = eap->line2;
6044
6045 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6046 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006047 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006048 }
6049 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006050 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006052 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006053 if (curbuf_locked())
6054 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006055
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006056 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006057 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006058 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060#ifdef FEAT_NETBEANS_INTG
6061 netbeansForcedQuit = eap->forceit;
6062#endif
6063
6064 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006065 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 */
6067 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6068 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006069 if ((!buf_hide(wp->w_buffer)
6070 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006071 | (eap->forceit ? CCGD_FORCEIT : 0)
6072 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006074 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 {
6076 not_exiting();
6077 }
6078 else
6079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006080 // quit last window
6081 // Note: only_one_window() returns true, even so a help window is
6082 // still open. In that case only quit, if no address has been
6083 // specified. Example:
6084 // :h|wincmd w|1q - don't quit
6085 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006086 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006088 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006089#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006091#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006092 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006093 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 }
6095}
6096
6097/*
6098 * ":cquit".
6099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006101ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006103 // this does not always pass on the exit code to the Manx compiler. why?
6104 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105}
6106
6107/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006108 * Do preparations for "qall" and "wqall".
6109 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006111 int
6112before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 if (cmdwin_type != 0)
6115 {
6116 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006117 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 else
6119 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006120 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006123 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006124 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006125 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006126 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006127 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006128 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006129
6130 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006131 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006132
Bram Moolenaar411da642023-05-10 16:53:27 +01006133 return OK;
6134}
6135
6136/*
6137 * ":qall": try to quit all windows
6138 */
6139 static void
6140ex_quit_all(exarg_T *eap)
6141{
6142 if (before_quit_all(eap) == FAIL)
6143 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006145 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 getout(0);
6147 not_exiting();
6148}
6149
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150/*
6151 * ":close": close current window, unless it is the last one
6152 */
6153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006154ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006156 win_T *win;
6157 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006159 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006161 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006162 {
6163 if (eap->addr_count == 0)
6164 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006165 else
6166 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006167 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006168 {
6169 winnr++;
6170 if (winnr == eap->line2)
6171 break;
6172 }
6173 if (win == NULL)
6174 win = lastwin;
6175 ex_win_close(eap->forceit, win, NULL);
6176 }
6177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178}
6179
Bram Moolenaar4033c552017-09-16 20:54:51 +02006180#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006181/*
6182 * ":pclose": Close any preview window.
6183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006185ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186{
6187 win_T *win;
6188
Bram Moolenaar79648732019-07-18 21:43:07 +02006189 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006190 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006191 if (win->w_p_pvw)
6192 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006193 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006194 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006195 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006196# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006197 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006198 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006199# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006200}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006201#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006202
Bram Moolenaarf740b292006-02-16 22:11:02 +00006203/*
6204 * Close window "win" and take care of handling closing the last window for a
6205 * modified buffer.
6206 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006208ex_win_close(
6209 int forceit,
6210 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006211 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 int need_hide;
6214 buf_T *buf = win->w_buffer;
6215
Bram Moolenaarcf844172020-06-26 19:44:06 +02006216 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006217 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006218 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006219 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006220 return;
6221 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006222 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006223 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006224
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006226 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006228#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006229 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006231# ifdef FEAT_TERMINAL
6232 if (term_job_running(buf->b_term))
6233 {
6234 if (term_confirm_stop(buf) == FAIL)
6235 return;
6236 // Manually kill the terminal here because this command will
6237 // hide it otherwise.
6238 free_terminal(buf);
6239 need_hide = FALSE;
6240 }
6241 else
6242# endif
6243 {
6244 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006245
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006246 set_bufref(&bufref, buf);
6247 dialog_changed(buf, FALSE);
6248 if (bufref_valid(&bufref) && bufIsChanged(buf))
6249 return;
6250 need_hide = FALSE;
6251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 }
6253 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006256 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 return;
6258 }
6259 }
6260
Bram Moolenaar4033c552017-09-16 20:54:51 +02006261#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006263#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006265 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006266 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006267 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006268 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006269 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270}
6271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006273 * Handle the argument for a tabpage related ex command.
6274 * Returns a tabpage number.
6275 * When an error is encountered then eap->errmsg is set.
6276 */
6277 static int
6278get_tabpage_arg(exarg_T *eap)
6279{
6280 int tab_number;
6281 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6282
6283 if (eap->arg && *eap->arg != NUL)
6284 {
6285 char_u *p = eap->arg;
6286 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006287 int relative = 0; // argument +N/-N means: go to N places to the
6288 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006289
6290 if (*p == '-')
6291 {
6292 relative = -1;
6293 p++;
6294 }
6295 else if (*p == '+')
6296 {
6297 relative = 1;
6298 p++;
6299 }
6300
6301 p_save = p;
6302 tab_number = getdigits(&p);
6303
6304 if (relative == 0)
6305 {
6306 if (STRCMP(p, "$") == 0)
6307 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006308 else if (STRCMP(p, "#") == 0)
6309 if (valid_tabpage(lastused_tabpage))
6310 tab_number = tabpage_index(lastused_tabpage);
6311 else
6312 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006313 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6314 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006315 tab_number = 0;
6316 goto theend;
6317 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006318 else if (p == p_save || *p_save == '-' || *p != NUL
6319 || tab_number > LAST_TAB_NR)
6320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006321 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006322 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006323 goto theend;
6324 }
6325 }
6326 else
6327 {
6328 if (*p_save == NUL)
6329 tab_number = 1;
6330 else if (p == p_save || *p_save == '-' || *p != NUL
6331 || tab_number == 0)
6332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006333 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006334 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006335 goto theend;
6336 }
6337 tab_number = tab_number * relative + tabpage_index(curtab);
6338 if (!unaccept_arg0 && relative == -1)
6339 --tab_number;
6340 }
6341 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006342 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006343 }
6344 else if (eap->addr_count > 0)
6345 {
6346 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006347 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006348 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006349 tab_number = 0;
6350 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006351 else
6352 {
6353 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006354 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006355 {
zeertzjq076faac2024-03-25 16:41:06 +01006356 char_u *cmdp = eap->cmd;
6357
6358 while (--cmdp > *eap->cmdlinep
6359 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6360 ;
6361 if (*cmdp == '-')
6362 {
6363 --tab_number;
6364 if (tab_number < unaccept_arg0)
6365 eap->errmsg = _(e_invalid_range);
6366 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006367 }
6368 }
6369 }
6370 else
6371 {
6372 switch (eap->cmdidx)
6373 {
6374 case CMD_tabnext:
6375 tab_number = tabpage_index(curtab) + 1;
6376 if (tab_number > LAST_TAB_NR)
6377 tab_number = 1;
6378 break;
6379 case CMD_tabmove:
6380 tab_number = LAST_TAB_NR;
6381 break;
6382 default:
6383 tab_number = tabpage_index(curtab);
6384 }
6385 }
6386
6387theend:
6388 return tab_number;
6389}
6390
6391/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006392 * ":tabclose": close current tab page, unless it is the last one.
6393 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 */
6395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006396ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006398 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006399 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006400
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006402 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006403 cmdwin_result = K_IGNORE;
6404 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006405 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006406
6407 if (first_tabpage->tp_next == NULL)
6408 {
6409 emsg(_(e_cannot_close_last_tab_page));
6410 return;
6411 }
6412
6413 if (window_layout_locked(CMD_tabclose))
6414 return;
6415
6416 tab_number = get_tabpage_arg(eap);
6417 if (eap->errmsg != NULL)
6418 return;
6419
6420 tp = find_tabpage(tab_number);
6421 if (tp == NULL)
6422 {
6423 beep_flush();
6424 return;
6425 }
6426 if (tp != curtab)
6427 {
6428 tabpage_close_other(tp, eap->forceit);
6429 return;
6430 }
6431 else if (!text_locked() && !curbuf_locked())
6432 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006433}
6434
6435/*
6436 * ":tabonly": close all tab pages except the current one
6437 */
6438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006439ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006440{
6441 tabpage_T *tp;
6442 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006443 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006444
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006445 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006446 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006447 cmdwin_result = K_IGNORE;
6448 return;
6449 }
6450
6451 if (first_tabpage->tp_next == NULL)
6452 {
6453 msg(_("Already only one tab page"));
6454 return;
6455 }
6456
6457 if (window_layout_locked(CMD_tabonly))
6458 return;
6459
6460 tab_number = get_tabpage_arg(eap);
6461 if (eap->errmsg != NULL)
6462 return;
6463
6464 goto_tabpage(tab_number);
6465 // Repeat this up to a 1000 times, because autocommands may
6466 // mess up the lists.
6467 for (done = 0; done < 1000; ++done)
6468 {
6469 FOR_ALL_TABPAGES(tp)
6470 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006471 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006472 tabpage_close_other(tp, eap->forceit);
6473 // if we failed to close it quit
6474 if (valid_tabpage(tp))
6475 done = 1000;
6476 // start over, "tp" is now invalid
6477 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006478 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006479 if (first_tabpage->tp_next == NULL)
6480 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
6484/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006485 * Close the current tab page.
6486 */
6487 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006488tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006489{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006490 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006491 return;
6492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006493 // First close all the windows but the current one. If that worked then
6494 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006495 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006496 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006497 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006498 ex_win_close(forceit, curwin, NULL);
6499# ifdef FEAT_GUI
6500 need_mouse_correct = TRUE;
6501# endif
6502}
6503
6504/*
6505 * Close tab page "tp", which is not the current tab page.
6506 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006507 * Also takes care of the tab pages line disappearing when closing the
6508 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006509 */
6510 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006511tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006512{
6513 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006514 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006516 // Limit to 1000 windows, autocommands may add a window while we close
6517 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006518 while (++done < 1000)
6519 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006520 wp = tp->tp_firstwin;
6521 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006523 // Autocommands may delete the tab page under our fingers and we may
6524 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006525 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006526 break;
6527 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006528
Bram Moolenaar29323592016-07-24 22:04:11 +02006529 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006530}
6531
6532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 * ":only".
6534 */
6535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006536ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006538 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006539 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540# ifdef FEAT_GUI
6541 need_mouse_correct = TRUE;
6542# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006543 if (eap->addr_count > 0)
6544 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006545 win_T *wp;
6546 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006547 for (wp = firstwin; --wnr > 0; )
6548 {
6549 if (wp->w_next == NULL)
6550 break;
6551 else
6552 wp = wp->w_next;
6553 }
6554 win_goto(wp);
6555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 close_others(TRUE, eap->forceit);
6557}
6558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006560ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006562 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006563 if (eap->skip)
6564 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006565
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006566 if (window_layout_locked(CMD_hide))
6567 return;
6568#ifdef FEAT_GUI
6569 need_mouse_correct = TRUE;
6570#endif
6571 if (eap->addr_count == 0)
6572 win_close(curwin, FALSE); // don't free buffer
6573 else
6574 {
6575 int winnr = 0;
6576 win_T *win;
6577
6578 FOR_ALL_WINDOWS(win)
6579 {
6580 winnr++;
6581 if (winnr == eap->line2)
6582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006584 if (win == NULL)
6585 win = lastwin;
6586 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 }
6588}
6589
6590/*
6591 * ":stop" and ":suspend": Suspend Vim.
6592 */
dbivolaruab16ad32021-12-29 19:41:47 +00006593 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006594ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
6596 /*
6597 * Disallow suspending for "rvim".
6598 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006599 if (check_restricted())
6600 return;
6601
6602 if (!eap->forceit)
6603 autowrite_all();
6604 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6605 windgoto((int)Rows - 1, 0);
6606 out_char('\n');
6607 out_flush();
6608 stoptermcap();
6609 out_flush(); // needed for SUN to restore xterm buffer
6610 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6611 ui_suspend(); // call machine specific function
6612 maketitle();
6613 resettitle(); // force updating the title
6614 starttermcap();
6615 scroll_start(); // scroll screen before redrawing
6616 redraw_later_clear();
6617 shell_resized(); // may have resized window
6618 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619}
6620
6621/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006622 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 */
6624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006625ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006627#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006628 if (not_in_vim9(eap) == FAIL)
6629 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (cmdwin_type != 0)
6632 {
6633 cmdwin_result = Ctrl_C;
6634 return;
6635 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006636 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006637 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006638 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006639 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006640 return;
6641 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006642
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006644 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 */
6646 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6647 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006648
6649 // Write the buffer for ":wq" or when it was changed.
6650 // Trigger QuitPre and ExitPre.
6651 // Check if we can exit now, after autocommands have changed things.
6652 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6653 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006655 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 {
6657 not_exiting();
6658 }
6659 else
6660 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006661 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006663 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664# ifdef FEAT_GUI
6665 need_mouse_correct = TRUE;
6666# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006667 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006668 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 }
6670}
6671
6672/*
6673 * ":print", ":list", ":number".
6674 */
6675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006676ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006678 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006679 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006680 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006682 for ( ;!got_int; ui_breakcheck())
6683 {
6684 print_line(eap->line1,
6685 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6686 || (eap->flags & EXFLAG_NR)),
6687 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6688 if (++eap->line1 > eap->line2)
6689 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006690 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006691 }
6692 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006693 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006694 curwin->w_cursor.lnum = eap->line2;
6695 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 }
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699}
6700
6701#ifdef FEAT_BYTEOFF
6702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006703ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
6705 goto_byte(eap->line2);
6706}
6707#endif
6708
6709/*
6710 * ":shell".
6711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006713ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 do_shell(NULL, 0);
6716}
6717
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006718#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006720static int drop_busy = FALSE;
6721static int drop_filec;
6722static char_u **drop_filev = NULL;
6723static int drop_split;
6724static void (*drop_callback)(void *);
6725static void *drop_cookie;
6726
6727 static void
6728handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 exarg_T ea;
6731 int save_msg_scroll = msg_scroll;
6732
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006733 // Setting the argument list may cause screen updates and being called
6734 // recursively. Avoid that by setting drop_busy.
6735 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006737 // Check whether the current buffer is changed. If so, we will need
6738 // to split the current window or data could be lost.
6739 // We don't need to check if the 'hidden' option is set, as in this
6740 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006741 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
6743 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006744 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 --emsg_off;
6746 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006747 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if (win_split(0, 0) == FAIL)
6750 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006751 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006753 // When splitting the window, create a new alist. Otherwise the
6754 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 alist_unlink(curwin->w_alist);
6756 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 }
6758
6759 /*
6760 * Set up the new argument list.
6761 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006762 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
6764 /*
6765 * Move to the first file.
6766 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006767 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006768 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 ea.cmd = (char_u *)"next";
6770 do_argfile(&ea, 0);
6771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006772 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6773 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 need_start_insertmode = FALSE;
6775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006776 // Restore msg_scroll, otherwise a following command may cause scrolling
6777 // unexpectedly. The screen will be redrawn by the caller, thus
6778 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006780
6781 if (drop_callback != NULL)
6782 drop_callback(drop_cookie);
6783
6784 drop_filev = NULL;
6785 drop_busy = FALSE;
6786}
6787
6788/*
6789 * Handle a file drop. The code is here because a drop is *nearly* like an
6790 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006791 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006792 * code is very similar to :args and hence needs access to a lot of the static
6793 * functions in this file.
6794 *
6795 * The "filev" list must have been allocated using alloc(), as should each item
6796 * in the list. This function takes over responsibility for freeing the "filev"
6797 * list.
6798 */
6799 void
6800handle_drop(
6801 int filec, // the number of files dropped
6802 char_u **filev, // the list of files dropped
6803 int split, // force splitting the window
6804 void (*callback)(void *), // to be called after setting the argument
6805 // list
6806 void *cookie) // argument for "callback" (allocated)
6807{
6808 // Cannot handle recursive drops, finish the pending one.
6809 if (drop_busy)
6810 {
6811 FreeWild(filec, filev);
6812 vim_free(cookie);
6813 return;
6814 }
6815
6816 // When calling handle_drop() more than once in a row we only use the last
6817 // one.
6818 if (drop_filev != NULL)
6819 {
6820 FreeWild(drop_filec, drop_filev);
6821 vim_free(drop_cookie);
6822 }
6823
6824 drop_filec = filec;
6825 drop_filev = filev;
6826 drop_split = split;
6827 drop_callback = callback;
6828 drop_cookie = cookie;
6829
6830 // Postpone this when:
6831 // - editing the command line
6832 // - not possible to change the current buffer
6833 // - updating the screen
6834 // As it may change buffers and window structures that are in use and cause
6835 // freed memory to be used.
6836 if (text_locked() || curbuf_locked() || updating_screen)
6837 return;
6838
6839 handle_drop_internal();
6840}
6841
6842/*
6843 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6844 * reset: Handle a postponed drop.
6845 */
6846 void
6847handle_any_postponed_drop(void)
6848{
6849 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006850 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006851 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852}
6853#endif
6854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 * ":preserve".
6857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006859ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006861 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 ml_preserve(curbuf, TRUE);
6863}
6864
6865/*
6866 * ":recover".
6867 */
6868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006869ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006871 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006873 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6874 | CCGD_MULTWIN
6875 | (eap->forceit ? CCGD_FORCEIT : 0)
6876 | CCGD_EXCMD)
6877
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 && (*eap->arg == NUL
6879 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006880 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 recoverymode = FALSE;
6882}
6883
6884/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006885 * Command modifier used in a wrong way. Also for other commands that can't
6886 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 */
6888 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006889ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006891 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892}
6893
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894/*
6895 * :sview [+command] file split window with new file, read-only
6896 * :split [[+command] file] split window with current or new file
6897 * :vsplit [[+command] file] split window vertically with current or new file
6898 * :new [[+command] file] split window with no or new file
6899 * :vnew [[+command] file] split vertically window with no or new file
6900 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006901 *
6902 * :tabedit open new Tab page with empty window
6903 * :tabedit [+command] file open new Tab page and edit "file"
6904 * :tabnew [[+command] file] just like :tabedit
6905 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 */
6907 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006908ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006910 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006912#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006913 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006914 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006915#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006916 int use_tab = eap->cmdidx == CMD_tabedit
6917 || eap->cmdidx == CMD_tabfind
6918 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006920 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006921 return;
6922
Bram Moolenaar4033c552017-09-16 20:54:51 +02006923#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
Bram Moolenaar4033c552017-09-16 20:54:51 +02006927#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006928 // A ":split" in the quickfix window works like ":new". Don't want two
6929 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006930 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
6932 if (eap->cmdidx == CMD_split)
6933 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 if (eap->cmdidx == CMD_vsplit)
6935 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006939 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006941 char_u *file_to_find = NULL;
6942 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006944 FNAME_MESS, TRUE, curbuf->b_ffname,
6945 &file_to_find, &search_ctx);
6946 vim_free(file_to_find);
6947 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 if (fname == NULL)
6949 goto theend;
6950 eap->arg = fname;
6951 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006952# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006953 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 && eap->cmdidx != CMD_new)
6956 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006957 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006958# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006959 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006960# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006961 au_has_group((char_u *)"FileExplorer"))
6962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006963 // No browsing supported but we do have the file explorer:
6964 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006965 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006966 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006967 }
6968 else
6969 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006970 fname = do_browse(0, (char_u *)(use_tab
6971 ? _("Edit File in new tab page")
6972 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006974 if (fname == NULL)
6975 goto theend;
6976 eap->arg = fname;
6977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006979 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006982 /*
6983 * Either open new tab page or split the window.
6984 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006985 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006986 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006987 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006988 : eap->addr_count == 0 ? 0
6989 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006990 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006991 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006993 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006994 if (curwin != old_curwin
6995 && win_valid(old_curwin)
6996 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006997 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006998 old_curwin->w_alt_fnum = curbuf->b_fnum;
6999 }
7000 }
7001 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7003 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007004 // Reset 'scrollbind' when editing another file, but keep it when
7005 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007006 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007007 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 else
7009 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 do_exedit(eap, old_curwin);
7011 }
7012
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007013# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007014 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017theend:
7018 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007020
7021/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007022 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007023 */
7024 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007025tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007026{
7027 exarg_T ea;
7028
Bram Moolenaara80faa82020-04-12 19:37:17 +02007029 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007030 ea.cmdidx = CMD_tabnew;
7031 ea.cmd = (char_u *)"tabn";
7032 ea.arg = (char_u *)"";
7033 ex_splitview(&ea);
7034}
7035
7036/*
7037 * :tabnext command
7038 */
7039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007040ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007041{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007042 int tab_number;
7043
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007044 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007045 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007046 switch (eap->cmdidx)
7047 {
7048 case CMD_tabfirst:
7049 case CMD_tabrewind:
7050 goto_tabpage(1);
7051 break;
7052 case CMD_tablast:
7053 goto_tabpage(9999);
7054 break;
7055 case CMD_tabprevious:
7056 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007057 if (eap->arg && *eap->arg != NUL)
7058 {
7059 char_u *p = eap->arg;
7060 char_u *p_save = p;
7061
7062 tab_number = getdigits(&p);
7063 if (p == p_save || *p_save == '-' || *p != NUL
7064 || tab_number == 0)
7065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007066 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007067 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007068 return;
7069 }
7070 }
7071 else
7072 {
7073 if (eap->addr_count == 0)
7074 tab_number = 1;
7075 else
7076 {
7077 tab_number = eap->line2;
7078 if (tab_number < 1)
7079 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007080 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007081 return;
7082 }
7083 }
7084 }
7085 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007086 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007087 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007088 tab_number = get_tabpage_arg(eap);
7089 if (eap->errmsg == NULL)
7090 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007091 break;
7092 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007093}
7094
7095/*
7096 * :tabmove command
7097 */
7098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007099ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007100{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007101 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007102
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007103 tab_number = get_tabpage_arg(eap);
7104 if (eap->errmsg == NULL)
7105 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007106}
7107
7108/*
7109 * :tabs command: List tabs and their contents.
7110 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007112ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007113{
7114 tabpage_T *tp;
7115 win_T *wp;
7116 int tabcount = 1;
7117
7118 msg_start();
7119 msg_scroll = TRUE;
7120 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7121 {
7122 msg_putchar('\n');
7123 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007124 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007125 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007126 ui_breakcheck();
7127
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007128 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007129 wp = firstwin;
7130 else
7131 wp = tp->tp_firstwin;
7132 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7133 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007134 msg_putchar('\n');
7135 msg_putchar(wp == curwin ? '>' : ' ');
7136 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007137 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7138 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007139 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007140 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007141 else
7142 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7143 IObuff, IOSIZE, TRUE);
7144 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007145 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007146 ui_breakcheck();
7147 }
7148 }
7149}
7150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151/*
7152 * ":mode": Set screen mode.
7153 * If no argument given, just get the screen size and redraw.
7154 */
7155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007156ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157{
7158 if (*eap->arg == NUL)
7159 shell_resized();
7160 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007161 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162}
7163
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164/*
7165 * ":resize".
7166 * set, increment or decrement current window height
7167 */
7168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007169ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 int n;
7172 win_T *wp = curwin;
7173
7174 if (eap->addr_count > 0)
7175 {
7176 n = eap->line2;
7177 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7178 ;
7179 }
7180
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007181# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007185 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
7187 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007188 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007189 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007191 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
7193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
7195 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007196 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007197 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007199 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
7201}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
7203/*
7204 * ":find [+command] <file>" command.
7205 */
7206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007207ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
Colin Kennedy21570352024-03-03 16:16:47 +01007209 if (!check_can_set_curbuf_forceit(eap->forceit))
7210 return;
7211
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 char_u *fname;
7213 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007214 char_u *file_to_find = NULL;
7215 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
7217 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007218 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (eap->addr_count > 0)
7220 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007221 // Repeat finding the file "count" times. This matters when it appears
7222 // several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 count = eap->line2;
7224 while (fname != NULL && --count > 0)
7225 {
7226 vim_free(fname);
7227 fname = find_file_in_path(NULL, 0, FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007228 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
7230 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007231 VIM_CLEAR(file_to_find);
7232 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007234 if (fname == NULL)
7235 return;
7236
7237 eap->arg = fname;
7238 do_exedit(eap, NULL);
7239 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240}
7241
7242/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007243 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007244 */
7245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007246ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007247{
7248 regmatch_T regmatch;
7249 char_u *p;
7250
Bram Moolenaar65088802021-03-13 21:07:21 +01007251#ifdef FEAT_EVAL
7252 if (not_in_vim9(eap) == FAIL)
7253 return;
7254#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007255 curwin->w_cursor.lnum = eap->line2;
7256 beginline(BL_SOL | BL_FIX);
7257 if (*eap->arg == '/')
7258 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007259 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007260 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007261 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007262 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007263 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007264 if (regmatch.regprog != NULL)
7265 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007266 // make a copy of the line, when searching for a mark it might be
7267 // flushed
7268 char_u *line = vim_strsave(ml_get_curline());
7269
Bram Moolenaardf177f62005-02-22 08:39:57 +00007270 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007271 if (vim_regexec(&regmatch, line, (colnr_T)0))
7272 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007273 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007274 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007275 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007276 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007277 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007278 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007279 eap->arg += STRLEN(eap->arg);
7280 }
7281 check_cursor();
7282
7283 eap->cmdidx = CMD_visual;
7284 do_exedit(eap, NULL);
7285}
7286
7287/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007288 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 */
7290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007291ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007293 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7294
Colin Kennedy21570352024-03-03 16:16:47 +01007295 // Exclude commands which keep the window's current buffer
7296 if (
7297 eap->cmdidx != CMD_badd
7298 && eap->cmdidx != CMD_balt
7299 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007300 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7301 )
Colin Kennedy21570352024-03-03 16:16:47 +01007302 return;
7303
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 do_exedit(eap, NULL);
7305}
7306
7307/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007308 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007311do_exedit(
7312 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007313 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
7315 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007317 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007319 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7320 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007321 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 /*
7323 * ":vi" command ends Ex mode.
7324 */
7325 if (exmode_active && (eap->cmdidx == CMD_visual
7326 || eap->cmdidx == CMD_view))
7327 {
7328 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007329 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007332 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007333 if (global_busy)
7334 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007335 if (eap->nextcmd != NULL)
7336 {
7337 stuffReadbuff(eap->nextcmd);
7338 eap->nextcmd = NULL;
7339 }
7340
7341 if (exmode_was != EXMODE_VIM)
7342 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007343 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007344 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007345 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007346 no_wait_return = 0;
7347 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007348 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007349 msg_scroll = 0;
7350#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007351 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007352 hold_gui_events = 0;
7353#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007354 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007355 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007356
7357 main_loop(FALSE, TRUE);
7358
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007359 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007360 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007361 no_wait_return = save_nwr;
7362 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007363#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007364 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007365#endif
7366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 }
7370
7371 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007372 || eap->cmdidx == CMD_tabnew
7373 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007374 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007376 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 setpcmark();
7378 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007379 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7380 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007382 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 || *eap->arg != NUL
7384#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007385 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386#endif
7387 )
7388 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007389 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7390 // Only ":edit" or ":script" can bring us here, others are stopped
7391 // earlier.
7392 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007393 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 n = readonlymode;
7396 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7397 readonlymode = TRUE;
7398 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007399 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7400 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007401 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7402 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7404 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007405 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7407 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7408 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007409 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007411 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007412 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007413 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7414 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007415 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007417 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 if (old_curwin != NULL)
7419 {
7420 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007421 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007423#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007424 cleanup_T cs;
7425
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007426 // Reset the error/interrupt/exception state here so that
7427 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007428 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007429#endif
7430#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007432#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007433 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007434
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007435#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007436 // Restore the error/interrupt/exception state if not
7437 // discarded by a new aborting error, interrupt, or
7438 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007439 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 }
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 }
7444 else if (readonlymode && curbuf->b_nwindows == 1)
7445 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007446 // When editing an already visited buffer, 'readonly' won't be set
7447 // but the previous value is kept. With ":view" and ":sview" we
7448 // want the file to be readonly, except when another window is
7449 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 curbuf->b_p_ro = TRUE;
7451 }
7452 readonlymode = n;
7453 }
7454 else
7455 {
7456 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007457 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 if (n != curwin->w_arg_idx_invalid)
7461 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
7463
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 /*
7465 * if ":split file" worked, set alternate file name in old window to new
7466 * file
7467 */
7468 if (old_curwin != NULL
7469 && *eap->arg != NUL
7470 && curwin != old_curwin
7471 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007472 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007473 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476 ex_no_reprint = TRUE;
7477}
7478
7479#ifndef FEAT_GUI
7480/*
7481 * ":gui" and ":gvim" when there is no GUI.
7482 */
7483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007484ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007486 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487}
7488#endif
7489
Bram Moolenaar4f974752019-02-17 17:44:42 +01007490#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007492ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
7494 gui_make_tearoff(eap->arg);
7495}
7496#endif
7497
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007498#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7499 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007501ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007503# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7504 if (gui.in_use)
7505 gui_make_popup(eap->arg, eap->forceit);
7506# ifdef FEAT_TERM_POPUP_MENU
7507 else
7508# endif
7509# endif
7510# ifdef FEAT_TERM_POPUP_MENU
7511 pum_make_popup(eap->arg, eap->forceit);
7512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
7514#endif
7515
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007517ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518{
7519 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007520 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007522 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523}
7524
7525/*
7526 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7527 * offset.
7528 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007531ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007534 win_T *save_curwin = curwin;
7535 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 long topline;
7537 long y;
7538 linenr_T old_linenr = curwin->w_cursor.lnum;
7539
7540 setpcmark();
7541
7542 /*
7543 * determine max topline
7544 */
7545 if (curwin->w_p_scb)
7546 {
7547 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007548 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 {
7550 if (wp->w_p_scb && wp->w_buffer)
7551 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007552 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 if (topline > y)
7554 topline = y;
7555 }
7556 }
7557 if (topline < 1)
7558 topline = 1;
7559 }
7560 else
7561 {
7562 topline = 1;
7563 }
7564
7565
7566 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007567 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007569 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 {
7571 if (curwin->w_p_scb)
7572 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007573 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 y = topline - curwin->w_topline;
7575 if (y > 0)
7576 scrollup(y, TRUE);
7577 else
7578 scrolldown(-y, TRUE);
7579 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007580 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 }
7584 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007585 curwin = save_curwin;
7586 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (curwin->w_p_scb)
7588 {
7589 did_syncbind = TRUE;
7590 checkpcmark();
7591 if (old_linenr != curwin->w_cursor.lnum)
7592 {
7593 char_u ctrl_o[2];
7594
7595 ctrl_o[0] = Ctrl_O;
7596 ctrl_o[1] = 0;
7597 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7598 }
7599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600}
7601
7602
7603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007604ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007606 int i;
7607 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7608 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007610 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007612 do_bang(1, eap, FALSE, FALSE, TRUE);
7613 return;
7614 }
7615
7616 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7617 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618
7619#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007620 if (cmdmod.cmod_flags & CMOD_BROWSE)
7621 {
7622 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007624 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7625 NULL, NULL, NULL, curbuf);
7626 if (browseFile != NULL)
7627 {
7628 i = readfile(browseFile, NULL,
7629 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7630 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007633 i = OK;
7634 }
7635 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007637 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007639 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 return;
7641 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007642 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 }
7644 else
7645 {
7646 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7647 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7648 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007649 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
7651 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007652 if (i != OK)
7653 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007654#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007655 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007657 semsg(_(e_cant_open_file_str), eap->arg);
7658 }
7659 else
7660 {
7661 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007662 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007663 // Delete the empty line that remains. Historically ex does
7664 // this but vi doesn't.
7665 if (eap->line2 == 0)
7666 lnum = curbuf->b_ml.ml_line_count;
7667 else
7668 lnum = 1;
7669 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007670 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007671 ml_delete(lnum);
7672 if (curwin->w_cursor.lnum > 1
7673 && curwin->w_cursor.lnum >= lnum)
7674 --curwin->w_cursor.lnum;
7675 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007676 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007677 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007678 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680}
7681
Bram Moolenaarea408852005-06-25 22:49:46 +00007682static char_u *prev_dir = NULL;
7683
7684#if defined(EXITFREE) || defined(PROTO)
7685 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007686free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007687{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007688 VIM_CLEAR(prev_dir);
7689 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007690}
7691#endif
7692
Bram Moolenaarf4258302013-06-02 18:20:17 +02007693/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007694 * Get the previous directory for the given chdir scope.
7695 */
7696 static char_u *
7697get_prevdir(cdscope_T scope)
7698{
7699 if (scope == CDSCOPE_WINDOW)
7700 return curwin->w_prevdir;
7701 else if (scope == CDSCOPE_TABPAGE)
7702 return curtab->tp_prevdir;
7703 return prev_dir;
7704}
7705
7706/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007707 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007708 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7709 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007710 */
7711 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007712post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007713{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007714 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007715 // Clear tab local directory for both :cd and :tcd
7716 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007717 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007718 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007719 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007720 char_u *pdir = get_prevdir(scope);
7721
7722 // If still in the global directory, need to remember current
7723 // directory as the global directory.
7724 if (globaldir == NULL && pdir != NULL)
7725 globaldir = vim_strsave(pdir);
7726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007727 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007728 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007729 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007730 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007731 curtab->tp_localdir = vim_strsave(NameBuff);
7732 else
7733 curwin->w_localdir = vim_strsave(NameBuff);
7734 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007735 }
7736 else
7737 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007738 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007739 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007740 }
7741
zeertzjq64be6aa2021-11-19 11:59:08 +00007742 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007743 shorten_fnames(TRUE);
7744}
7745
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007746/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007747 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7748 */
7749 void
7750trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7751{
7752#ifdef FEAT_EVAL
7753 dict_T *v_event;
7754 save_v_event_T save_v_event;
7755
7756 v_event = get_v_event(&save_v_event);
7757 (void)dict_add_string(v_event, "directory", new_dir);
7758 dict_set_items_ro(v_event);
7759#endif
7760 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7761#ifdef FEAT_EVAL
7762 restore_v_event(v_event, &save_v_event);
7763#endif
7764}
7765
7766/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007767 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007768 * chdir() function.
7769 * scope == CDSCOPE_WINDOW: changes the window-local directory
7770 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7771 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007772 * Returns TRUE if the directory is successfully changed.
7773 */
7774 int
7775changedir_func(
7776 char_u *new_dir,
7777 int forceit,
7778 cdscope_T scope)
7779{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007780 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007781 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007782 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007783 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007784 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007785
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007786 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007787 return FALSE;
7788
7789 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7790 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007791 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007792 return FALSE;
7793 }
7794
7795 // ":cd -": Change to previous directory
7796 if (STRCMP(new_dir, "-") == 0)
7797 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007798 pdir = get_prevdir(scope);
7799 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007800 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007801 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007802 return FALSE;
7803 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007804 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007805 }
7806
7807 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007808 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007809 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007810 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007811 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007812
Bakudankun29f3a452021-12-11 12:28:08 +00007813 // For UNIX ":cd" means: go to home directory.
7814 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007815#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007816 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007817#else
7818 if (*new_dir == NUL && p_cdh)
7819#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007820 {
7821 // use NameBuff for home directory name
7822# ifdef VMS
7823 char_u *p;
7824
7825 p = mch_getenv((char_u *)"SYS$LOGIN");
7826 if (p == NULL || *p == NUL) // empty is the same as not set
7827 NameBuff[0] = NUL;
7828 else
7829 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7830# else
7831 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7832# endif
7833 new_dir = NameBuff;
7834 }
zeertzjqf38aad82021-12-30 13:45:57 +00007835 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007836 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7837 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007838 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007839 if (scope == CDSCOPE_WINDOW)
7840 acmd_fname = (char_u *)"window";
7841 else if (scope == CDSCOPE_TABPAGE)
7842 acmd_fname = (char_u *)"tabpage";
7843 else
7844 acmd_fname = (char_u *)"global";
7845 trigger_DirChangedPre(acmd_fname, new_dir);
7846
7847 if (vim_chdir(new_dir))
7848 {
7849 emsg(_(e_command_failed));
7850 vim_free(pdir);
7851 return FALSE;
7852 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007853 }
zeertzjq73257142022-02-02 13:16:37 +00007854
7855 if (scope == CDSCOPE_WINDOW)
7856 pp = &curwin->w_prevdir;
7857 else if (scope == CDSCOPE_TABPAGE)
7858 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007859 else
zeertzjq73257142022-02-02 13:16:37 +00007860 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007861 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007862 *pp = pdir;
7863
7864 post_chdir(scope);
7865
7866 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007867 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007868 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007869 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007870}
Bram Moolenaarea408852005-06-25 22:49:46 +00007871
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007873 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007875 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007876ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007878 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879
7880 new_dir = eap->arg;
7881#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007882 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7883 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007885 ex_pwd(NULL);
7886 return;
7887 }
7888#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007889
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007890 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007891
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007892 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7893 scope = CDSCOPE_WINDOW;
7894 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7895 scope = CDSCOPE_TABPAGE;
7896
7897 if (changedir_func(new_dir, eap->forceit, scope))
7898 {
7899 // Echo the new current directory if the command was typed.
7900 if (KeyTyped || p_verbose >= 5)
7901 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 }
7903}
7904
7905/*
7906 * ":pwd".
7907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007909ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
7911 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7912 {
7913#ifdef BACKSLASH_IN_FILENAME
7914 slash_adjust(NameBuff);
7915#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007916 if (p_verbose > 0)
7917 {
7918 char *context = "global";
7919
Bram Moolenaar05268152021-11-18 18:53:45 +00007920 if (last_chdir_reason != NULL)
7921 context = last_chdir_reason;
7922 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007923 context = "window";
7924 else if (curtab->tp_localdir != NULL)
7925 context = "tabpage";
7926 smsg("[%s] %s", context, (char *)NameBuff);
7927 }
7928 else
7929 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 }
7931 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007932 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933}
7934
7935/*
7936 * ":=".
7937 */
7938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007939ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007941 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007942 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943}
7944
7945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007946ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007948 int n;
7949 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
7951 if (cursor_valid())
7952 {
7953 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7954 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007955 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007957
7958 len = eap->line2;
7959 switch (*eap->arg)
7960 {
7961 case 'm': break;
7962 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007963 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007964 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007965
7966 // Hide the cursor if invoked with !
7967 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968}
7969
7970/*
7971 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007972 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 */
7974 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007975do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
Bram Moolenaar52953192019-08-16 10:27:13 +02007977 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007978 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007979# ifdef ELAPSED_FUNC
7980 elapsed_T start_tv;
7981
7982 // Remember at what time we started, so that we know how much longer we
7983 // should wait after waiting for a bit.
7984 ELAPSED_INIT(start_tv);
7985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007987 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007988 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007989 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007990 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007991
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007992 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007993 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007995 wait_now = msec - done > 1000L ? 1000L : msec - done;
7996#ifdef FEAT_TIMERS
7997 {
7998 long due_time = check_due_timer();
7999
8000 if (due_time > 0 && due_time < wait_now)
8001 wait_now = due_time;
8002 }
8003#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008004#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008005 if (has_any_channel() && wait_now > 20L)
8006 wait_now = 20L;
8007#endif
8008#ifdef FEAT_SOUND
8009 if (has_any_sound_callback() && wait_now > 20L)
8010 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008011#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008012 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008013
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008014#ifdef FEAT_JOB_CHANNEL
8015 if (has_any_channel())
8016 ui_breakcheck_force(TRUE);
8017 else
8018#endif
8019 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008020#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008021 // Process the netbeans and clientserver messages that may have been
8022 // received in the call to ui_breakcheck() when the GUI is in use. This
8023 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008024 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008025#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008026
8027# ifdef ELAPSED_FUNC
8028 // actual time passed
8029 done = ELAPSED_FUNC(start_tv);
8030# else
8031 // guestimate time passed (will actually be more)
8032 done += wait_now;
8033# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008035
8036 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8037 // input buffer, otherwise a following call to input() fails.
8038 if (got_int)
8039 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008040
8041 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008042 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043}
8044
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045/*
8046 * ":winsize" command (obsolete).
8047 */
8048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008049ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
8051 int w, h;
8052 char_u *arg = eap->arg;
8053 char_u *p;
8054
Keith Thompson184f71c2024-01-04 21:19:04 +01008055 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008056 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008057 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008058 return;
8059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 w = getdigits(&arg);
8061 arg = skipwhite(arg);
8062 p = arg;
8063 h = getdigits(&arg);
8064 if (*p != NUL && *arg == NUL)
8065 set_shellsize(w, h, TRUE);
8066 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008067 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068}
8069
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 int xchar = NUL;
8074 char_u *p;
8075
8076 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008078 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 if (eap->arg[1] == NUL)
8080 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008081 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 return;
8083 }
8084 xchar = eap->arg[1];
8085 p = eap->arg + 2;
8086 }
8087 else
8088 p = eap->arg + 1;
8089
Bram Moolenaar63b91732021-08-05 20:40:03 +02008090 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008092 if (*p != NUL && *p != (
8093#ifdef FEAT_EVAL
8094 in_vim9script() ? '#' :
8095#endif
8096 '"')
8097 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008098 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008099 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008101 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008102 postponed_split_flags = cmdmod.cmod_split;
8103 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8105 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008106 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109
Bram Moolenaar843ee412004-06-30 16:16:41 +00008110#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111/*
8112 * ":winpos".
8113 */
8114 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008115ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 int x, y;
8118 char_u *arg = eap->arg;
8119 char_u *p;
8120
8121 if (*arg == NUL)
8122 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008123# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008124# ifdef VIMDLL
8125 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8126 mch_get_winpos(&x, &y) != FAIL)
8127# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008129# else
8130 if (mch_get_winpos(&x, &y) != FAIL)
8131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {
8133 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008134 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 }
8136 else
8137# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008138 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 }
8140 else
8141 {
8142 x = getdigits(&arg);
8143 arg = skipwhite(arg);
8144 p = arg;
8145 y = getdigits(&arg);
8146 if (*p == NUL || *arg != NUL)
8147 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008148 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 return;
8150 }
8151# ifdef FEAT_GUI
8152 if (gui.in_use)
8153 gui_mch_set_winpos(x, y);
8154 else if (gui.starting)
8155 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008156 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 gui_win_x = x;
8158 gui_win_y = y;
8159 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008160# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 else
8162# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008163# endif
8164# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008165 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166# endif
8167# ifdef HAVE_TGETENT
8168 if (*T_CWP)
8169 term_set_winpos(x, y);
8170# endif
8171 }
8172}
8173#endif
8174
8175/*
8176 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8177 */
8178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008179ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180{
8181 oparg_T oa;
8182
8183 clear_oparg(&oa);
8184 oa.regname = eap->regname;
8185 oa.start.lnum = eap->line1;
8186 oa.end.lnum = eap->line2;
8187 oa.line_count = eap->line2 - eap->line1 + 1;
8188 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008190 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {
8192 setpcmark();
8193 curwin->w_cursor.lnum = eap->line1;
8194 beginline(BL_SOL | BL_FIX);
8195 }
8196
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008197 if (VIsual_active)
8198 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008199
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 switch (eap->cmdidx)
8201 {
8202 case CMD_delete:
8203 oa.op_type = OP_DELETE;
8204 op_delete(&oa);
8205 break;
8206
8207 case CMD_yank:
8208 oa.op_type = OP_YANK;
8209 (void)op_yank(&oa, FALSE, TRUE);
8210 break;
8211
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008212 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008213 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008215 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8216#else
8217 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008219 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 oa.op_type = OP_RSHIFT;
8221 else
8222 oa.op_type = OP_LSHIFT;
8223 op_shift(&oa, FALSE, eap->amount);
8224 break;
8225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008227 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
8230/*
8231 * ":put".
8232 */
8233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008234ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008236 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if (eap->line2 == 0)
8238 {
8239 eap->line2 = 1;
8240 eap->forceit = TRUE;
8241 }
8242 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008243 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008244 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008245 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246}
8247
8248/*
8249 * Handle ":copy" and ":move".
8250 */
8251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008252ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253{
8254 long n;
8255
Bram Moolenaar491799b2020-08-01 19:23:43 +02008256#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008257 if (not_in_vim9(eap) == FAIL)
8258 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008259#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008260 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008261 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {
8263 eap->nextcmd = NULL;
8264 return;
8265 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008266 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267
8268 /*
8269 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8270 */
8271 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8272 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008273 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 return;
8275 }
8276
8277 if (eap->cmdidx == CMD_move)
8278 {
8279 if (do_move(eap->line1, eap->line2, n) == FAIL)
8280 return;
8281 }
8282 else
8283 ex_copy(eap->line1, eap->line2, n);
8284 u_clearline();
8285 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008286 ex_may_print(eap);
8287}
8288
8289/*
8290 * Print the current line if flags were given to the Ex command.
8291 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008292 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008293ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008294{
8295 if (eap->flags != 0)
8296 {
8297 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8298 (eap->flags & EXFLAG_LIST));
8299 ex_no_reprint = TRUE;
8300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301}
8302
8303/*
8304 * ":smagic" and ":snomagic".
8305 */
8306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008307ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008309 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008311 magic_overruled = eap->cmdidx == CMD_smagic
8312 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008313 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008314 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315}
8316
8317/*
8318 * ":join".
8319 */
8320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008321ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
8323 curwin->w_cursor.lnum = eap->line1;
8324 if (eap->line1 == eap->line2)
8325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008326 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 return;
8328 if (eap->line2 == curbuf->b_ml.ml_line_count)
8329 {
8330 beep_flush();
8331 return;
8332 }
8333 ++eap->line2;
8334 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008335 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008337 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338}
8339
8340/*
8341 * ":[addr]@r" or ":[addr]*r": execute register
8342 */
8343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008344ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
8346 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008347 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
8349 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008350 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351
8352#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354#endif
8355
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008356 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 c = *eap->arg;
8358 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8359 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008360 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008361 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8362 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008365 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008368 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008370 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008372 /*
8373 * Execute from the typeahead buffer.
8374 * Continue until the stuff buffer is empty and all added characters
8375 * have been consumed.
8376 */
8377 while (!stuff_empty() || typebuf.tb_len > prev_len)
8378 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8379
8380 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381}
8382
8383/*
8384 * ":!".
8385 */
8386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008387ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388{
8389 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8390}
8391
8392/*
8393 * ":undo".
8394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008396ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008398 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008399 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008400 else
8401 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402}
8403
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008404#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008406ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008407{
8408 char_u hash[UNDO_HASH_SIZE];
8409
8410 u_compute_hash(hash);
8411 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8412}
8413
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008415ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008416{
8417 char_u hash[UNDO_HASH_SIZE];
8418
8419 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008420 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008421}
8422#endif
8423
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424/*
8425 * ":redo".
8426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008428ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429{
8430 u_redo(1);
8431}
8432
8433/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008434 * ":earlier" and ":later".
8435 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008437ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008438{
8439 long count = 0;
8440 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008441 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008442 char_u *p = eap->arg;
8443
8444 if (*p == NUL)
8445 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008446 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008447 {
8448 count = getdigits(&p);
8449 switch (*p)
8450 {
8451 case 's': ++p; sec = TRUE; break;
8452 case 'm': ++p; sec = TRUE; count *= 60; break;
8453 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008454 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8455 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008456 }
8457 }
8458
8459 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008460 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008461 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008462 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8463 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008464}
8465
8466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 * ":redir": start/stop redirection.
8468 */
8469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008470ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
8472 char *mode;
8473 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008474 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475
Bram Moolenaarba768492016-07-08 15:32:54 +02008476#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008477 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008478 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008479 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008480 return;
8481 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008482#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008483
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 if (STRICMP(eap->arg, "END") == 0)
8485 close_redir();
8486 else
8487 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008488 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008490 ++arg;
8491 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008493 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 mode = "a";
8495 }
8496 else
8497 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008498 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
8500 close_redir();
8501
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008502 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008503 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 if (fname == NULL)
8505 return;
8506#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008507 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {
8509 char_u *browseFile;
8510
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008511 browseFile = do_browse(BROWSE_SAVE,
8512 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008513 fname, NULL, NULL,
8514 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008516 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 vim_free(fname);
8518 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008519 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 }
8521#endif
8522
8523 redir_fd = open_exfile(fname, eap->forceit, mode);
8524 vim_free(fname);
8525 }
8526#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008527 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008531 ++arg;
8532 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008534 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008535 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008537 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008539 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008540 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008541 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008542 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008544 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008545 if (*arg == '>')
8546 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008547 // Make register empty when not using @A-@Z and the
8548 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008549 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008550 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008552 }
8553 if (*arg != NUL)
8554 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008555 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008556 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008557 }
8558 }
8559 else if (*arg == '=' && arg[1] == '>')
8560 {
8561 int append;
8562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008563 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008564 close_redir();
8565 arg += 2;
8566
8567 if (*arg == '>')
8568 {
8569 ++arg;
8570 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 }
8572 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008573 append = FALSE;
8574
8575 if (var_redir_start(skipwhite(arg), append) == OK)
8576 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 }
8578#endif
8579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008580 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008583 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008585
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008586 // Make sure redirection is not off. Can happen for cmdline completion
8587 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008588 if (redir_fd != NULL
8589#ifdef FEAT_EVAL
8590 || redir_reg || redir_vname
8591#endif
8592 )
8593 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594}
8595
8596/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008597 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008599 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008600ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008602 redraw_cmd(eap->forceit);
8603}
8604
8605/*
8606 * ":redraw": force redraw, with clear if "clear" is TRUE.
8607 */
8608 void
8609redraw_cmd(int clear)
8610{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008611 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008613
8614 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008616
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008617 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008619 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 if (need_maketitle)
8621 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008622#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8623# ifdef VIMDLL
8624 if (!gui.in_use)
8625# endif
8626 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008627#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008628 RedrawingDisabled = save_RedrawingDisabled;
8629 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630
Bram Moolenaar5017c662022-04-07 18:06:08 +01008631 // After drawing the statusline screen_attr may still be set.
8632 screen_stop_highlight();
8633
Bram Moolenaara2a89732022-08-31 14:46:18 +01008634 // Reset msg_didout, so that a message that's there is overwritten.
8635 msg_didout = FALSE;
8636 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008638 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008639 need_wait_return = FALSE;
8640
Bram Moolenaara653e532022-04-19 11:38:24 +01008641 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008642 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008643 redrawcmdline();
8644
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 out_flush();
8646}
8647
8648/*
8649 * ":redrawstatus": force redraw of status line(s)
8650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008652ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 if (eap->forceit)
8655 status_redraw_all();
8656 else
8657 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008658 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008659 return; // redraw later
8660
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008661 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008662 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008663
8664 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008665 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008666
zeertzjq320d9102022-09-20 17:12:13 +01008667 if (State & MODE_CMDLINE)
8668 redraw_statuslines();
8669 else
8670 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008671 RedrawingDisabled = save_RedrawingDisabled;
8672 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674}
8675
Bram Moolenaare12bab32019-01-08 22:02:56 +01008676/*
8677 * ":redrawtabline": force redraw of the tabline
8678 */
8679 static void
8680ex_redrawtabline(exarg_T *eap UNUSED)
8681{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008682 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008683 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008684
8685 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008686 p_lz = FALSE;
8687
8688 draw_tabline();
8689
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008690 RedrawingDisabled = save_RedrawingDisabled;
8691 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008692 out_flush();
8693}
8694
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008696close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
8698 if (redir_fd != NULL)
8699 {
8700 fclose(redir_fd);
8701 redir_fd = NULL;
8702 }
8703#ifdef FEAT_EVAL
8704 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008705 if (redir_vname)
8706 {
8707 var_redir_stop();
8708 redir_vname = 0;
8709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710#endif
8711}
8712
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008713#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008714 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008715vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008716{
8717 if (vim_mkdir(name, prot) != 0)
8718 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008719 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008720 return FAIL;
8721 }
8722 return OK;
8723}
8724#endif
8725
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726/*
8727 * Open a file for writing for an Ex command, with some checks.
8728 * Return file descriptor, or NULL on failure.
8729 */
8730 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008731open_exfile(
8732 char_u *fname,
8733 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008734 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735{
8736 FILE *fd;
8737
8738#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008739 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 if (mch_isdir(fname))
8741 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008742 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 return NULL;
8744 }
8745#endif
8746 if (!forceit && *mode != 'a' && vim_fexists(fname))
8747 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008748 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 return NULL;
8750 }
8751
8752 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008753 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754
8755 return fd;
8756}
8757
8758/*
8759 * ":mark" and ":k".
8760 */
8761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008762ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
8764 pos_T pos;
8765
Bram Moolenaar10b94212021-02-19 21:42:57 +01008766#ifdef FEAT_EVAL
8767 if (not_in_vim9(eap) == FAIL)
8768 return;
8769#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008770 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008772 emsg(_(e_argument_required));
8773 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008775
8776 if (eap->arg[1] != NUL) // more than one character?
8777 {
8778 semsg(_(e_trailing_characters_str), eap->arg);
8779 return;
8780 }
8781
8782 pos = curwin->w_cursor; // save curwin->w_cursor
8783 curwin->w_cursor.lnum = eap->line2;
8784 beginline(BL_WHITE | BL_FIX);
8785 if (setmark(*eap->arg) == FAIL) // set mark
8786 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8787 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
8789
8790/*
8791 * Update w_topline, w_leftcol and the cursor position.
8792 */
8793 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008794update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008796 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 update_topline();
8798 if (!curwin->w_p_wrap)
8799 validate_cursor();
8800 update_curswant();
8801}
8802
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008804 * Save the current State and go to Normal mode.
8805 * Return TRUE if the typeahead could be saved.
8806 */
8807 int
8808save_current_state(save_state_T *sst)
8809{
8810 sst->save_msg_scroll = msg_scroll;
8811 sst->save_restart_edit = restart_edit;
8812 sst->save_msg_didout = msg_didout;
8813 sst->save_State = State;
8814 sst->save_insertmode = p_im;
8815 sst->save_finish_op = finish_op;
8816 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008817 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008818 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008819
Bram Moolenaar4324d872020-12-01 20:12:24 +01008820 msg_scroll = FALSE; // no msg scrolling in Normal mode
8821 restart_edit = 0; // don't go to Insert mode
8822 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008823
Bram Moolenaara452b802020-12-01 21:47:59 +01008824 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008825 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008826
8827 /*
8828 * Save the current typeahead. This is required to allow using ":normal"
8829 * from an event handler and makes sure we don't hang when the argument
8830 * ends with half a command.
8831 */
8832 save_typeahead(&sst->tabuf);
8833 return sst->tabuf.typebuf_valid;
8834}
8835
8836 void
8837restore_current_state(save_state_T *sst)
8838{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008839 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008840 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008841
8842 msg_scroll = sst->save_msg_scroll;
8843 restart_edit = sst->save_restart_edit;
8844 p_im = sst->save_insertmode;
8845 finish_op = sst->save_finish_op;
8846 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008847 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008848 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008849 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008850 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008851
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008852 // Restore the state (needed when called from a function executed for
8853 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008854 State = sst->save_State;
8855#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008856 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008857#endif
8858}
8859
8860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 * ":normal[!] {commands}": Execute normal mode commands.
8862 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008863 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008864ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008866 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 char_u *arg = NULL;
8868 int l;
8869 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008871 if (ex_normal_lock > 0)
8872 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008873 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008874 return;
8875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 if (ex_normal_busy >= p_mmd)
8877 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008878 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 return;
8880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 /*
8883 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8884 * this for the K_SPECIAL leading byte, otherwise special keys will not
8885 * work.
8886 */
8887 if (has_mbyte)
8888 {
8889 int len = 0;
8890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008891 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 for (p = eap->arg; *p != NUL; ++p)
8893 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008894#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008895 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008897#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008898 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008899 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008900#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 )
8904 len += 2;
8905 }
8906 if (len > 0)
8907 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008908 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 if (arg != NULL)
8910 {
8911 len = 0;
8912 for (p = eap->arg; *p != NUL; ++p)
8913 {
8914 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008915#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 if (*p == CSI)
8917 {
8918 arg[len++] = KS_EXTRA;
8919 arg[len++] = (int)KE_CSI;
8920 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008921#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008922 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 {
8924 arg[len++] = *++p;
8925 if (*p == K_SPECIAL)
8926 {
8927 arg[len++] = KS_SPECIAL;
8928 arg[len++] = KE_FILLER;
8929 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008930#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 else if (*p == CSI)
8932 {
8933 arg[len++] = KS_EXTRA;
8934 arg[len++] = (int)KE_CSI;
8935 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 }
8938 arg[len] = NUL;
8939 }
8940 }
8941 }
8942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008944 ++ex_normal_busy;
8945 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 {
8947 /*
8948 * Repeat the :normal command for each line in the range. When no
8949 * range given, execute it just once, without positioning the cursor
8950 * first.
8951 */
8952 do
8953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 if (eap->addr_count != 0)
8955 {
8956 curwin->w_cursor.lnum = eap->line1++;
8957 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008958 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 }
8960
Bram Moolenaar13505972019-01-24 15:04:48 +01008961 exec_normal_cmd(arg != NULL
8962 ? arg
8963 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 }
8965 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8966 }
8967
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008968 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 update_topline_cursor();
8970
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008971 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008973 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008974#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008975 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008976#endif
8977
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979}
8980
8981/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008982 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 */
8984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008985ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008987 if (eap->forceit)
8988 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008989 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008990 if (!curwin->w_cursor.lnum)
8991 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008992 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008993 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008994#ifdef FEAT_TERMINAL
8995 // Ignore this when running in an active terminal.
8996 if (term_job_running(curbuf->b_term))
8997 return;
8998#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008999
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009000 // Ignore the command when already in Insert mode. Inserting an
9001 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009002 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009003 return;
9004
Bram Moolenaar64969662005-12-14 21:59:55 +00009005 if (eap->cmdidx == CMD_startinsert)
9006 restart_edit = 'a';
9007 else if (eap->cmdidx == CMD_startreplace)
9008 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009010 restart_edit = 'V';
9011
9012 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009014 if (eap->cmdidx == CMD_startinsert)
9015 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009016 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009018
9019 if (VIsual_active)
9020 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021}
9022
9023/*
9024 * ":stopinsert"
9025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009027ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
9029 restart_edit = 0;
9030 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009031 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009034/*
9035 * Execute normal mode command "cmd".
9036 * "remap" can be REMAP_NONE or REMAP_YES.
9037 */
9038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009039exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009040{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009041 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009042 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009043 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009044}
Bram Moolenaar25281632016-01-21 23:32:32 +01009045
Bram Moolenaar25281632016-01-21 23:32:32 +01009046/*
9047 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009048 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009049 */
9050 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009051exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009052{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009053 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009054 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009055
Bram Moolenaar905dd902019-04-07 14:21:47 +02009056 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9057 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009058 clear_oparg(&oa);
9059 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009060 while ((!stuff_empty()
9061 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009062 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009063 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009064 {
9065 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009066#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009067 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009068 && oa.op_type == OP_NOP && oa.regname == NUL
9069 && !VIsual_active)
9070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009071 // If terminal_loop() returns OK we got a key that is handled
9072 // in Normal model. With FAIL we first need to position the
9073 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009074 if (terminal_loop(TRUE) == OK)
9075 normal_cmd(&oa, TRUE);
9076 }
9077 else
9078#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009079 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009080 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009081 }
9082}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009083
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084#ifdef FEAT_FIND_ID
9085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009086ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087{
9088 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9089 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009090 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091}
9092
Bram Moolenaar4033c552017-09-16 20:54:51 +02009093#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094/*
9095 * ":psearch"
9096 */
9097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009098ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099{
9100 g_do_tagpreview = p_pvh;
9101 ex_findpat(eap);
9102 g_do_tagpreview = 0;
9103}
9104#endif
9105
9106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009107ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109 int whole = TRUE;
9110 long n;
9111 char_u *p;
9112 int action;
9113
9114 switch (cmdnames[eap->cmdidx].cmd_name[2])
9115 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009116 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9118 action = ACTION_GOTO;
9119 else
9120 action = ACTION_SHOW;
9121 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009122 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 action = ACTION_SHOW_ALL;
9124 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009125 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 action = ACTION_GOTO;
9127 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009128 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 action = ACTION_SPLIT;
9130 break;
9131 }
9132
9133 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009134 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 {
9136 n = getdigits(&eap->arg);
9137 eap->arg = skipwhite(eap->arg);
9138 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009139 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 {
9141 whole = FALSE;
9142 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009143 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 if (*p)
9145 {
9146 *p++ = NUL;
9147 p = skipwhite(p);
9148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009149 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009150 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009151 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009153 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 }
9155 }
9156 if (!eap->skip)
9157 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9158 whole, !eap->forceit,
9159 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009160 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161}
9162#endif
9163
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164
Bram Moolenaar4033c552017-09-16 20:54:51 +02009165#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166/*
9167 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9168 */
9169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009170ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009172 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9174}
9175
9176/*
9177 * ":pedit"
9178 */
9179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009180ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182 win_T *curwin_save = curwin;
9183
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009184 if (ERROR_IF_ANY_POPUP_WINDOW)
9185 return;
9186
Bram Moolenaar026587b2019-08-17 15:08:00 +02009187 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009189 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009190
Bram Moolenaar026587b2019-08-17 15:08:00 +02009191 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009193
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 if (curwin != curwin_save && win_valid(curwin_save))
9195 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009196 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009198 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 win_enter(curwin_save, TRUE);
9200 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009201# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009202 else if (WIN_IS_POPUP(curwin))
9203 {
9204 // can't keep focus in popup window
9205 win_enter(firstwin, TRUE);
9206 }
9207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 g_do_tagpreview = 0;
9209}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211
9212/*
9213 * ":stag", ":stselect" and ":stjump".
9214 */
9215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009216ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217{
9218 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009219 postponed_split_flags = cmdmod.cmod_split;
9220 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9222 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009223 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225
9226/*
9227 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9228 */
9229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009230ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
9232 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9233}
9234
9235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009236ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
9238 int cmd;
9239
9240 switch (name[1])
9241 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009242 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009244 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009246 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009248 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009250 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009252 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009254 case 'f': // ":tfirst"
9255 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009257 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009259 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009261 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009263 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 return;
9265 }
9266#endif
9267 cmd = DT_TAG;
9268 break;
9269 }
9270
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009271 if (name[0] == 'l')
9272 {
9273#ifndef FEAT_QUICKFIX
9274 ex_ni(eap);
9275 return;
9276#else
9277 cmd = DT_LTAG;
9278#endif
9279 }
9280
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9282 eap->forceit, TRUE);
9283}
9284
9285/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009286 * Check "str" for starting with a special cmdline variable.
9287 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9288 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9289 */
9290 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009291find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009292{
9293 int len;
9294 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009295 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009296 "%",
9297#define SPEC_PERC 0
9298 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009299#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009300 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02009301#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009302 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02009303#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009304 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02009305#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009306 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009307#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009308 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02009309#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009310 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02009311#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02009312 "<stack>", // call stack
9313#define SPEC_STACK (SPEC_SLNUM + 1)
LemonBoy6013d002022-04-09 21:42:10 +01009314 "<script>", // script file name
9315#define SPEC_SCRIPT (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009316 "<afile>", // autocommand file name
LemonBoy6013d002022-04-09 21:42:10 +01009317#define SPEC_AFILE (SPEC_SCRIPT + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009318 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009319#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009320 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009321#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009322 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009323#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02009324 "<SID>", // script ID: <SNR>123_
9325#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009326#ifdef FEAT_CLIENTSERVER
9327 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02009328# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009329#endif
9330 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009331
K.Takataeeec2542021-06-02 13:28:16 +02009332 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009333 {
9334 len = (int)STRLEN(spec_str[i]);
9335 if (STRNCMP(src, spec_str[i], len) == 0)
9336 {
9337 *usedlen = len;
9338 return i;
9339 }
9340 }
9341 return -1;
9342}
9343
9344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 * Evaluate cmdline variables.
9346 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009347 * change "%" to curbuf->b_ffname
9348 * "#" to curwin->w_alt_fnum
9349 * "%%" to curwin->w_alt_fnum in Vim9 script
9350 * "<cword>" to word under the cursor
9351 * "<cWORD>" to WORD under the cursor
9352 * "<cexpr>" to C-expression under the cursor
9353 * "<cfile>" to path name under the cursor
9354 * "<sfile>" to sourced file name
9355 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009356 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009357 * "<slnum>" to sourced file line number
9358 * "<afile>" to file name for autocommand
9359 * "<abuf>" to buffer number for autocommand
9360 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 *
9362 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9363 * "" for error without a message) and NULL is returned.
9364 * Returns an allocated string if a valid match was found.
9365 * Returns NULL if no match was found. "usedlen" then still contains the
9366 * number of characters to skip.
9367 */
9368 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009369eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009370 char_u *src, // pointer into commandline
9371 char_u *srcstart, // beginning of valid memory for src
9372 int *usedlen, // characters after src that are used
9373 linenr_T *lnump, // line number for :e command, or NULL
9374 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009375 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009376 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009377 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 int i;
9380 char_u *s;
9381 char_u *result;
9382 char_u *resultbuf = NULL;
9383 int resultlen;
9384 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009385 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009387 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390
9391 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009392 if (escaped != NULL)
9393 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394
9395 /*
9396 * Check if there is something to do.
9397 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009398 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009399 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 {
9401 *usedlen = 1;
9402 return NULL;
9403 }
9404
9405 /*
9406 * Skip when preceded with a backslash "\%" and "\#".
9407 * Note: In "\\%" the % is also not recognized!
9408 */
9409 if (src > srcstart && src[-1] == '\\')
9410 {
9411 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009412 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 return NULL;
9414 }
9415
9416 /*
9417 * word or WORD under cursor
9418 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009419 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9420 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009422 resultlen = find_ident_under_cursor(&result,
9423 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9424 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9425 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 if (resultlen == 0)
9427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009428 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 return NULL;
9430 }
9431 }
9432
9433 /*
9434 * '#': Alternate file name
9435 * '%': Current file name
9436 * File name under the cursor
9437 * File name for autocommand
9438 * and following modifiers
9439 */
9440 else
9441 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009442 int off = 0;
9443
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 switch (spec_idx)
9445 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009446 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009447#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009448 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009451 // '%': current file
9452 if (curbuf->b_fname == NULL)
9453 {
9454 result = (char_u *)"";
9455 valid = 0; // Must have ":p:h" to be valid
9456 }
9457 else
9458 {
9459 result = curbuf->b_fname;
9460 tilde_file = STRCMP(result, "~") == 0;
9461 }
9462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009464#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009465 // "%%" alternate file
9466 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009467#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009468 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009469 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009470 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009472 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 result = arg_all();
9474 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009475 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009476 if (escaped != NULL)
9477 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 break;
9480 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009481 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009482 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009483 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009485 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009486 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009487 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009488 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009490 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009492 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009493 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009494 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009495 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009496 return NULL;
9497 }
9498#ifdef FEAT_EVAL
9499 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9500 (long)i);
9501 if (result == NULL)
9502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009503 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009504 return NULL;
9505 }
9506#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009507 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 }
9511 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009512 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009513 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9514 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009515 buf = buflist_findnr(i);
9516 if (buf == NULL)
9517 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009518 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009519 return NULL;
9520 }
9521 if (lnump != NULL)
9522 *lnump = ECMD_LAST;
9523 if (buf->b_fname == NULL)
9524 {
9525 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009526 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009527 }
9528 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009529 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009530 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009531 tilde_file = STRCMP(result, "~") == 0;
9532 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 break;
9535
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009536 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009537 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 if (result == NULL)
9539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009540 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 return NULL;
9542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009543 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009546 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009548 if (result != NULL && !autocmd_fname_full)
9549 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009550 // Still need to turn the fname into a full path. It is
9551 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009552 autocmd_fname_full = TRUE;
9553 result = FullName_save(autocmd_fname, FALSE);
9554 vim_free(autocmd_fname);
9555 autocmd_fname = result;
9556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 if (result == NULL)
9558 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009559 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 return NULL;
9561 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009562 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 break;
9564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009565 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 if (autocmd_bufnr <= 0)
9567 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009568 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 return NULL;
9570 }
9571 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9572 result = strbuf;
9573 break;
9574
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009575 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 result = autocmd_match;
9577 if (result == NULL)
9578 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009579 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 return NULL;
9581 }
9582 break;
9583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009584 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009585 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (result == NULL)
9587 {
LemonBoy6013d002022-04-09 21:42:10 +01009588 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9589 return NULL;
9590 }
9591 resultbuf = result; // remember allocated string
9592 break;
9593 case SPEC_STACK: // call stack
9594 result = estack_sfile(ESTACK_STACK);
9595 if (result == NULL)
9596 {
9597 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9598 return NULL;
9599 }
9600 resultbuf = result; // remember allocated string
9601 break;
9602 case SPEC_SCRIPT: // script file name
9603 result = estack_sfile(ESTACK_SCRIPT);
9604 if (result == NULL)
9605 {
9606 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 return NULL;
9608 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009609 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009612 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009613 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009614 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009615 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009616 return NULL;
9617 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009618 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009619 result = strbuf;
9620 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009621
9622#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009623 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009624 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009625 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009626 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009627 return NULL;
9628 }
9629 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009630 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009631 result = strbuf;
9632 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009633
Bram Moolenaar90944302020-08-01 20:45:11 +02009634 case SPEC_SID:
9635 if (current_sctx.sc_sid <= 0)
9636 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009637 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009638 return NULL;
9639 }
9640 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9641 result = strbuf;
9642 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009643#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009644
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009645#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009646 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009647 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9648 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 result = strbuf;
9650 break;
9651#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009652
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009653 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009654 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009655 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 }
9657
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009658 resultlen = (int)STRLEN(result); // length of new string
9659 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 {
9661 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 resultlen = (int)(s - result);
9664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 else if (!skip_mod)
9666 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009667 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 &resultlen);
9669 if (result == NULL)
9670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009671 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 return NULL;
9673 }
9674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 }
9676
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009677 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009679 if (empty_is_error)
9680 {
9681 if (valid != VALID_HEAD + VALID_PATH)
9682 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9683 else
9684 *errormsg = _(e_evaluates_to_an_empty_string);
9685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 result = NULL;
9687 }
9688 else
9689 result = vim_strnsave(result, resultlen);
9690 vim_free(resultbuf);
9691 return result;
9692}
9693
9694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 * Expand the <sfile> string in "arg".
9696 *
9697 * Returns an allocated string, or NULL for any error.
9698 */
9699 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009700expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009702 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 int len;
9704 char_u *result;
9705 char_u *newres;
9706 char_u *repl;
9707 int srclen;
9708 char_u *p;
9709
9710 result = vim_strsave(arg);
9711 if (result == NULL)
9712 return NULL;
9713
9714 for (p = result; *p; )
9715 {
9716 if (STRNCMP(p, "<sfile>", 7) != 0)
9717 ++p;
9718 else
9719 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009720 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009721 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 if (errormsg != NULL)
9723 {
9724 if (*errormsg)
9725 emsg(errormsg);
9726 vim_free(result);
9727 return NULL;
9728 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009729 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 {
9731 p += srclen;
9732 continue;
9733 }
9734 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9735 newres = alloc(len);
9736 if (newres == NULL)
9737 {
9738 vim_free(repl);
9739 vim_free(result);
9740 return NULL;
9741 }
9742 mch_memmove(newres, result, (size_t)(p - result));
9743 STRCPY(newres + (p - result), repl);
9744 len = (int)STRLEN(newres);
9745 STRCAT(newres, p + srclen);
9746 vim_free(repl);
9747 vim_free(result);
9748 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009749 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 }
9751 }
9752
9753 return result;
9754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009757/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009758 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009759 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009762dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 if (fname == NULL)
9765 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009766 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767}
9768#endif
9769
9770/*
9771 * ":behave {mswin,xterm}"
9772 */
9773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009774ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
9776 if (STRCMP(eap->arg, "mswin") == 0)
9777 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009778 set_option_value_give_err((char_u *)"selection",
9779 0L, (char_u *)"exclusive", 0);
9780 set_option_value_give_err((char_u *)"selectmode",
9781 0L, (char_u *)"mouse,key", 0);
9782 set_option_value_give_err((char_u *)"mousemodel",
9783 0L, (char_u *)"popup", 0);
9784 set_option_value_give_err((char_u *)"keymodel",
9785 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 }
9787 else if (STRCMP(eap->arg, "xterm") == 0)
9788 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009789 set_option_value_give_err((char_u *)"selection",
9790 0L, (char_u *)"inclusive", 0);
9791 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9792 set_option_value_give_err((char_u *)"mousemodel",
9793 0L, (char_u *)"extend", 0);
9794 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 }
9796 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009797 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798}
9799
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800static int filetype_detect = FALSE;
9801static int filetype_plugin = FALSE;
9802static int filetype_indent = FALSE;
9803
9804/*
9805 * ":filetype [plugin] [indent] {on,off,detect}"
9806 * on: Load the filetype.vim file to install autocommands for file types.
9807 * off: Load the ftoff.vim file to remove all autocommands for file types.
9808 * plugin on: load filetype.vim and ftplugin.vim
9809 * plugin off: load ftplugof.vim
9810 * indent on: load filetype.vim and indent.vim
9811 * indent off: load indoff.vim
9812 */
9813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009814ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
9816 char_u *arg = eap->arg;
9817 int plugin = FALSE;
9818 int indent = FALSE;
9819
9820 if (*eap->arg == NUL)
9821 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009822 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009823 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 filetype_detect ? "ON" : "OFF",
9825 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9826 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9827 return;
9828 }
9829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009830 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 for (;;)
9832 {
9833 if (STRNCMP(arg, "plugin", 6) == 0)
9834 {
9835 plugin = TRUE;
9836 arg = skipwhite(arg + 6);
9837 continue;
9838 }
9839 if (STRNCMP(arg, "indent", 6) == 0)
9840 {
9841 indent = TRUE;
9842 arg = skipwhite(arg + 6);
9843 continue;
9844 }
9845 break;
9846 }
9847 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9848 {
9849 if (*arg == 'o' || !filetype_detect)
9850 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009851 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 filetype_detect = TRUE;
9853 if (plugin)
9854 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009855 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 filetype_plugin = TRUE;
9857 }
9858 if (indent)
9859 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009860 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 filetype_indent = TRUE;
9862 }
9863 }
9864 if (*arg == 'd')
9865 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009866 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009867 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 }
9869 }
9870 else if (STRCMP(arg, "off") == 0)
9871 {
9872 if (plugin || indent)
9873 {
9874 if (plugin)
9875 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009876 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 filetype_plugin = FALSE;
9878 }
9879 if (indent)
9880 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009881 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 filetype_indent = FALSE;
9883 }
9884 }
9885 else
9886 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009887 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 filetype_detect = FALSE;
9889 }
9890 }
9891 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009892 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893}
9894
9895/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009896 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 */
9898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009899ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009901 if (did_filetype)
9902 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009903
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009904 char_u *arg = eap->arg;
9905 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9906 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009907
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009908 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9909 if (arg != eap->arg)
9910 did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009914ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916#ifdef FEAT_DIGRAPHS
9917 if (*eap->arg != NUL)
9918 putdigraph(eap->arg);
9919 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009920 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921#else
mityu61065042021-07-19 20:07:21 +02009922 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923#endif
9924}
9925
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009926#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9927 void
9928set_no_hlsearch(int flag)
9929{
9930 no_hlsearch = flag;
9931# ifdef FEAT_EVAL
9932 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9933# endif
9934}
9935
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936/*
9937 * ":nohlsearch"
9938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009940ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009942 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009943 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945#endif
9946
9947#ifdef FEAT_CRYPT
9948/*
9949 * ":X": Get crypt key
9950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009952ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009954 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009955 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956}
9957#endif
9958
9959#ifdef FEAT_FOLDING
9960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009961ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962{
9963 if (foldManualAllowed(TRUE))
9964 foldCreate(eap->line1, eap->line2);
9965}
9966
9967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009968ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969{
9970 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9971 eap->forceit, FALSE);
9972}
9973
9974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009975ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976{
9977 linenr_T lnum;
9978
Bram Moolenaar4facea32019-10-12 20:17:40 +02009979# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009980 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009981# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009982
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009983 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9985 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9986 ml_setmarked(lnum);
9987
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009988 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009990 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009991# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009992 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994}
9995#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009997#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009998/*
9999 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10000 * instead of a quickfix command.
10001 */
10002 int
10003is_loclist_cmd(int cmdidx)
10004{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010005 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010006 return FALSE;
10007 return cmdnames[cmdidx].cmd_name[0] == 'l';
10008}
10009#endif
10010
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010011 int
10012get_pressedreturn(void)
10013{
10014 return ex_pressedreturn;
10015}
10016
10017 void
10018set_pressedreturn(int val)
10019{
10020 ex_pressedreturn = val;
10021}