blob: a24b4efbce8b3e97810373a7c7c2eb967de1b5b0 [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
John Marriotted908f72024-04-18 22:46:56 +020085static char_u *repl_cmdline(exarg_T *eap, char_u *src, size_t srclen, char_u *repl, char_u **cmdlinep);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static 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'
LemonBoy749ba0f2024-07-04 19:23:16 +02001459
1460 // If "silent!" is active the uncaught exception is not fatal.
1461 if (emsg_silent == 0)
1462 {
1463 suppress_errthrow = TRUE;
1464 force_abort = TRUE;
1465 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001466
1467 if (messages != NULL)
1468 {
1469 do
1470 {
1471 msglist_T *next = messages->next;
1472 int save_compiling = estack_compiling;
1473
1474 estack_compiling = messages->msg_compiling;
1475 emsg(messages->msg);
1476 vim_free(messages->msg);
1477 vim_free(messages->sfile);
1478 vim_free(messages);
1479 messages = next;
1480 estack_compiling = save_compiling;
1481 }
1482 while (messages != NULL);
1483 }
1484 else if (p != NULL)
1485 {
1486 emsg(p);
1487 vim_free(p);
1488 }
1489 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001490 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001491 estack_pop();
1492}
1493
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 */
1497 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001498get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 wcmd_T *wp;
1502 char_u *line;
1503
1504 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1505 {
1506 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001507 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001509 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001510 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001511 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001513 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001514 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 ++cp->current_line;
1516
1517 return line;
1518 }
1519
1520 KeyTyped = FALSE;
1521 ++cp->current_line;
1522 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001523 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 return vim_strsave(wp->line);
1525}
1526
1527/*
1528 * Store a line in "gap" so that a ":while" loop can execute it again.
1529 */
1530 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001531store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 if (ga_grow(gap, 1) == FAIL)
1534 return FAIL;
1535 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001536 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 return OK;
1539}
1540
1541/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001542 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 */
1544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001545free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 while (gap->ga_len > 0)
1548 {
1549 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1550 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 }
1552}
1553#endif
1554
1555/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001556 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1557 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001560getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001561 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001562 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001563 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001566 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001569 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1570 // function that's originally used to obtain the lines. This may be
1571 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001572 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 cp = (struct loop_cookie *)cookie;
1574 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001576 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 cp = cp->cookie;
1578 }
1579 return gp == func;
1580#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001581 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582#endif
1583}
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 * getline function. Otherwise return "cookie".
1588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001590getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001591 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001592 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
Bram Moolenaar13505972019-01-24 15:04:48 +01001594#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001595 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001596 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001598 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1599 // cookie that's originally used to obtain the lines. This may be nested
1600 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001601 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001602 cp = (struct loop_cookie *)cookie;
1603 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001605 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 cp = cp->cookie;
1607 }
1608 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001609#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001614#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001615/*
1616 * Get the next line source line without advancing.
1617 */
1618 char_u *
1619getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001620 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001621 void *cookie) // argument for fgetline()
1622{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001623 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001624 struct loop_cookie *cp;
1625 wcmd_T *wp;
1626
1627 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1628 // cookie that's originally used to obtain the lines. This may be nested
1629 // several levels.
1630 gp = fgetline;
1631 cp = (struct loop_cookie *)cookie;
1632 while (gp == get_loop_line)
1633 {
1634 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1635 {
1636 // executing lines a second time, use the stored copy
1637 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1638 return wp->line;
1639 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001640 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001641 cp = cp->cookie;
1642 }
1643 if (gp == getsourceline)
1644 return source_nextline(cp);
1645 return NULL;
1646}
1647#endif
1648
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649
1650/*
1651 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1652 * ":bwipeout", etc.
1653 * Returns the buffer number.
1654 */
1655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001656compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001657{
1658 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001659 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001660 int count = offset;
1661
1662 buf = firstbuf;
1663 while (buf->b_next != NULL && buf->b_fnum < lnum)
1664 buf = buf->b_next;
1665 while (count != 0)
1666 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001667 count += (offset < 0) ? 1 : -1;
1668 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1669 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001670 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001671 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001672 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001673 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001674 while (buf->b_ml.ml_mfp == NULL)
1675 {
1676 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1677 if (nextbuf == NULL)
1678 break;
1679 buf = nextbuf;
1680 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001681 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001682 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001683 if (addr_type == ADDR_LOADED_BUFFERS)
1684 while (buf->b_ml.ml_mfp == NULL)
1685 {
1686 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1687 if (nextbuf == NULL)
1688 break;
1689 buf = nextbuf;
1690 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001691 return buf->b_fnum;
1692}
1693
Bram Moolenaarb7316892019-05-01 18:08:42 +02001694/*
1695 * Return the window number of "win".
1696 * When "win" is NULL return the number of windows.
1697 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001698 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001699current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001700{
1701 win_T *wp;
1702 int nr = 0;
1703
Bram Moolenaar29323592016-07-24 22:04:11 +02001704 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001705 {
1706 ++nr;
1707 if (wp == win)
1708 break;
1709 }
1710 return nr;
1711}
1712
1713 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001714current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001715{
1716 tabpage_T *tp;
1717 int nr = 0;
1718
Bram Moolenaar29323592016-07-24 22:04:11 +02001719 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001720 {
1721 ++nr;
1722 if (tp == tab)
1723 break;
1724 }
1725 return nr;
1726}
1727
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001728 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001729comment_start(char_u *p, int starts_with_colon UNUSED)
1730{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001731 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001732 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001733 return *p == '"';
1734}
1735
Bram Moolenaarf240e182014-11-27 18:33:02 +01001736# define CURRENT_WIN_NR current_win_nr(curwin)
1737# define LAST_WIN_NR current_win_nr(NULL)
1738# define CURRENT_TAB_NR current_tab_nr(curtab)
1739# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741/*
1742 * Execute one Ex command.
1743 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001744 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1745 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 *
1747 * 1. skip comment lines and leading space
1748 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001749 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001750 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001751 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001752 * 6. parse arguments
1753 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001755 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 *
1757 * This function may be called recursively!
1758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001760do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001761 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001762 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001764 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001766 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001767 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001769 char_u *p;
1770 linenr_T lnum;
1771 long n;
1772 char *errormsg = NULL; // error message
1773 char_u *after_modifier = NULL;
1774 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001775 cmdmod_T save_cmdmod;
1776 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001777 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001778 int ni; // set when Not Implemented
1779 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001780 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001781 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001782#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001783 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001784#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001785 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001786 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaara80faa82020-04-12 19:37:17 +02001788 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 ea.line1 = 1;
1790 ea.line2 = 1;
1791#ifdef FEAT_EVAL
1792 ++ex_nesting_level;
1793#endif
1794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001795 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (quitmore
1797#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001798 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001799 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001800#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001801 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001802 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 --quitmore;
1804
1805 /*
1806 * Reset browse, confirm, etc.. They are restored when returning, for
1807 * recursive calls.
1808 */
1809 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001811 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001812 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1813 goto doend;
1814
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001815/*
1816 * 1. Skip comment lines and leading white space and colons.
1817 * 2. Handle command modifiers.
1818 */
1819 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001821 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001822 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001823 ea.cookie = cookie;
1824#ifdef FEAT_EVAL
1825 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001826 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001828 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001829 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001830 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001831 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833#ifdef FEAT_EVAL
1834 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1835 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1836#else
1837 ea.skip = (if_level > 0);
1838#endif
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001841 * 3. Skip over the range to find the command. Let "p" point to after it.
1842 *
1843 * We need the command to know what kind of range it uses.
1844 */
1845 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001846
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001847 // In Vim9 script a colon is required before the range. This may also be
1848 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001849 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001850 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001851 {
1852 may_have_range = FALSE;
1853 for (p = ea.cmd; p >= *cmdlinep; --p)
1854 {
1855 if (*p == ':')
1856 may_have_range = TRUE;
1857 if (p < ea.cmd && !VIM_ISWHITE(*p))
1858 break;
1859 }
1860 }
1861 else
1862 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001863 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001864 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001866#ifdef FEAT_EVAL
1867 // Handle ":export" - it functions almost like a command modifier.
1868 // ":export var Name: type"
1869 // ":export def Name(..."
1870 // etc.
1871 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1872 is_export = TRUE;
1873#endif
1874
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001875 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001876 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001877 if (ea.cmd == cmd + 1 && *cmd == '$')
1878 // should be "$VAR = val"
1879 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001880#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001881 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001882#else
1883 p = find_ex_command(&ea, NULL, NULL, NULL);
1884#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001885 if (ea.cmdidx == CMD_SIZE)
1886 {
1887 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1888
1889 // If a ':' before the range is missing, give a clearer error
1890 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001891 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001892 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001893 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001894 goto doend;
1895 }
1896 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001900
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001901#ifdef FEAT_EVAL
1902# ifdef FEAT_PROFILE
1903 // Count this line for profiling if skip is TRUE.
1904 if (do_profiling == PROF_YES
1905 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1906 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1907 {
1908 int skip = did_emsg || got_int || did_throw;
1909
1910 if (ea.cmdidx == CMD_catch)
1911 skip = !skip && !(cstack->cs_idx >= 0
1912 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1913 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1914 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1915 skip = skip || !(cstack->cs_idx >= 0
1916 && !(cstack->cs_flags[cstack->cs_idx]
1917 & (CSF_ACTIVE | CSF_TRUE)));
1918 else if (ea.cmdidx == CMD_finally)
1919 skip = FALSE;
1920 else if (ea.cmdidx != CMD_endif
1921 && ea.cmdidx != CMD_endfor
1922 && ea.cmdidx != CMD_endtry
1923 && ea.cmdidx != CMD_endwhile)
1924 skip = ea.skip;
1925
1926 if (!skip)
1927 {
1928 if (getline_equal(fgetline, cookie, get_func_line))
1929 func_line_exec(getline_cookie(fgetline, cookie));
1930 else if (getline_equal(fgetline, cookie, getsourceline))
1931 script_line_exec();
1932 }
1933 }
1934# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001935#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001936
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001937 ea.cmd = cmd;
1938
1939#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001940 // May go to debug mode. If this happens and the ">quit" debug command is
1941 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001942 dbg_check_breakpoint(&ea);
1943 if (!ea.skip && got_int)
1944 {
1945 ea.skip = TRUE;
1946 (void)do_intthrow(cstack);
1947 }
1948#endif
1949
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001950/*
1951 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 *
1953 * where 'addr' is:
1954 *
1955 * % (entire file)
1956 * $ [+-NUM]
1957 * 'x [+-NUM] (where x denotes a currently defined mark)
1958 * . [+-NUM]
1959 * [+-NUM]..
1960 * NUM
1961 *
1962 * The ea.cmd pointer is updated to point to the first character following the
1963 * range spec. If an initial address is found, but no second, the upper bound
1964 * is equal to the lower.
1965 */
1966
Bram Moolenaar25190db2019-05-04 15:05:28 +02001967 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001968 if (!IS_USER_CMDIDX(ea.cmdidx))
1969 {
1970 if (ea.cmdidx != CMD_SIZE)
1971 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1972 else
1973 ea.addr_type = ADDR_LINES;
1974
Bram Moolenaar25190db2019-05-04 15:05:28 +02001975 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001976 if (ea.cmdidx == CMD_wincmd && p != NULL)
1977 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001978#ifdef FEAT_QUICKFIX
1979 // :.cc in quickfix window uses line number
1980 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1981 ea.addr_type = ADDR_OTHER;
1982#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001983 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001984
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001985 if (!may_have_range)
1986 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001987 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1988 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001991 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 */
1993
1994 /*
1995 * Skip ':' and any white space
1996 */
1997 ea.cmd = skipwhite(ea.cmd);
1998 while (*ea.cmd == ':')
1999 ea.cmd = skipwhite(ea.cmd + 1);
2000
2001 /*
2002 * If we got a line, but no command, then go to the line.
2003 * If we find a '|' or '\n' we set ea.nextcmd.
2004 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002005 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2006 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 /*
2009 * strange vi behaviour:
2010 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002011 * ":3|..." prints line 3 (not in Vim9 script)
2012 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002016 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto doend;
2018 }
2019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // If this looks like an undefined user command and there are CmdUndefined
2021 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002022 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2023 && ASCII_ISUPPER(*ea.cmd)
2024 && has_cmdundefined())
2025 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002026 int ret;
2027
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002028 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002029 while (ASCII_ISALNUM(*p))
2030 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002031 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002032 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2033 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // If the autocommands did something and didn't cause an error, try
2035 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002036 p = (ret
2037#ifdef FEAT_EVAL
2038 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002039#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002041 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (p == NULL)
2044 {
2045 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002046 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 goto doend;
2048 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002049 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002050 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2051 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
2053 errormsg = uc_fun_cmd();
2054 goto doend;
2055 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (ea.cmdidx == CMD_SIZE)
2058 {
2059 if (!ea.skip)
2060 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002061 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002064 // If the modifier was parsed OK the error must be in the
2065 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002066 if (after_modifier != NULL)
2067 append_command(after_modifier);
2068 else
2069 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002070 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002071 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002073 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075 goto doend;
2076 }
2077
Bram Moolenaar958636c2014-10-21 20:01:58 +02002078 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2079 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002080#ifdef HAVE_EX_SCRIPT_NI
2081 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2082#endif
2083 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085#ifndef FEAT_EVAL
2086 /*
2087 * When the expression evaluation is disabled, recognize the ":if" and
2088 * ":endif" commands and ignore everything in between it.
2089 */
2090 if (ea.cmdidx == CMD_if)
2091 ++if_level;
2092 if (if_level)
2093 {
2094 if (ea.cmdidx == CMD_endif)
2095 --if_level;
2096 goto doend;
2097 }
2098
2099#endif
2100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002101 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002102 if (*p == '!' && ea.cmdidx != CMD_substitute
2103 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 ++p;
2106 ea.forceit = TRUE;
2107 }
2108 else
2109 ea.forceit = FALSE;
2110
2111/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002112 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002114 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002115 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 if (!ea.skip)
2118 {
2119#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002120 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002122 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002123 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 goto doend;
2125 }
2126#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002127 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002128 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002129 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002130 goto doend;
2131 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002132 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002135 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 goto doend;
2137 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002138
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002139 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002141 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2142 {
2143 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002144 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002145 goto doend;
2146 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002147 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2148 {
2149 // Command not allowed when text is locked
2150 errormsg = _(get_text_locked_msg());
2151 goto doend;
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002155 // Disallow editing another buffer when "curbuf_lock" is set.
2156 // Do allow ":checktime" (it is postponed).
2157 // Do allow ":edit" (check for an argument later).
2158 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002159 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002160 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002161 && ea.cmdidx != CMD_edit
2162 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002163 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002164 && curbuf_locked())
2165 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002167 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002169 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 goto doend;
2171 }
2172 }
2173
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002174 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002176 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 goto doend;
2178 }
2179
2180 /*
2181 * Don't complain about the range if it is not used
2182 * (could happen if line_count is accidentally set to 0).
2183 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002184 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
2186 /*
2187 * If the range is backwards, ask for confirmation and, if given, swap
2188 * ea.line1 & ea.line2 so it's forwards again.
2189 * When global command is busy, don't ask, will fail below.
2190 */
2191 if (!global_busy && ea.line1 > ea.line2)
2192 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002193 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002195 if (sourcing || exmode_active)
2196 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002197 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002198 goto doend;
2199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (ask_yesno((char_u *)
2201 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002202 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 lnum = ea.line1;
2205 ea.line1 = ea.line2;
2206 ea.line2 = lnum;
2207 }
2208 if ((errormsg = invalid_range(&ea)) != NULL)
2209 goto doend;
2210 }
2211
Bram Moolenaarb7316892019-05-01 18:08:42 +02002212 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2213 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 ea.line2 = 1;
2215
2216 correct_range(&ea);
2217
2218#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002219 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002220 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002222 // Put the first line at the start of a closed fold, put the last line
2223 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 (void)hasFolding(ea.line1, &ea.line1, NULL);
2225 (void)hasFolding(ea.line2, NULL, &ea.line2);
2226 }
2227#endif
2228
2229#ifdef FEAT_QUICKFIX
2230 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002231 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 * option here, so things like % get expanded.
2233 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002234 p = replace_makeprg(&ea, p, cmdlinep);
2235 if (p == NULL)
2236 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
2238
2239 /*
2240 * Skip to start of argument.
2241 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2242 */
2243 if (ea.cmdidx == CMD_bang)
2244 ea.arg = p;
2245 else
2246 ea.arg = skipwhite(p);
2247
Bram Moolenaar379fb762018-08-30 15:58:28 +02002248 // ":file" cannot be run with an argument when "curbuf_lock" is set
2249 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2250 goto doend;
2251
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 /*
2253 * Check for "++opt=val" argument.
2254 * Must be first, allow ":w ++enc=utf8 !cmd"
2255 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002256 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2258 if (getargopt(&ea) == FAIL && !ni)
2259 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002260 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 goto doend;
2262 }
2263
2264 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2265 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002266 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002270 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 goto doend;
2272 }
2273 ea.arg = skipwhite(ea.arg + 1);
2274 ea.append = TRUE;
2275 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002276 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 ++ea.arg;
2279 ea.usefilter = TRUE;
2280 }
2281 }
2282
2283 if (ea.cmdidx == CMD_read)
2284 {
2285 if (ea.forceit)
2286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002287 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 ea.forceit = FALSE;
2289 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002290 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 ++ea.arg;
2293 ea.usefilter = TRUE;
2294 }
2295 }
2296
2297 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2298 {
2299 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002300 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 ++ea.arg;
2303 ++ea.amount;
2304 }
2305 ea.arg = skipwhite(ea.arg);
2306 }
2307
2308 /*
2309 * Check for "+command" argument, before checking for next command.
2310 * Don't do this for ":read !cmd" and ":write !cmd".
2311 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2314
2315 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002316 * For commands that do not use '|' inside their argument: Check for '|' to
2317 * separate commands and '"' or '#' to start comments.
2318 *
2319 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002320 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002321 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002322 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002324 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2325 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002326 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002327 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002328 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002329 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330 || ea.cmdidx == CMD_global
2331 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002332 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002333#ifdef FEAT_EVAL
2334 || inside_block(&ea)
2335#endif
2336 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 for (p = ea.arg; *p; ++p)
2339 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002340 // Remove one backslash before a newline
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/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002721 * The "+" string used in place of an empty command in Ex mode.
2722 * This string is used in pointer comparison.
2723 */
2724static char exmode_plus[] = "+";
2725
2726/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002727 * Handle a range without a command.
2728 * Returns an error message on failure.
2729 */
2730 char *
2731ex_range_without_command(exarg_T *eap)
2732{
2733 char *errormsg = NULL;
2734
zeertzjq7d664bf2024-07-14 10:22:54 +02002735 if ((*eap->cmd == '|' ||
2736 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002737#ifdef FEAT_EVAL
2738 && !in_vim9script()
2739#endif
2740 )
2741 {
2742 eap->cmdidx = CMD_print;
2743 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2744 if ((errormsg = invalid_range(eap)) == NULL)
2745 {
2746 correct_range(eap);
2747 ex_print(eap);
2748 }
2749 }
2750 else if (eap->addr_count != 0)
2751 {
2752 if (eap->line2 > curbuf->b_ml.ml_line_count)
2753 {
2754 // With '-' in 'cpoptions' a line number past the file is an
2755 // error, otherwise put it at the end of the file.
2756 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2757 eap->line2 = -1;
2758 else
2759 eap->line2 = curbuf->b_ml.ml_line_count;
2760 }
2761
2762 if (eap->line2 < 0)
2763 errormsg = _(e_invalid_range);
2764 else
2765 {
2766 if (eap->line2 == 0)
2767 curwin->w_cursor.lnum = 1;
2768 else
2769 curwin->w_cursor.lnum = eap->line2;
2770 beginline(BL_SOL | BL_FIX);
2771 }
2772 }
2773 return errormsg;
2774}
2775
2776/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002777 * Check for an Ex command with optional tail.
2778 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002779 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002780 */
2781 static int
2782checkforcmd_opt(
2783 char_u **pp, // start of command
2784 char *cmd, // name of command
2785 int len, // required length
2786 int noparen)
2787{
2788 int i;
2789
2790 for (i = 0; cmd[i] != NUL; ++i)
2791 if (((char_u *)cmd)[i] != (*pp)[i])
2792 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002793 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002794 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002795 {
2796 *pp = skipwhite(*pp + i);
2797 return TRUE;
2798 }
2799 return FALSE;
2800}
2801
2802/*
2803 * Check for an Ex command with optional tail.
2804 * If there is a match advance "pp" to the argument and return TRUE.
2805 */
2806 int
2807checkforcmd(
2808 char_u **pp, // start of command
2809 char *cmd, // name of command
2810 int len) // required length
2811{
2812 return checkforcmd_opt(pp, cmd, len, FALSE);
2813}
2814
2815/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002816 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002817 * If there is a match advance "pp" to the argument and return TRUE.
2818 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002819 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002820checkforcmd_noparen(
2821 char_u **pp, // start of command
2822 char *cmd, // name of command
2823 int len) // required length
2824{
2825 return checkforcmd_opt(pp, cmd, len, TRUE);
2826}
2827
2828/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 * Parse and skip over command modifiers:
2830 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002831 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002833 * When "skip_only" is TRUE the global variables are not changed, except for
2834 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002835 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2836 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002837 * Call apply_cmdmod() to get the side effects of the modifiers:
2838 * - Increment "sandbox" for ":sandbox"
2839 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002840 * - set msg_silent for ":silent"
2841 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 * Return FAIL when the command is not to be executed.
2843 * May set "errormsg" to an error message.
2844 */
2845 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002846parse_command_modifiers(
2847 exarg_T *eap,
2848 char **errormsg,
2849 cmdmod_T *cmod,
2850 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002851{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002852 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002853 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002854 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002855 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002856 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002857 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002858
Bram Moolenaare1004402020-10-24 20:49:43 +02002859 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002860 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002861
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002862 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2863 {
2864 // The automatically inserted Visual area range is skipped, so that
2865 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002866 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2867 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002868 eap->cmd += 5;
2869 cmd_start = eap->cmd;
2870 has_visual_range = TRUE;
2871 }
2872
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002873 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 for (;;)
2875 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002876 char_u *p;
2877
Bram Moolenaareffed932018-08-14 13:38:17 +02002878 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002879 {
2880 if (*eap->cmd == ':')
2881 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002883 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002884
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002885 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002887 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2888 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2890 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002891 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002892 if (!skip_only)
2893 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002894 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002895 }
2896
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002897 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002898 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002899 {
2900 // a comment ends at a NL
zeertzjqf7b86092024-09-05 17:26:30 +02002901 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2902 if (eap->nextcmd != NULL)
2903 ++eap->nextcmd;
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002904 if (vim9script)
2905 {
2906 if (has_cmdmod(cmod, FALSE))
2907 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002908#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002909 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2910 && eap->cmd[2] != '{')
2911 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002912#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002913 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002915 }
zeertzjqf7b86092024-09-05 17:26:30 +02002916 if (*eap->cmd == '\n')
zeertzjq2432b4a2024-09-03 22:58:30 +02002917 {
2918 eap->nextcmd = eap->cmd + 1;
2919 return FAIL;
2920 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 if (*eap->cmd == NUL)
2922 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002923 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002924 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002925 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002926 if (vim9script && has_cmdmod(cmod, FALSE))
2927 *errormsg = _(e_command_modifier_without_command);
2928 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002929 return FAIL;
2930 }
2931
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002932 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002933
2934 // In Vim9 script a variable can shadow a command modifier:
2935 // verbose = 123
2936 // verbose += 123
2937 // silent! verbose = func()
2938 // verbose.member = 2
2939 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002940 // But not:
2941 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002942 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002943 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002944 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002945
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002946 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002947 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002948 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002949 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2950 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002951 break;
2952 }
2953
Bram Moolenaareffed932018-08-14 13:38:17 +02002954 switch (*p)
2955 {
John Marriotted908f72024-04-18 22:46:56 +02002956 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002957 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002959 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 continue;
2961
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002962 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002964 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 continue;
2966 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002967 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 {
2969#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002970 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002971#endif
2972 continue;
2973 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002974 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002976 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002977 continue;
2978
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002979 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002980 break;
2981#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002982 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002983#endif
2984 continue;
2985
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 continue;
2990 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002991 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 continue;
2995 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002996 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 continue;
3000 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003001 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 continue;
3005
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003006 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003008 char_u *reg_pat;
3009 char_u *nulp = NULL;
3010 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003011
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003012 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003013 || *p == NUL
3014 || (ends_excmd(*p)
3015#ifdef FEAT_EVAL
3016 // in ":filter #pat# cmd" # does not
3017 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003018 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003019#endif
3020 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 break;
3022 if (*p == '!')
3023 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003024 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 p = skipwhite(p + 1);
3026 if (*p == NUL || ends_excmd(*p))
3027 break;
3028 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003029#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003030 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003031 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003032 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003033#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003034 if (skip_only)
3035 p = skip_vimgrep_pat(p, NULL, NULL);
3036 else
3037 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003038 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3039 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 if (p == NULL || *p == NUL)
3041 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003042 if (!skip_only)
3043 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003044 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003045 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003046 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003047 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003048 // restore the character overwritten by NUL
3049 if (nulp != NULL)
3050 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003051 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 eap->cmd = p;
3053 continue;
3054 }
3055
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003056 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3057 {
3058 cmod->cmod_split |= WSP_HOR;
3059 continue;
3060 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003061 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003062 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003063 || *p == NUL || ends_excmd(*p))
3064 break;
3065 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003066 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003067 continue;
3068
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003069 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003070 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003071 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003072 continue;
3073 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003074 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3075 {
3076 if (ends_excmd2(p, eap->cmd))
3077 {
3078 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003079 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003080 return FAIL;
3081 }
3082 cmod->cmod_flags |= CMOD_LEGACY;
3083 continue;
3084 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003085
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003086 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003087 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003088 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003089 continue;
3090
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003091 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003092 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003093 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003094 continue;
3095 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003096 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003098 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003099 continue;
3100
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003101 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003103 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003104 continue;
3105
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003106 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003107 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003108 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003109 continue;
3110 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003111 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003112 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003113 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003114 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3115 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003116 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003117 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003118 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003119 }
3120 continue;
3121
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003122 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003123 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003124 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003125 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003126 long tabnr = get_address(eap, &eap->cmd,
3127 ADDR_TABS, eap->skip,
3128 skip_only, FALSE, 1);
3129 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003130 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003131 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003132 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003133 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3134 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003135 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003136 return FAIL;
3137 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003138 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003139 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003140 }
3141 eap->cmd = p;
3142 continue;
3143 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003144 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003145 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003146 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003147 continue;
3148
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003149 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003150 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003151 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003152 continue;
3153
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003154 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003155 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003156 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003157 continue;
3158 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003159 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003160 {
3161 if (ends_excmd2(p, eap->cmd))
3162 {
3163 *errormsg =
3164 _(e_vim9cmd_must_be_followed_by_command);
3165 return FAIL;
3166 }
3167 cmod->cmod_flags |= CMOD_VIM9CMD;
3168 continue;
3169 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003170 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003171 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003172 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003173 {
zeertzjqcd749632022-06-14 13:30:35 +01003174 // zero means not set, one is verbose == 0, etc.
3175 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003176 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003177 else
zeertzjqcd749632022-06-14 13:30:35 +01003178 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003179 eap->cmd = p;
3180 continue;
3181 }
3182 break;
3183 }
3184
Bram Moolenaar1501b632022-03-27 16:56:21 +01003185 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003186 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003187 if (eap->cmd > cmd_start)
3188 {
3189 // Move the '<,'> range to after the modifiers and insert a colon.
3190 // Since the modifiers have been parsed put the colon on top of the
3191 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3192 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003193 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003194 {
3195 size_t len = STRLEN(cmd_start);
3196
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003197 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003198 // "'<,'>mods" -> "mods *+
3199 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003200 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003201 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003202 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003203 }
3204 else
3205 {
3206 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3207 eap->cmd -= 5;
3208 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3209 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003210 }
3211 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003212 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003213 // Special case: change empty command to "+".
3214 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003215 eap->cmd = (char_u *)"'<,'>+";
3216 else
3217 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003218 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003219 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003220 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003221
Bram Moolenaareffed932018-08-14 13:38:17 +02003222 return OK;
3223}
3224
3225/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003226 * Return TRUE if "cmod" has anything set.
3227 */
3228 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003229has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003230{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003231 return (cmod->cmod_flags != 0 && (!ignore_silent
3232 || (cmod->cmod_flags
3233 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003234 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003235 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003236 || cmod->cmod_tab != 0
3237 || cmod->cmod_filter_regmatch.regprog != NULL;
3238}
3239
Bram Moolenaar8991be22022-02-14 21:51:46 +00003240#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003241/*
3242 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3243 */
3244 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003245cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003246{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003247 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003248 {
3249 emsg(_(e_misplaced_command_modifier));
3250 return TRUE;
3251 }
3252 return FALSE;
3253}
Dominique Pelle748b3082022-01-08 12:41:16 +00003254#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003255
3256/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003257 * Apply the command modifiers. Saves current state in "cmdmod", call
3258 * undo_cmdmod() later.
3259 */
3260 void
3261apply_cmdmod(cmdmod_T *cmod)
3262{
3263#ifdef HAVE_SANDBOX
3264 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3265 {
3266 ++sandbox;
3267 cmod->cmod_did_sandbox = TRUE;
3268 }
3269#endif
zeertzjqcd749632022-06-14 13:30:35 +01003270 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003271 {
3272 if (cmod->cmod_verbose_save == 0)
3273 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003274 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003275 }
3276
3277 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3278 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003279 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003280 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003281 cmod->cmod_save_msg_scroll = msg_scroll;
3282 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003283 if (cmod->cmod_flags & CMOD_SILENT)
3284 ++msg_silent;
3285 if (cmod->cmod_flags & CMOD_UNSILENT)
3286 msg_silent = 0;
3287
3288 if (cmod->cmod_flags & CMOD_ERRSILENT)
3289 {
3290 ++emsg_silent;
3291 ++cmod->cmod_did_esilent;
3292 }
3293
Bram Moolenaare1004402020-10-24 20:49:43 +02003294 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003295 {
3296 // Set 'eventignore' to "all".
3297 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003298 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003299 set_string_option_direct((char_u *)"ei", -1,
3300 (char_u *)"all", OPT_FREE, SID_NONE);
3301 }
3302}
3303
3304/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003305 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003306 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003307 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003308undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003309{
Bram Moolenaare1004402020-10-24 20:49:43 +02003310 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003311 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003312 p_verbose = cmod->cmod_verbose_save - 1;
3313 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003314 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003315
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003316#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003317 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003318 {
3319 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003320 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003321 }
3322#endif
3323
Bram Moolenaare1004402020-10-24 20:49:43 +02003324 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003326 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003327 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3328 OPT_FREE, SID_NONE);
3329 free_string_option(cmod->cmod_save_ei);
3330 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003331 }
3332
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003333 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003334
Bram Moolenaare1004402020-10-24 20:49:43 +02003335 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003336 {
3337 // messages could be enabled for a serious error, need to check if the
3338 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003339 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3340 msg_silent = cmod->cmod_save_msg_silent - 1;
3341 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003342 if (emsg_silent < 0)
3343 emsg_silent = 0;
3344 // Restore msg_scroll, it's set by file I/O commands, even when no
3345 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003346 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003347
3348 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3349 // somewhere in the line. Put it back in the first column.
3350 if (redirecting())
3351 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003352
Bram Moolenaare1004402020-10-24 20:49:43 +02003353 cmod->cmod_save_msg_silent = 0;
3354 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003355 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003356}
3357
3358/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003359 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003360 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003361 * Return FAIL and set "errormsg" or return OK.
3362 */
3363 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003364parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003365{
3366 int address_count = 1;
3367 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003368 int need_check_cursor = FALSE;
3369 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003370
3371 // Repeat for all ',' or ';' separated addresses.
3372 for (;;)
3373 {
3374 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003375 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003376 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003377 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003378 eap->addr_count == 0, address_count++);
3379 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003380 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003381 if (lnum == MAXLNUM)
3382 {
3383 if (*eap->cmd == '%') // '%' - all lines
3384 {
3385 ++eap->cmd;
3386 switch (eap->addr_type)
3387 {
3388 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003389 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003390 eap->line1 = 1;
3391 eap->line2 = curbuf->b_ml.ml_line_count;
3392 break;
3393 case ADDR_LOADED_BUFFERS:
3394 {
3395 buf_T *buf = firstbuf;
3396
3397 while (buf->b_next != NULL
3398 && buf->b_ml.ml_mfp == NULL)
3399 buf = buf->b_next;
3400 eap->line1 = buf->b_fnum;
3401 buf = lastbuf;
3402 while (buf->b_prev != NULL
3403 && buf->b_ml.ml_mfp == NULL)
3404 buf = buf->b_prev;
3405 eap->line2 = buf->b_fnum;
3406 break;
3407 }
3408 case ADDR_BUFFERS:
3409 eap->line1 = firstbuf->b_fnum;
3410 eap->line2 = lastbuf->b_fnum;
3411 break;
3412 case ADDR_WINDOWS:
3413 case ADDR_TABS:
3414 if (IS_USER_CMDIDX(eap->cmdidx))
3415 {
3416 eap->line1 = 1;
3417 eap->line2 = eap->addr_type == ADDR_WINDOWS
3418 ? LAST_WIN_NR : LAST_TAB_NR;
3419 }
3420 else
3421 {
3422 // there is no Vim command which uses '%' and
3423 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003424 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003425 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003426 }
3427 break;
3428 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003429 case ADDR_UNSIGNED:
3430 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003431 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003432 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003433 case ADDR_ARGUMENTS:
3434 if (ARGCOUNT == 0)
3435 eap->line1 = eap->line2 = 0;
3436 else
3437 {
3438 eap->line1 = 1;
3439 eap->line2 = ARGCOUNT;
3440 }
3441 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003442 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003443#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003444 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003445 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003446 if (eap->line2 == 0)
3447 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003448#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003449 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003450 case ADDR_NONE:
3451 // Will give an error later if a range is found.
3452 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003453 }
3454 ++eap->addr_count;
3455 }
3456 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3457 {
3458 pos_T *fp;
3459
3460 // '*' - visual area
3461 if (eap->addr_type != ADDR_LINES)
3462 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003463 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003464 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003465 }
3466
3467 ++eap->cmd;
3468 if (!eap->skip)
3469 {
3470 fp = getmark('<', FALSE);
3471 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003472 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003473 eap->line1 = fp->lnum;
3474 fp = getmark('>', FALSE);
3475 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003476 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003477 eap->line2 = fp->lnum;
3478 ++eap->addr_count;
3479 }
3480 }
3481 }
3482 else
3483 eap->line2 = lnum;
3484 eap->addr_count++;
3485
3486 if (*eap->cmd == ';')
3487 {
3488 if (!eap->skip)
3489 {
3490 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003491
Bram Moolenaar0e717042020-04-27 19:29:01 +02003492 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003493 // accept zero as address, so 0;/PATTERN/ works correctly
3494 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003495 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003496 if (eap->line2 > 0)
3497 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003498 else
3499 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003500 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003501 }
3502 }
3503 else if (*eap->cmd != ',')
3504 break;
3505 ++eap->cmd;
3506 }
3507
3508 // One address given: set start and end lines.
3509 if (eap->addr_count == 1)
3510 {
3511 eap->line1 = eap->line2;
3512 // ... but only implicit: really no address given
3513 if (lnum == MAXLNUM)
3514 eap->addr_count = 0;
3515 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003516 ret = OK;
3517
3518theend:
3519 if (need_check_cursor)
3520 check_cursor();
3521 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003522}
3523
3524/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003525 * Append "cmd" to the error message in IObuff.
3526 * Takes care of limiting the length and handling 0xa0, which would be
3527 * invisible otherwise.
3528 */
3529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003530append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003531{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003532 size_t len = STRLEN(IObuff);
3533 char_u *s = cmd;
3534 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003535
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003536 if (len > IOSIZE - 100)
3537 {
3538 // Not enough space, truncate and put in "...".
3539 d = IObuff + IOSIZE - 100;
3540 d -= mb_head_off(IObuff, d);
3541 STRCPY(d, "...");
3542 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003543 STRCAT(IObuff, ": ");
3544 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003545 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003546 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003547 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003548 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003549 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003550 STRCPY(d, "<a0>");
3551 d += 4;
3552 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003553 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3554 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003555 else
3556 MB_COPY_CHAR(s, d);
3557 }
3558 *d = NUL;
3559}
3560
Dominique Pelle748b3082022-01-08 12:41:16 +00003561#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003562/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003563 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3564 * the name. Otherwise just return "start".
3565 */
3566 char_u *
3567skip_option_env_lead(char_u *start)
3568{
3569 char_u *name = start;
3570
3571 if (*start == '&')
3572 {
3573 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3574 name += 3;
3575 else
3576 name += 1;
3577 }
3578 else if (*start == '$')
3579 name += 1;
3580 return name;
3581}
Dominique Pelle748b3082022-01-08 12:41:16 +00003582#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003583
3584/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003585 * Return TRUE and set "*idx" if "p" points to a one letter command.
3586 * If not in Vim9 script:
3587 * - The 'k' command can directly be followed by any character.
3588 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3589 * but :sre[wind] is another command, as are :scr[iptnames],
3590 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3591 */
3592 static int
3593one_letter_cmd(char_u *p, cmdidx_T *idx)
3594{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003595 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003596 return FALSE;
3597 if (*p == 'k')
3598 {
3599 *idx = CMD_k;
3600 return TRUE;
3601 }
3602 if (p[0] == 's'
3603 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3604 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3605 || p[1] == 'g'
3606 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3607 || p[1] == 'I'
3608 || (p[1] == 'r' && p[2] != 'e')))
3609 {
3610 *idx = CMD_substitute;
3611 return TRUE;
3612 }
3613 return FALSE;
3614}
3615
Dominique Pellee764d1b2023-03-12 21:20:59 +00003616#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003617/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003618 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3619 * call.
3620 */
3621 int
3622number_method(char_u *cmd)
3623{
3624 char_u *p = skipdigits(cmd);
3625
3626 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3627}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003628#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003629
3630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003632 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003633 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003634 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 *
3636 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3637 * assignment without "let". Sets eap->cmdidx to the command while returning
3638 * "eap->cmd".
3639 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 * Returns NULL for an ambiguous user command.
3641 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 char_u *
3643find_ex_command(
3644 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003645 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003646 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003647 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648{
3649 int len;
3650 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003651 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003652#ifndef FEAT_EVAL
3653 int vim9 = FALSE;
3654#else
3655 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 /*
3658 * Recognize a Vim9 script function/method call and assignment:
3659 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3660 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003661 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003662 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003664 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003666 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003667 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3668 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003670 int oplen;
3671 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003672 char_u *swp;
3673
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003674 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003675 || (eap->cmd[0] == '$'
3676 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003677 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003678 && (valid_yank_reg(eap->cmd[1], FALSE)
3679 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003680 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003681 if (*eap->cmd == '&')
3682 {
3683 p = eap->cmd + 1;
3684 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3685 p += 2;
3686 p = to_name_end(p, FALSE);
3687 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003688 else if (*eap->cmd == '$')
3689 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003690 else
3691 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003692 if (ends_excmd(*skipwhite(p)))
3693 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003694 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3695 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003696 eap->cmdidx = CMD_eval;
3697 return eap->cmd;
3698 }
3699 // "&option" can be followed by "->" or "=", check below
3700 }
3701
3702 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003704 if (
3705 // "(..." is an expression.
3706 // "funcname(" is always a function call.
3707 *p == '('
3708 || (p == eap->cmd
3709 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003710 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003711 *eap->cmd == '{'
3712 // "'string'->func()" is an expression.
3713 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003714 // '"string"->func()' is an expression.
3715 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003716 // '$"string"->func()' is an expression.
3717 // "$'string'->func()" is an expression.
3718 || (eap->cmd[0] == '$'
3719 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3720 // '0z1234->func()' is an expression.
3721 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003722 // "g:varname" is an expression.
3723 || eap->cmd[1] == ':'
3724 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003725 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003726 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003727 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003728 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3729 {
3730 // "{" by itself is the start of a block.
3731 eap->cmdidx = CMD_block;
3732 return eap->cmd + 1;
3733 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003734 eap->cmdidx = CMD_eval;
3735 return eap->cmd;
3736 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003737
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003738 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003739 // "varname[]" is an expression.
3740 *p == '['
3741 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003742 || (*p == '.'
3743 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3744 // g:[key] is an expression
3745 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003746 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003747 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003748
3749 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003750 // Skip over the whole thing, it can be:
3751 // name.member = val
3752 // name[a : b] = val
3753 // name[idx] = val
3754 // name[idx].member = val
3755 // etc.
3756 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003757 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003758 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003759 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003760 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003761 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003762 || (after[0] == '.' && after[1] == '.'
3763 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003764 eap->cmdidx = CMD_var;
3765 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003766 --emsg_silent;
3767 return eap->cmd;
3768 }
3769
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003770 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3771 // an assignment.
3772 // If there is no line break inside the "[...]" then "p" is
3773 // advanced to after the "]" by to_name_const_end(): check if a "="
3774 // follows.
3775 // If "[...]" has a line break "p" still points at the "[" and it
3776 // can't be an assignment.
3777 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003778 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003779 char_u *eq;
3780
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003781 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003782 if (p == eap->cmd && *p == '[')
3783 {
3784 int count = 0;
3785 int semicolon = FALSE;
3786
3787 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3788 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003789 eq = p;
3790 if (eq != NULL)
3791 {
3792 eq = skipwhite(eq);
3793 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3794 ++eq;
3795 }
3796 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003797 {
3798 eap->cmdidx = CMD_eval;
3799 return eap->cmd;
3800 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003801 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003802 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003803 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003804 return eap->cmd;
3805 }
3806 }
3807
3808 // Recognize an assignment if we recognize the variable name:
3809 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003810 // "@r = expr"
3811 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003812 // "var = expr" where "var" is a variable name or we are skipping
3813 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003814 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003815 oplen = assignment_len(skipwhite(p), &heredoc);
3816 if (oplen > 0)
3817 {
3818 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3819 || *eap->cmd == '&'
3820 || *eap->cmd == '$'
3821 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003822 || (eap->skip && IS_WHITE_OR_NUL(
3823 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003824 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003825 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003826 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003827 return eap->cmd;
3828 }
3829 }
3830
Bram Moolenaar9510d222022-09-11 15:14:05 +01003831 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003832 // "w:varname: number = 123".
3833 if (eap->cmd[1] == ':' && *p == ':')
3834 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003835 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003836 return eap->cmd;
3837 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003838 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003839
Bram Moolenaar31d99482022-05-26 22:24:43 +01003840 // 1234->func() is a method call
3841 if (number_method(eap->cmd))
3842 {
3843 eap->cmdidx = CMD_eval;
3844 return eap->cmd;
3845 }
3846
Bram Moolenaar7b829262021-10-13 15:04:34 +01003847 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3848 // an expression. A global/substitute/list command needs to use a
3849 // longer name.
3850 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3851 {
3852 eap->cmdidx = CMD_eval;
3853 return eap->cmd;
3854 }
3855
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003856 // If it is an ID it might be a variable with an operator on the next
3857 // line, if the variable exists it can't be an Ex command.
3858 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003859 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003860 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3861 {
3862 eap->cmdidx = CMD_eval;
3863 return eap->cmd;
3864 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003865
3866 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003867 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3868 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003869 {
3870 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3871 return eap->cmd + 2;
3872 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 }
3874#endif
3875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 /*
3877 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 */
3879 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003880 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 ++p;
3883 }
3884 else
3885 {
3886 while (ASCII_ISALPHA(*p))
3887 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003888 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003889 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003891 while (ASCII_ISALNUM(*p))
3892 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 }
3894 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3895 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003896 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 ++p;
3898 while (ASCII_ISALPHA(*p))
3899 ++p;
3900 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003902 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003903 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 ++p;
3905 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003906 // The "d" command can directly be followed by 'l' or 'p' flag, when
3907 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003908 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003909 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003910 // Check for ":dl", ":dell", etc. to ":deletel": that's
3911 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003912 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003913 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003914 break;
3915 if (i == len - 1)
3916 {
3917 --len;
3918 if (p[-1] == 'l')
3919 eap->flags |= EXFLAG_LIST;
3920 else
3921 eap->flags |= EXFLAG_PRINT;
3922 }
3923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003925 if (ASCII_ISLOWER(eap->cmd[0]))
3926 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003927 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003928 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003929
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003930 if (command_count != (int)CMD_SIZE)
3931 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003932 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003933 getout(1);
3934 }
3935
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003936 // Use a precomputed index for fast look-up in cmdnames[]
3937 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003938 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3939 if (ASCII_ISLOWER(c2))
3940 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3941 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003942 else if (ASCII_ISUPPER(eap->cmd[0]))
3943 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003945 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
3947 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3948 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3949 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3950 (size_t)len) == 0)
3951 {
3952#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003953 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 *full = TRUE;
3955#endif
3956 break;
3957 }
3958
Bram Moolenaar204852a2022-03-05 12:56:44 +00003959 // :Print and :mode are not supported in Vim9 script.
3960 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003961 if (vim9 && eap->cmdidx != CMD_SIZE)
3962 {
3963 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3964 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003965 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003966 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003967 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003968 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003969 eap->cmdidx = CMD_SIZE;
3970 }
3971 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003972
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003973 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003974 // 'cpoptions'.
3975 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3976 p = eap->cmd;
3977
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003978 // Look for a user defined command as a last resort. Let ":Print" be
3979 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003980 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3981 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003983 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 while (ASCII_ISALNUM(*p))
3985 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003986 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003988 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 eap->cmdidx = CMD_SIZE;
3990 }
3991
Bram Moolenaar204852a2022-03-05 12:56:44 +00003992 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3993 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003994 eap->cmdidx = CMD_finally;
3995
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003996#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003997 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003998 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02003999 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004000 && (eap->cmdidx < 0 ||
4001 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004002 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004003 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4004
4005 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004006 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004007 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004008 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004009#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 return p;
4012}
4013
4014#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004015typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004016{
4017 char *name;
4018 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004019 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004020} cmdmod_info_T;
4021
4022static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004023 {"aboveleft", 3, FALSE},
4024 {"belowright", 3, FALSE},
4025 {"botright", 2, FALSE},
4026 {"browse", 3, FALSE},
4027 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004028 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004029 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004030 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004031 {"keepalt", 5, FALSE},
4032 {"keepjumps", 5, FALSE},
4033 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004034 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004035 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004036 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004037 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004038 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004039 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004040 {"rightbelow", 6, FALSE},
4041 {"sandbox", 3, FALSE},
4042 {"silent", 3, FALSE},
4043 {"tab", 3, TRUE},
4044 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004045 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004046 {"verbose", 4, TRUE},
4047 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004048 {"vim9cmd", 4, FALSE}
4049}; // cmdmod_info_tab
4050
Bram Moolenaared53fb92007-11-24 20:50:24 +00004051/*
4052 * Return length of a command modifier (including optional count).
4053 * Return zero when it's not a modifier.
4054 */
4055 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004056modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004057{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004058 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004059 char_u *p = cmd;
4060
4061 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004062 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004063 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004064 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004065 for (j = 0; p[j] != NUL; ++j)
4066 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004067 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004068 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4069 && (p == cmd || cmdmod_info_tab[i].has_count))
4070 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004071 }
4072 return 0;
4073}
4074
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075/*
4076 * Return > 0 if an Ex command "name" exists.
4077 * Return 2 if there is an exact match.
4078 * Return 3 if there is an ambiguous match.
4079 */
4080 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004081cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
4083 exarg_T ea;
4084 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004085 int i;
4086 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004087 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
Christian Brabandt70a11a62024-07-26 19:13:55 +02004089 // Check command modifiers.
4090 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004092 for (j = 0; name[j] != NUL; ++j)
4093 if (name[j] != cmdmod_info_tab[i].name[j])
4094 break;
4095 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4096 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004099 // Check built-in commands and user defined commands.
4100 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004101 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004103 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004105 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004107 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4108 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004109 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004110 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4112}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004113
4114/*
4115 * "fullcommand" function
4116 */
4117 void
4118f_fullcommand(typval_T *argvars, typval_T *rettv)
4119{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004120 exarg_T ea;
4121 char_u *name;
4122 char_u *p;
4123 int vim9script = in_vim9script();
4124 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004125
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004126 rettv->v_type = VAR_STRING;
4127 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004128
Bram Moolenaaraa534142022-09-15 21:46:02 +01004129 if (in_vim9script()
4130 && (check_for_string_arg(argvars, 0) == FAIL
4131 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004132 return;
4133
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004134 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004135 if (name == NULL)
4136 return;
4137
Bram Moolenaaraa534142022-09-15 21:46:02 +01004138 if (argvars[1].v_type != VAR_UNKNOWN)
4139 {
4140 vim9script = tv_get_bool(&argvars[1]);
4141 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4142 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4143 }
4144
zeertzjqc024ed92022-01-04 16:22:52 +00004145 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004146 name++;
4147 name = skip_range(name, TRUE, NULL);
4148
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004149 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4150 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004151 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004152 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004153 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004154 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004155 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004156 goto theend;
4157
4158 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004159 {
4160 int res;
4161
4162 ++emsg_silent;
4163 res = not_in_vim9(&ea);
4164 --emsg_silent;
4165
4166 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004167 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004168 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004169
4170 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004171 ? get_user_command_name(ea.useridx, ea.cmdidx)
4172 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004173theend:
4174 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176#endif
4177
Bram Moolenaard0190392019-08-23 21:17:35 +02004178 cmdidx_T
4179excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180{
Bram Moolenaard0190392019-08-23 21:17:35 +02004181 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004183 if (!one_letter_cmd(cmd, &idx))
4184 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4185 idx = (cmdidx_T)((int)idx + 1))
4186 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4187 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaard0190392019-08-23 21:17:35 +02004189 return idx;
4190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
Bram Moolenaard0190392019-08-23 21:17:35 +02004192 long
4193excmd_get_argt(cmdidx_T idx)
4194{
4195 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196}
4197
4198/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004199 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 *
4201 * Backslashed delimiters after / or ? will be skipped, and commands will
4202 * not be expanded between /'s and ?'s or after "'".
4203 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004204 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * Returns the "cmd" pointer advanced to beyond the range.
4206 */
4207 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004208skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004209 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004210 int skip_star, // skip "*" used for Visual range
4211 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004213 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004214 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004216 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004218 if (*cmd == '\\')
4219 {
4220 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4221 ++cmd;
4222 else
4223 break;
4224 }
4225 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004227 char_u *p = cmd;
4228
4229 // a quote is only valid at the start or after a separator
4230 while (p > cmd_start)
4231 {
4232 --p;
4233 if (!VIM_ISWHITE(*p))
4234 break;
4235 }
4236 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4237 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 if (*++cmd == NUL && ctx != NULL)
4239 *ctx = EXPAND_NOTHING;
4240 }
4241 else if (*cmd == '/' || *cmd == '?')
4242 {
4243 delim = *cmd++;
4244 while (*cmd != NUL && *cmd != delim)
4245 if (*cmd++ == '\\' && *cmd != NUL)
4246 ++cmd;
4247 if (*cmd == NUL && ctx != NULL)
4248 *ctx = EXPAND_NOTHING;
4249 }
4250 if (*cmd != NUL)
4251 ++cmd;
4252 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004253
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004254 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004255 while (*cmd == ':')
4256 cmd = skipwhite(cmd + 1);
4257
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004258 // Skip "*" used for Visual range.
4259 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4260 cmd = skipwhite(cmd + 1);
4261
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 return cmd;
4263}
4264
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004265 static void
4266addr_error(cmd_addr_T addr_type)
4267{
4268 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004269 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004270 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004271 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004272}
4273
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004275 * Return the default address for an address type.
4276 */
4277 static linenr_T
4278default_address(exarg_T *eap)
4279{
4280 linenr_T lnum = 0;
4281
4282 switch (eap->addr_type)
4283 {
4284 case ADDR_LINES:
4285 case ADDR_OTHER:
4286 // Default is the cursor line number. Avoid using an invalid
4287 // line number though.
4288 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4289 lnum = curbuf->b_ml.ml_line_count;
4290 else
4291 lnum = curwin->w_cursor.lnum;
4292 break;
4293 case ADDR_WINDOWS:
4294 lnum = CURRENT_WIN_NR;
4295 break;
4296 case ADDR_ARGUMENTS:
4297 lnum = curwin->w_arg_idx + 1;
4298 if (lnum > ARGCOUNT)
4299 lnum = ARGCOUNT;
4300 break;
4301 case ADDR_LOADED_BUFFERS:
4302 case ADDR_BUFFERS:
4303 lnum = curbuf->b_fnum;
4304 break;
4305 case ADDR_TABS:
4306 lnum = CURRENT_TAB_NR;
4307 break;
4308 case ADDR_TABS_RELATIVE:
4309 case ADDR_UNSIGNED:
4310 lnum = 1;
4311 break;
4312 case ADDR_QUICKFIX:
4313#ifdef FEAT_QUICKFIX
4314 lnum = qf_get_cur_idx(eap);
4315#endif
4316 break;
4317 case ADDR_QUICKFIX_VALID:
4318#ifdef FEAT_QUICKFIX
4319 lnum = qf_get_cur_valid_idx(eap);
4320#endif
4321 break;
4322 case ADDR_NONE:
4323 // Will give an error later if a range is found.
4324 break;
4325 }
4326 return lnum;
4327}
4328
4329/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004330 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 *
4332 * Set ptr to the next character after the part that was interpreted.
4333 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004334 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 *
4336 * Return MAXLNUM when no Ex address was found.
4337 */
4338 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004339get_address(
4340 exarg_T *eap UNUSED,
4341 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004342 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004343 int skip, // only skip the address, don't use it
4344 int silent, // no errors or side effects
4345 int to_other_file, // flag: may jump to other file
4346 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
4348 int c;
4349 int i;
4350 long n;
4351 char_u *cmd;
4352 pos_T pos;
4353 pos_T *fp;
4354 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004355 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 cmd = skipwhite(*ptr);
4358 lnum = MAXLNUM;
4359 do
4360 {
4361 switch (*cmd)
4362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 ++cmd;
4365 switch (addr_type)
4366 {
4367 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004368 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 lnum = curwin->w_cursor.lnum;
4370 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004371 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004372 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004373 break;
4374 case ADDR_ARGUMENTS:
4375 lnum = curwin->w_arg_idx + 1;
4376 break;
4377 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004378 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004379 lnum = curbuf->b_fnum;
4380 break;
4381 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004382 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004383 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004384 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004385 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004386 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004387 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004388 cmd = NULL;
4389 goto error;
4390 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004391 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004392#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004393 lnum = qf_get_cur_idx(eap);
4394#endif
4395 break;
4396 case ADDR_QUICKFIX_VALID:
4397#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004398 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004399#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004400 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 }
4402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004404 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004405 ++cmd;
4406 switch (addr_type)
4407 {
4408 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004409 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 lnum = curbuf->b_ml.ml_line_count;
4411 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004413 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004414 break;
4415 case ADDR_ARGUMENTS:
4416 lnum = ARGCOUNT;
4417 break;
4418 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004419 buf = lastbuf;
4420 while (buf->b_ml.ml_mfp == NULL)
4421 {
4422 if (buf->b_prev == NULL)
4423 break;
4424 buf = buf->b_prev;
4425 }
4426 lnum = buf->b_fnum;
4427 break;
4428 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 lnum = lastbuf->b_fnum;
4430 break;
4431 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004432 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004434 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004435 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004436 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004437 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004438 cmd = NULL;
4439 goto error;
4440 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004441 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004442#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004443 lnum = qf_get_size(eap);
4444 if (lnum == 0)
4445 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004446#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004447 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004448 case ADDR_QUICKFIX_VALID:
4449#ifdef FEAT_QUICKFIX
4450 lnum = qf_get_valid_size(eap);
4451 if (lnum == 0)
4452 lnum = 1;
4453#endif
4454 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 }
4456 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004458 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 if (*++cmd == NUL)
4460 {
4461 cmd = NULL;
4462 goto error;
4463 }
4464 if (addr_type != ADDR_LINES)
4465 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004466 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004467 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 goto error;
4469 }
4470 if (skip)
4471 ++cmd;
4472 else
4473 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004474 // Only accept a mark in another file when it is
4475 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4477 ++cmd;
4478 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004479 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 lnum = curwin->w_cursor.lnum;
4481 else
4482 {
4483 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
4485 cmd = NULL;
4486 goto error;
4487 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 lnum = fp->lnum;
4489 }
4490 }
4491 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004494 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 c = *cmd++;
4496 if (addr_type != ADDR_LINES)
4497 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004498 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004499 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 goto error;
4501 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004502 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004504 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 if (*cmd == c)
4506 ++cmd;
4507 }
4508 else
4509 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004510 int flags;
4511
4512 pos = curwin->w_cursor; // save curwin->w_cursor
4513
4514 // When '/' or '?' follows another address, start from
4515 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004516 if (lnum > 0 && lnum != MAXLNUM)
4517 curwin->w_cursor.lnum =
4518 lnum > curbuf->b_ml.ml_line_count
4519 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004520
4521 // Start a forward search at the end of the line (unless
4522 // before the first line).
4523 // Start a backward search at the start of the line.
4524 // This makes sure we never match in the current
4525 // line, and can match anywhere in the
4526 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004527 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004528 curwin->w_cursor.col = MAXCOL;
4529 else
4530 curwin->w_cursor.col = 0;
4531 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004532 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004533 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004534 {
4535 curwin->w_cursor = pos;
4536 cmd = NULL;
4537 goto error;
4538 }
4539 lnum = curwin->w_cursor.lnum;
4540 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004541 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004542 cmd += searchcmdlen;
4543 }
4544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004546 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004547 ++cmd;
4548 if (addr_type != ADDR_LINES)
4549 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004550 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004551 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004552 goto error;
4553 }
4554 if (*cmd == '&')
4555 i = RE_SUBST;
4556 else if (*cmd == '?' || *cmd == '/')
4557 i = RE_SEARCH;
4558 else
4559 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004560 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004561 cmd = NULL;
4562 goto error;
4563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004565 if (!skip)
4566 {
4567 /*
4568 * When search follows another address, start from
4569 * there.
4570 */
4571 if (lnum != MAXLNUM)
4572 pos.lnum = lnum;
4573 else
4574 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 /*
4577 * Start the search just like for the above
4578 * do_search().
4579 */
4580 if (*cmd != '?')
4581 pos.col = MAXCOL;
4582 else
4583 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004584 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004585 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004586 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004587 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004588 lnum = pos.lnum;
4589 else
4590 {
4591 cmd = NULL;
4592 goto error;
4593 }
4594 }
4595 ++cmd;
4596 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004599 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004600 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602
4603 for (;;)
4604 {
4605 cmd = skipwhite(cmd);
4606 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4607 break;
4608
4609 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004610 {
4611 switch (addr_type)
4612 {
4613 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004614 case ADDR_OTHER:
4615 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004616 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 break;
4618 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004619 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004620 break;
4621 case ADDR_ARGUMENTS:
4622 lnum = curwin->w_arg_idx + 1;
4623 break;
4624 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004625 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004626 lnum = curbuf->b_fnum;
4627 break;
4628 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004629 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004630 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004631 case ADDR_TABS_RELATIVE:
4632 lnum = 1;
4633 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004634 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004635#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004636 lnum = qf_get_cur_idx(eap);
4637#endif
4638 break;
4639 case ADDR_QUICKFIX_VALID:
4640#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004641 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004642#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004643 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004644 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004645 case ADDR_UNSIGNED:
4646 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004647 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004648 }
4649 }
4650
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004652 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 else
4654 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004655 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 n = 1;
4657 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004658 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004659 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004661 if (n == MAXLNUM)
4662 {
4663 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004664 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004665 goto error;
4666 }
4667 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004668
4669 if (addr_type == ADDR_TABS_RELATIVE)
4670 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004671 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004672 cmd = NULL;
4673 goto error;
4674 }
4675 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004676 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004677 lnum = compute_buffer_local_count(
4678 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004680 {
4681#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004682 // Relative line addressing: need to adjust for lines in a
4683 // closed fold after the first address.
4684 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4685 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004686 (void)hasFolding(lnum, NULL, &lnum);
4687#endif
4688 if (i == '-')
4689 lnum -= n;
4690 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004691 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004692 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004693 {
4694 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004695 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004696 goto error;
4697 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004698 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004699 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
4702 } while (*cmd == '/' || *cmd == '?');
4703
4704error:
4705 *ptr = cmd;
4706 return lnum;
4707}
4708
4709/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004710 * Set eap->line1 and eap->line2 to the whole range.
4711 * Used for commands with the EX_DFLALL flag and no range given.
4712 */
4713 static void
4714address_default_all(exarg_T *eap)
4715{
4716 eap->line1 = 1;
4717 switch (eap->addr_type)
4718 {
4719 case ADDR_LINES:
4720 case ADDR_OTHER:
4721 eap->line2 = curbuf->b_ml.ml_line_count;
4722 break;
4723 case ADDR_LOADED_BUFFERS:
4724 {
4725 buf_T *buf = firstbuf;
4726
4727 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4728 buf = buf->b_next;
4729 eap->line1 = buf->b_fnum;
4730 buf = lastbuf;
4731 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4732 buf = buf->b_prev;
4733 eap->line2 = buf->b_fnum;
4734 }
4735 break;
4736 case ADDR_BUFFERS:
4737 eap->line1 = firstbuf->b_fnum;
4738 eap->line2 = lastbuf->b_fnum;
4739 break;
4740 case ADDR_WINDOWS:
4741 eap->line2 = LAST_WIN_NR;
4742 break;
4743 case ADDR_TABS:
4744 eap->line2 = LAST_TAB_NR;
4745 break;
4746 case ADDR_TABS_RELATIVE:
4747 eap->line2 = 1;
4748 break;
4749 case ADDR_ARGUMENTS:
4750 if (ARGCOUNT == 0)
4751 eap->line1 = eap->line2 = 0;
4752 else
4753 eap->line2 = ARGCOUNT;
4754 break;
4755 case ADDR_QUICKFIX_VALID:
4756#ifdef FEAT_QUICKFIX
4757 eap->line2 = qf_get_valid_size(eap);
4758 if (eap->line2 == 0)
4759 eap->line2 = 1;
4760#endif
4761 break;
4762 case ADDR_NONE:
4763 case ADDR_UNSIGNED:
4764 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004765 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004766 break;
4767 }
4768}
4769
4770
4771/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004772 * Get flags from an Ex command argument.
4773 */
4774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004775get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004776{
4777 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4778 {
4779 if (*eap->arg == 'l')
4780 eap->flags |= EXFLAG_LIST;
4781 else if (*eap->arg == 'p')
4782 eap->flags |= EXFLAG_PRINT;
4783 else
4784 eap->flags |= EXFLAG_NR;
4785 eap->arg = skipwhite(eap->arg + 1);
4786 }
4787}
4788
4789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 * Function called for command which is Not Implemented. NI!
4791 */
4792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004793ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
4795 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004796 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004797 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798}
4799
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004800#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801/*
4802 * Function called for script command which is Not Implemented. NI!
4803 * Skips over ":perl <<EOF" constructs.
4804 */
4805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004806ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807{
4808 if (!eap->skip)
4809 ex_ni(eap);
4810 else
4811 vim_free(script_get(eap, eap->arg));
4812}
4813#endif
4814
4815/*
4816 * Check range in Ex command for validity.
4817 * Return NULL when valid, error message when invalid.
4818 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004819 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004820invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004822 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if ( eap->line1 < 0
4825 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004826 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004827 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004828
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004829 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004830 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004831 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004832 {
4833 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004834 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004835#ifdef FEAT_DIFF
4836 + (eap->cmdidx == CMD_diffget)
4837#endif
4838 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004839 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004840 break;
4841 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004842 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004843 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004844 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004845 break;
4846 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004847 // Only a boundary check, not whether the buffers actually
4848 // exist.
4849 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004850 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004851 break;
4852 case ADDR_LOADED_BUFFERS:
4853 buf = firstbuf;
4854 while (buf->b_ml.ml_mfp == NULL)
4855 {
4856 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004857 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004858 buf = buf->b_next;
4859 }
4860 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004861 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004862 buf = lastbuf;
4863 while (buf->b_ml.ml_mfp == NULL)
4864 {
4865 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004866 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004867 buf = buf->b_prev;
4868 }
4869 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004870 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004871 break;
4872 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004873 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004874 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004875 break;
4876 case ADDR_TABS:
4877 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004878 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004879 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004880 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004881 case ADDR_OTHER:
4882 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004883 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004884 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004885#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004886 // No error for value that is too big, will use the last entry.
4887 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004888 {
4889 if (eap->addr_count == 0)
4890 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004891 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004892 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004893#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004894 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004895 case ADDR_QUICKFIX_VALID:
4896#ifdef FEAT_QUICKFIX
4897 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4898 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004899 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004900#endif
4901 break;
4902 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004903 case ADDR_NONE:
4904 // Will give an error elsewhere.
4905 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004906 }
4907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 return NULL;
4909}
4910
4911/*
4912 * Correct the range for zero line number, if required.
4913 */
4914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004915correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004917 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
4919 if (eap->line1 == 0)
4920 eap->line1 = 1;
4921 if (eap->line2 == 0)
4922 eap->line2 = 1;
4923 }
4924}
4925
Bram Moolenaar748bf032005-02-02 23:04:36 +00004926#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004927/*
4928 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4929 * pattern. Otherwise return eap->arg.
4930 */
4931 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004932skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004933{
4934 char_u *p = eap->arg;
4935
Bram Moolenaara37420f2006-02-04 22:37:47 +00004936 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4937 || eap->cmdidx == CMD_vimgrepadd
4938 || eap->cmdidx == CMD_lvimgrepadd
4939 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004940 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004941 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004942 if (p == NULL)
4943 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004944 }
4945 return p;
4946}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004947
4948/*
4949 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4950 * in the command line, so that things like % get expanded.
4951 */
4952 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004953replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004954{
4955 char_u *new_cmdline;
4956 char_u *program;
4957 char_u *pos;
4958 char_u *ptr;
4959 int len;
4960 int i;
4961
4962 /*
4963 * Don't do it when ":vimgrep" is used for ":grep".
4964 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004965 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4966 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4967 || eap->cmdidx == CMD_grepadd
4968 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004969 && !grep_internal(eap->cmdidx))
4970 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004971 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4972 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004973 {
4974 if (*curbuf->b_p_gp == NUL)
4975 program = p_gp;
4976 else
4977 program = curbuf->b_p_gp;
4978 }
4979 else
4980 {
4981 if (*curbuf->b_p_mp == NUL)
4982 program = p_mp;
4983 else
4984 program = curbuf->b_p_mp;
4985 }
4986
4987 p = skipwhite(p);
4988
4989 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4990 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004991 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004992 i = 1;
4993 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4994 ++i;
4995 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004996 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004997 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004998 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004999 ptr = new_cmdline;
5000 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5001 {
5002 i = (int)(pos - program);
5003 STRNCPY(ptr, program, i);
5004 STRCPY(ptr += i, p);
5005 ptr += len;
5006 program = pos + 2;
5007 }
5008 STRCPY(ptr, program);
5009 }
5010 else
5011 {
John Marriotted908f72024-04-18 22:46:56 +02005012 size_t program_len = STRLEN(program);
5013
5014 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005015 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005016 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005017 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005018 STRCPY(new_cmdline + program_len, " ");
5019 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005020 }
5021 msg_make(p);
5022
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005023 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005024 vim_free(*cmdlinep);
5025 *cmdlinep = new_cmdline;
5026 p = new_cmdline;
5027 }
5028 return p;
5029}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005030#endif
5031
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032/*
5033 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005034 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 * Return FAIL for failure, OK otherwise.
5036 */
5037 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005038expand_filename(
5039 exarg_T *eap,
5040 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005041 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005043 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005045 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 char_u *p;
5047 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005048 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
Bram Moolenaar748bf032005-02-02 23:04:36 +00005050#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005051 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005052 p = skip_grep_pat(eap);
5053#else
5054 p = eap->arg;
5055#endif
5056
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 /*
5058 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5059 * the file name contains a wildcard it should not cause expanding.
5060 * (it will be expanded anyway if there is a wildcard before replacing).
5061 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005062 has_wildcards = mch_has_wildcard(p);
5063 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005065#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005066 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005067 if (p[0] == '`' && p[1] == '=')
5068 {
5069 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005070 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005071 if (*p == '`')
5072 ++p;
5073 continue;
5074 }
5075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 /*
5077 * Quick check if this cannot be the start of a special string.
5078 * Also removes backslash before '%', '#' and '<'.
5079 */
5080 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5081 {
5082 ++p;
5083 continue;
5084 }
5085
5086 /*
5087 * Try to find a match at this position.
5088 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005089 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005090 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005091 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005093 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 p += srclen;
5096 continue;
5097 }
5098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005099 // Wildcards won't be expanded below, the replacement is taken
5100 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005101 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5102 {
5103 char_u *l = repl;
5104
5105 repl = expand_env_save(repl);
5106 vim_free(l);
5107 }
5108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 // Need to escape white space et al. with a backslash.
5110 // Don't do this for:
5111 // - replacement that already has been escaped: "##"
5112 // - shell commands (may have to use quotes instead).
5113 // - non-unix systems when there is a single argument (spaces don't
5114 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005116 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 && eap->cmdidx != CMD_grep
5119 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005120 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005121 && eap->cmdidx != CMD_lgrep
5122 && eap->cmdidx != CMD_lgrepadd
5123 && eap->cmdidx != CMD_lmake
5124 && eap->cmdidx != CMD_make
5125 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005127 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128#endif
5129 )
5130 {
5131 char_u *l;
5132#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005133 // Don't escape a backslash here, because rem_backslash() doesn't
5134 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 static char_u *nobslash = (char_u *)" \t\"|";
5136# define ESCAPE_CHARS nobslash
5137#else
5138# define ESCAPE_CHARS escape_chars
5139#endif
5140
5141 for (l = repl; *l; ++l)
5142 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5143 {
5144 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5145 if (l != NULL)
5146 {
5147 vim_free(repl);
5148 repl = l;
5149 }
5150 break;
5151 }
5152 }
5153
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005154 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005155 if ((eap->usefilter || eap->cmdidx == CMD_bang
5156 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005157 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
5159 char_u *l;
5160
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005161 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 if (l != NULL)
5163 {
5164 vim_free(repl);
5165 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 }
5168
Mike Williams51024bb2024-05-30 07:46:30 +02005169 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 vim_free(repl);
5171 if (p == NULL)
5172 return FAIL;
5173 }
5174
5175 /*
5176 * One file argument: Expand wildcards.
5177 * Don't do this with ":r !command" or ":w !command".
5178 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005179 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
5181 /*
5182 * May do this twice:
5183 * 1. Replace environment variables.
5184 * 2. Replace any other wildcards, remove backslashes.
5185 */
5186 for (n = 1; n <= 2; ++n)
5187 {
5188 if (n == 2)
5189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 /*
5191 * Halve the number of backslashes (this is Vi compatible).
5192 * For Unix and OS/2, when wildcards are expanded, this is
5193 * done by ExpandOne() below.
5194 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005195#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (!has_wildcards)
5197#endif
5198 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200
5201 if (has_wildcards)
5202 {
5203 if (n == 1)
5204 {
5205 /*
5206 * First loop: May expand environment variables. This
5207 * can be done much faster with expand_env() than with
5208 * something else (e.g., calling a shell).
5209 * After expanding environment variables, check again
5210 * if there are still wildcards present.
5211 */
5212 if (vim_strchr(eap->arg, '$') != NULL
5213 || vim_strchr(eap->arg, '~') != NULL)
5214 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005215 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005216 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 has_wildcards = mch_has_wildcard(NameBuff);
5218 p = NameBuff;
5219 }
5220 else
5221 p = NULL;
5222 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005223 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
5225 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005226 int options = WILD_LIST_NOTFOUND
5227 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
5229 ExpandInit(&xpc);
5230 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005231 if (p_wic)
5232 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005234 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (p == NULL)
5236 return FAIL;
5237 }
5238 if (p != NULL)
5239 {
John Marriotted908f72024-04-18 22:46:56 +02005240 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005242 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 vim_free(p);
5244 }
5245 }
5246 }
5247 }
5248 return OK;
5249}
5250
5251/*
5252 * Replace part of the command line, keeping eap->cmd, eap->arg and
5253 * eap->nextcmd correct.
5254 * "src" points to the part that is to be replaced, of length "srclen".
5255 * "repl" is the replacement string.
5256 * Returns a pointer to the character after the replaced string.
5257 * Returns NULL for failure.
5258 */
5259 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005260repl_cmdline(
5261 exarg_T *eap,
5262 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005263 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005264 char_u *repl,
5265 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
John Marriotted908f72024-04-18 22:46:56 +02005267 size_t repllen;
5268 size_t taillen;
5269 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005271 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 /*
5274 * The new command line is build in new_cmdline[].
5275 * First allocate it.
5276 * Careful: a "+cmd" argument may have been NUL terminated.
5277 */
John Marriotted908f72024-04-18 22:46:56 +02005278 repllen = STRLEN(repl);
5279 taillen = STRLEN(src + srclen);
5280 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005281 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005282 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005283 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005284 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 /*
5287 * Copy the stuff before the expanded part.
5288 * Copy the expanded stuff.
5289 * Copy what came after the expanded part.
5290 * Copy the next commands, if there are any.
5291 */
John Marriotted908f72024-04-18 22:46:56 +02005292 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5293 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005294
John Marriotted908f72024-04-18 22:46:56 +02005295 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5296 new_cmdlinelen += repllen; // remember the end of the string
5297 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5298 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005300 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
John Marriotted908f72024-04-18 22:46:56 +02005302 new_cmdlinelen += taillen + 1;
5303 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5304 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5307 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5308 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5309 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5310 vim_free(*cmdlinep);
5311 *cmdlinep = new_cmdline;
5312
5313 return src;
5314}
5315
5316/*
5317 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005318 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 */
5320 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005321separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322{
5323 char_u *p;
5324
Bram Moolenaar86b68352004-12-27 21:59:20 +00005325#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005326 p = skip_grep_pat(eap);
5327#else
5328 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005329#endif
5330
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005331 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
5333 if (*p == Ctrl_V)
5334 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005335 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005338 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005339 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005340 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 break;
5342 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005343
5344#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005345 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005346 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005347 {
5348 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005349 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005350 if (*p == NUL) // stop at NUL after CTRL-V
5351 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005352 }
5353#endif
5354
Bram Moolenaareda29c92022-10-02 12:59:00 +01005355 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005356 // :@" and :*" do not start a comment!
5357 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005358 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005359 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005360 && !(eap->argt & EX_NOTRLCOM)
5361 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5362 || p != eap->arg)
5363 && (eap->cmdidx != CMD_redir
5364 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005365 || (*p == '#'
5366 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005367 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005368 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005369 || (*p == '|'
5370 && eap->cmdidx != CMD_append
5371 && eap->cmdidx != CMD_change
5372 && eap->cmdidx != CMD_insert)
5373 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
5375 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005376 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 * AND 'b' is present in 'cpoptions'.
5378 */
5379 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005380 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005382 if (!keep_backslash)
5383 {
5384 STRMOVE(p - 1, p); // remove the '\'
5385 --p;
5386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 else
5389 {
5390 eap->nextcmd = check_nextcmd(p);
5391 *p = NUL;
5392 break;
5393 }
5394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005397 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 del_trailing_spaces(eap->arg);
5399}
5400
5401/*
5402 * get + command from ex argument
5403 */
5404 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005405getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 char_u *arg = *argp;
5408 char_u *command = NULL;
5409
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005410 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
5412 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005413 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 command = dollar_command;
5415 else
5416 {
5417 command = arg;
5418 arg = skip_cmd_arg(command, TRUE);
5419 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005420 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
5422
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005423 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 *argp = arg;
5425 }
5426 return command;
5427}
5428
5429/*
5430 * Find end of "+command" argument. Skip over "\ " and "\\".
5431 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005432 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005433skip_cmd_arg(
5434 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 while (*p && !vim_isspace(*p))
5438 {
5439 if (*p == '\\' && p[1] != NUL)
5440 {
5441 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005442 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 else
5444 ++p;
5445 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005446 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 }
5448 return p;
5449}
5450
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005451 int
5452get_bad_opt(char_u *p, exarg_T *eap)
5453{
5454 if (STRICMP(p, "keep") == 0)
5455 eap->bad_char = BAD_KEEP;
5456 else if (STRICMP(p, "drop") == 0)
5457 eap->bad_char = BAD_DROP;
5458 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5459 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005460 else
5461 return FAIL;
5462 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005463}
5464
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005466 * Function given to ExpandGeneric() to obtain the list of bad= names.
5467 */
5468 static char_u *
5469get_bad_name(expand_T *xp UNUSED, int idx)
5470{
5471 // Note: Keep this in sync with getargopt.
5472 static char *(p_bad_values[]) =
5473 {
5474 "?",
5475 "keep",
5476 "drop",
5477 };
5478
John Marriotted908f72024-04-18 22:46:56 +02005479 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5480 return NULL;
5481
5482 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005483}
5484
5485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 * Get "++opt=arg" argument.
5487 * Return FAIL or OK.
5488 */
5489 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005490getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
5492 char_u *arg = eap->arg + 2;
5493 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005494 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005497 // Note: Keep this in sync with get_argopt_name.
5498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005499 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5501 {
5502 if (*arg == 'n')
5503 {
5504 arg += 2;
5505 eap->force_bin = FORCE_NOBIN;
5506 }
5507 else
5508 eap->force_bin = FORCE_BIN;
5509 if (!checkforcmd(&arg, "binary", 3))
5510 return FAIL;
5511 eap->arg = skipwhite(arg);
5512 return OK;
5513 }
5514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005515 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005516 if (STRNCMP(arg, "edit", 4) == 0)
5517 {
5518 eap->read_edit = TRUE;
5519 eap->arg = skipwhite(arg + 4);
5520 return OK;
5521 }
5522
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 if (STRNCMP(arg, "ff", 2) == 0)
5524 {
5525 arg += 2;
5526 pp = &eap->force_ff;
5527 }
5528 else if (STRNCMP(arg, "fileformat", 10) == 0)
5529 {
5530 arg += 10;
5531 pp = &eap->force_ff;
5532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 else if (STRNCMP(arg, "enc", 3) == 0)
5534 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005535 if (STRNCMP(arg, "encoding", 8) == 0)
5536 arg += 8;
5537 else
5538 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 pp = &eap->force_enc;
5540 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005541 else if (STRNCMP(arg, "bad", 3) == 0)
5542 {
5543 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005544 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547 if (pp == NULL || *arg != '=')
5548 return FAIL;
5549
5550 ++arg;
5551 *pp = (int)(arg - eap->cmd);
5552 arg = skip_cmd_arg(arg, FALSE);
5553 eap->arg = skipwhite(arg);
5554 *arg = NUL;
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (pp == &eap->force_ff)
5557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5559 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005560 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005562 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005564 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5566 *p = TOLOWER_ASC(*p);
5567 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005568 else
5569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005570 // Check ++bad= argument. Must be a single-byte character, "keep" or
5571 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005572 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005573 return FAIL;
5574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
5576 return OK;
5577}
5578
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005579/*
5580 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5581 */
5582 static char_u *
5583get_argopt_name(expand_T *xp UNUSED, int idx)
5584{
5585 // Note: Keep this in sync with getargopt.
5586 static char *(p_opt_values[]) =
5587 {
5588 "fileformat=",
5589 "encoding=",
5590 "binary",
5591 "nobinary",
5592 "bad=",
5593 "edit",
5594 };
5595
John Marriotted908f72024-04-18 22:46:56 +02005596 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5597 return NULL;
5598
5599 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005600}
5601
5602/*
5603 * Command-line expansion for ++opt=name.
5604 */
5605 int
5606expand_argopt(
5607 char_u *pat,
5608 expand_T *xp,
5609 regmatch_T *rmp,
5610 char_u ***matches,
5611 int *numMatches)
5612{
5613 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5614 {
5615 char_u *(*cb)(expand_T *, int) = NULL;
5616
5617 char_u *name_end = xp->xp_pattern - 1;
5618 if (name_end - xp->xp_line >= 2
5619 && STRNCMP(name_end - 2, "ff", 2) == 0)
5620 cb = get_fileformat_name;
5621 else if (name_end - xp->xp_line >= 10
5622 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5623 cb = get_fileformat_name;
5624 else if (name_end - xp->xp_line >= 3
5625 && STRNCMP(name_end - 3, "enc", 3) == 0)
5626 cb = get_encoding_name;
5627 else if (name_end - xp->xp_line >= 8
5628 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5629 cb = get_encoding_name;
5630 else if (name_end - xp->xp_line >= 3
5631 && STRNCMP(name_end - 3, "bad", 3) == 0)
5632 cb = get_bad_name;
5633
5634 if (cb != NULL)
5635 {
5636 return ExpandGeneric(
5637 pat,
5638 xp,
5639 rmp,
5640 matches,
5641 numMatches,
5642 cb,
5643 FALSE);
5644 }
5645 return FAIL;
5646 }
5647
5648 // Special handling of "ff" which acts as a short form of
5649 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005650 if (xp->xp_pattern_len == 2
5651 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005652 {
5653 *matches = ALLOC_MULT(char_u *, 1);
5654 if (*matches == NULL)
5655 return FAIL;
5656 *numMatches = 1;
5657 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5658 return OK;
5659 }
5660
5661 return ExpandGeneric(
5662 pat,
5663 xp,
5664 rmp,
5665 matches,
5666 numMatches,
5667 get_argopt_name,
5668 FALSE);
5669}
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
5674 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005675 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 * directory for security reasons.
5677 */
5678 if (secure)
5679 {
5680 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005681 eap->errmsg =
5682 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005685 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 else
5687 do_augroup(eap->arg, eap->forceit);
5688}
5689
5690/*
5691 * ":doautocmd": Apply the automatic commands to the current buffer.
5692 */
5693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005694ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005696 char_u *arg = eap->arg;
5697 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005698 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005699
Bram Moolenaar1610d052016-06-09 22:53:01 +02005700 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005701 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005702 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005703 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706/*
5707 * :[N]bunload[!] [N] [bufname] unload buffer
5708 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5709 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5710 */
5711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005712ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005714 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005715 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 eap->errmsg = do_bufdel(
5717 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5718 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5719 : DOBUF_UNLOAD, eap->arg,
5720 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5721}
5722
5723/*
5724 * :[N]buffer [N] to buffer N
5725 * :[N]sbuffer [N] to buffer N
5726 */
5727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005728ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005730 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005731 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005733 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 else
5735 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005736 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5738 else
5739 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005740 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005741 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743}
5744
5745/*
5746 * :[N]bmodified [N] to next mod. buffer
5747 * :[N]sbmodified [N] to next mod. buffer
5748 */
5749 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005750ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751{
5752 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005753 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005754 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755}
5756
5757/*
5758 * :[N]bnext [N] to next buffer
5759 * :[N]sbnext [N] split and to next buffer
5760 */
5761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005762ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005764 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005765 return;
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005768 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005769 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770}
5771
5772/*
5773 * :[N]bNext [N] to previous buffer
5774 * :[N]bprevious [N] to previous buffer
5775 * :[N]sbNext [N] split and to previous buffer
5776 * :[N]sbprevious [N] split and to previous buffer
5777 */
5778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005779ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005781 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005782 return;
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005785 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005786 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787}
5788
5789/*
5790 * :brewind to first buffer
5791 * :bfirst to first buffer
5792 * :sbrewind split and to first buffer
5793 * :sbfirst split and to first buffer
5794 */
5795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005796ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005798 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005799 return;
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005802 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005803 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804}
5805
5806/*
5807 * :blast to last buffer
5808 * :sblast split and to last buffer
5809 */
5810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005811ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005813 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005814 return;
5815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005817 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005818 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
Bram Moolenaar7a092242020-04-16 22:10:49 +02005821/*
5822 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005823 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005826ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005828 int comment_char = '"';
5829
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005830 if (in_vim9script())
5831 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005832 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005833}
5834
5835/*
5836 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5837 * to "cmd_start" or has a white space character before it.
5838 */
5839 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005840ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005841{
5842 int c = *cmd;
5843
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005844 if (c == NUL || c == '|' || c == '\n')
5845 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005846 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005847 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5848 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005849 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005850 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851}
5852
5853#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5854 || defined(PROTO)
5855/*
5856 * Return the next command, after the first '|' or '\n'.
5857 * Return NULL if not found.
5858 */
5859 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005860find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
5862 while (*p != '|' && *p != '\n')
5863 {
5864 if (*p == NUL)
5865 return NULL;
5866 ++p;
5867 }
5868 return (p + 1);
5869}
5870#endif
5871
5872/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005873 * Check if *p is a separator between Ex commands, skipping over white space.
5874 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 */
5876 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005877check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005879 char_u *s = skipwhite(p);
5880
5881 if (*s == '|' || *s == '\n')
5882 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 else
5884 return NULL;
5885}
5886
5887/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005888 * If "eap->nextcmd" is not set, check for a next command at "p".
5889 */
5890 void
5891set_nextcmd(exarg_T *eap, char_u *arg)
5892{
5893 char_u *p = check_nextcmd(arg);
5894
5895 if (eap->nextcmd == NULL)
5896 eap->nextcmd = p;
5897 else if (p != NULL)
5898 // cannot use "| command" inside a {} block
5899 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5900}
5901
5902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 * - if there are more files to edit
5904 * - and this is the last window
5905 * - and forceit not used
5906 * - and not repeated twice on a row
5907 * return FAIL and give error message if 'message' TRUE
5908 * return OK otherwise
5909 */
5910 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005911check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005912 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005913 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914{
5915 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5916
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005917 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005918 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {
5920 if (message)
5921 {
5922#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005923 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5924 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005926 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005928 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5929 NGETTEXT("%d more file to edit. Quit anyway?",
5930 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5932 return OK;
5933 return FAIL;
5934 }
5935#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005936 semsg(NGETTEXT(e_nr_more_file_to_edit,
5937 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005938 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 }
5940 return FAIL;
5941 }
5942 return OK;
5943}
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945/*
5946 * Function given to ExpandGeneric() to obtain the list of command names.
5947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005949get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005952 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005953 // the following are no real commands
5954 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
5955 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
5956 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 return cmdnames[idx].cmd_name;
5958}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005961ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
Bram Moolenaare6850792010-05-14 15:28:44 +02005963 if (*eap->arg == NUL)
5964 {
5965#ifdef FEAT_EVAL
5966 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5967 char_u *p = NULL;
5968
5969 if (expr != NULL)
5970 {
5971 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005972 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005973 --emsg_off;
5974 vim_free(expr);
5975 }
5976 if (p != NULL)
5977 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005978 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005979 vim_free(p);
5980 }
5981 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005982 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005983#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005984 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005985#endif
5986 }
5987 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005988 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005989
5990#ifdef FEAT_VTP
5991 else if (has_vtp_working())
5992 {
5993 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005994 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005995 redrawcmd();
5996 }
5997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998}
5999
6000 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006001ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006004 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 do_highlight(eap->arg, eap->forceit, FALSE);
6006}
6007
6008
6009/*
6010 * Call this function if we thought we were going to exit, but we won't
6011 * (because of an error). May need to restore the terminal mode.
6012 */
6013 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006014not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
6016 exiting = FALSE;
6017 settmode(TMODE_RAW);
6018}
6019
Bram Moolenaar3552e742021-05-29 12:21:58 +02006020 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006021before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6022{
6023 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6024
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006025 // Bail out when autocommands closed the window.
6026 // Refuse to quit when the buffer in the last window is being closed (can
6027 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006028 if (!win_valid(wp)
6029 || curbuf_locked()
6030 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6031 return TRUE;
6032
6033 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6034 {
6035 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006036 // Refuse to quit when locked or when the window was closed or the
6037 // buffer in the last window is being closed (can only happen in
6038 // autocommands).
6039 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006040 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6041 return TRUE;
6042 }
6043
6044 return FALSE;
6045}
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006048 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006049 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006050 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006053ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006055 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006056
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 if (cmdwin_type != 0)
6058 {
6059 cmdwin_result = Ctrl_C;
6060 return;
6061 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006062 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006063 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006064 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006065 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006066 return;
6067 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006068 if (eap->addr_count > 0)
6069 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006070 int wnr = eap->line2;
6071
6072 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6073 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006074 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006075 }
6076 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006077 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006079 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006080 if (curbuf_locked())
6081 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006082
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006083 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006084 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006085 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
6087#ifdef FEAT_NETBEANS_INTG
6088 netbeansForcedQuit = eap->forceit;
6089#endif
6090
6091 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006092 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 */
6094 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6095 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006096 if ((!buf_hide(wp->w_buffer)
6097 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006098 | (eap->forceit ? CCGD_FORCEIT : 0)
6099 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006101 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 {
6103 not_exiting();
6104 }
6105 else
6106 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006107 // quit last window
6108 // Note: only_one_window() returns true, even so a help window is
6109 // still open. In that case only quit, if no address has been
6110 // specified. Example:
6111 // :h|wincmd w|1q - don't quit
6112 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006113 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006115 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006116#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006118#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006119 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006120 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 }
6122}
6123
6124/*
6125 * ":cquit".
6126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006128ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006130 // this does not always pass on the exit code to the Manx compiler. why?
6131 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132}
6133
6134/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006135 * Do preparations for "qall" and "wqall".
6136 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006138 int
6139before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (cmdwin_type != 0)
6142 {
6143 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006144 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 else
6146 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006147 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006150 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006151 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006152 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006153 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006154 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006155 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006156
6157 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006158 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006159
Bram Moolenaar411da642023-05-10 16:53:27 +01006160 return OK;
6161}
6162
6163/*
6164 * ":qall": try to quit all windows
6165 */
6166 static void
6167ex_quit_all(exarg_T *eap)
6168{
6169 if (before_quit_all(eap) == FAIL)
6170 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006172 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 getout(0);
6174 not_exiting();
6175}
6176
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177/*
6178 * ":close": close current window, unless it is the last one
6179 */
6180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006181ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006183 win_T *win;
6184 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006186 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006188 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006189 {
6190 if (eap->addr_count == 0)
6191 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006192 else
6193 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006194 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006195 {
6196 winnr++;
6197 if (winnr == eap->line2)
6198 break;
6199 }
6200 if (win == NULL)
6201 win = lastwin;
6202 ex_win_close(eap->forceit, win, NULL);
6203 }
6204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205}
6206
Bram Moolenaar4033c552017-09-16 20:54:51 +02006207#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006208/*
6209 * ":pclose": Close any preview window.
6210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006212ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213{
6214 win_T *win;
6215
Bram Moolenaar79648732019-07-18 21:43:07 +02006216 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006217 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006218 if (win->w_p_pvw)
6219 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006220 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006221 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006223# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006224 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006225 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006226# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006228#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229
Bram Moolenaarf740b292006-02-16 22:11:02 +00006230/*
6231 * Close window "win" and take care of handling closing the last window for a
6232 * modified buffer.
6233 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006235ex_win_close(
6236 int forceit,
6237 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006238 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
6240 int need_hide;
6241 buf_T *buf = win->w_buffer;
6242
Bram Moolenaarcf844172020-06-26 19:44:06 +02006243 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006244 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006245 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006246 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006247 return;
6248 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006249 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006250 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006253 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006255#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006256 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006258# ifdef FEAT_TERMINAL
6259 if (term_job_running(buf->b_term))
6260 {
6261 if (term_confirm_stop(buf) == FAIL)
6262 return;
6263 // Manually kill the terminal here because this command will
6264 // hide it otherwise.
6265 free_terminal(buf);
6266 need_hide = FALSE;
6267 }
6268 else
6269# endif
6270 {
6271 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006272
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006273 set_bufref(&bufref, buf);
6274 dialog_changed(buf, FALSE);
6275 if (bufref_valid(&bufref) && bufIsChanged(buf))
6276 return;
6277 need_hide = FALSE;
6278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
6280 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006283 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 return;
6285 }
6286 }
6287
Bram Moolenaar4033c552017-09-16 20:54:51 +02006288#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006290#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006291
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006292 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006293 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006294 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006295 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006296 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297}
6298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006300 * Handle the argument for a tabpage related ex command.
6301 * Returns a tabpage number.
6302 * When an error is encountered then eap->errmsg is set.
6303 */
6304 static int
6305get_tabpage_arg(exarg_T *eap)
6306{
6307 int tab_number;
6308 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6309
6310 if (eap->arg && *eap->arg != NUL)
6311 {
6312 char_u *p = eap->arg;
6313 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006314 int relative = 0; // argument +N/-N means: go to N places to the
6315 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006316
6317 if (*p == '-')
6318 {
6319 relative = -1;
6320 p++;
6321 }
6322 else if (*p == '+')
6323 {
6324 relative = 1;
6325 p++;
6326 }
6327
6328 p_save = p;
6329 tab_number = getdigits(&p);
6330
6331 if (relative == 0)
6332 {
6333 if (STRCMP(p, "$") == 0)
6334 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006335 else if (STRCMP(p, "#") == 0)
6336 if (valid_tabpage(lastused_tabpage))
6337 tab_number = tabpage_index(lastused_tabpage);
6338 else
6339 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006340 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6341 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006342 tab_number = 0;
6343 goto theend;
6344 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006345 else if (p == p_save || *p_save == '-' || *p != NUL
6346 || tab_number > LAST_TAB_NR)
6347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006348 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006349 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006350 goto theend;
6351 }
6352 }
6353 else
6354 {
6355 if (*p_save == NUL)
6356 tab_number = 1;
6357 else if (p == p_save || *p_save == '-' || *p != NUL
6358 || tab_number == 0)
6359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006360 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006361 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006362 goto theend;
6363 }
6364 tab_number = tab_number * relative + tabpage_index(curtab);
6365 if (!unaccept_arg0 && relative == -1)
6366 --tab_number;
6367 }
6368 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006369 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006370 }
6371 else if (eap->addr_count > 0)
6372 {
6373 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006374 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006375 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006376 tab_number = 0;
6377 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006378 else
6379 {
6380 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006381 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006382 {
zeertzjq076faac2024-03-25 16:41:06 +01006383 char_u *cmdp = eap->cmd;
6384
6385 while (--cmdp > *eap->cmdlinep
6386 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6387 ;
6388 if (*cmdp == '-')
6389 {
6390 --tab_number;
6391 if (tab_number < unaccept_arg0)
6392 eap->errmsg = _(e_invalid_range);
6393 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006394 }
6395 }
6396 }
6397 else
6398 {
6399 switch (eap->cmdidx)
6400 {
6401 case CMD_tabnext:
6402 tab_number = tabpage_index(curtab) + 1;
6403 if (tab_number > LAST_TAB_NR)
6404 tab_number = 1;
6405 break;
6406 case CMD_tabmove:
6407 tab_number = LAST_TAB_NR;
6408 break;
6409 default:
6410 tab_number = tabpage_index(curtab);
6411 }
6412 }
6413
6414theend:
6415 return tab_number;
6416}
6417
6418/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006419 * ":tabclose": close current tab page, unless it is the last one.
6420 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 */
6422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006423ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006425 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006426 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006427
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006428 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006429 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006430 cmdwin_result = K_IGNORE;
6431 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006432 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006433
6434 if (first_tabpage->tp_next == NULL)
6435 {
6436 emsg(_(e_cannot_close_last_tab_page));
6437 return;
6438 }
6439
6440 if (window_layout_locked(CMD_tabclose))
6441 return;
6442
6443 tab_number = get_tabpage_arg(eap);
6444 if (eap->errmsg != NULL)
6445 return;
6446
6447 tp = find_tabpage(tab_number);
6448 if (tp == NULL)
6449 {
6450 beep_flush();
6451 return;
6452 }
6453 if (tp != curtab)
6454 {
6455 tabpage_close_other(tp, eap->forceit);
6456 return;
6457 }
6458 else if (!text_locked() && !curbuf_locked())
6459 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006460}
6461
6462/*
6463 * ":tabonly": close all tab pages except the current one
6464 */
6465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006466ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006467{
6468 tabpage_T *tp;
6469 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006470 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006471
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006472 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006473 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006474 cmdwin_result = K_IGNORE;
6475 return;
6476 }
6477
6478 if (first_tabpage->tp_next == NULL)
6479 {
6480 msg(_("Already only one tab page"));
6481 return;
6482 }
6483
6484 if (window_layout_locked(CMD_tabonly))
6485 return;
6486
6487 tab_number = get_tabpage_arg(eap);
6488 if (eap->errmsg != NULL)
6489 return;
6490
6491 goto_tabpage(tab_number);
6492 // Repeat this up to a 1000 times, because autocommands may
6493 // mess up the lists.
6494 for (done = 0; done < 1000; ++done)
6495 {
6496 FOR_ALL_TABPAGES(tp)
6497 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006498 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006499 tabpage_close_other(tp, eap->forceit);
6500 // if we failed to close it quit
6501 if (valid_tabpage(tp))
6502 done = 1000;
6503 // start over, "tp" is now invalid
6504 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006505 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006506 if (first_tabpage->tp_next == NULL)
6507 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006512 * Close the current tab page.
6513 */
6514 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006515tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006516{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006517 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006518 return;
6519
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006520 // First close all the windows but the current one. If that worked then
6521 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006522 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006523 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006524 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006525 ex_win_close(forceit, curwin, NULL);
6526# ifdef FEAT_GUI
6527 need_mouse_correct = TRUE;
6528# endif
6529}
6530
6531/*
6532 * Close tab page "tp", which is not the current tab page.
6533 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006534 * Also takes care of the tab pages line disappearing when closing the
6535 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006536 */
6537 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006538tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006539{
6540 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006541 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006542
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006543 // Limit to 1000 windows, autocommands may add a window while we close
6544 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006545 while (++done < 1000)
6546 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006547 wp = tp->tp_firstwin;
6548 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006550 // Autocommands may delete the tab page under our fingers and we may
6551 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006552 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006553 break;
6554 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006555
Bram Moolenaar29323592016-07-24 22:04:11 +02006556 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006557}
6558
6559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 * ":only".
6561 */
6562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006563ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006565 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006566 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567# ifdef FEAT_GUI
6568 need_mouse_correct = TRUE;
6569# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006570 if (eap->addr_count > 0)
6571 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006572 win_T *wp;
6573 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006574 for (wp = firstwin; --wnr > 0; )
6575 {
6576 if (wp->w_next == NULL)
6577 break;
6578 else
6579 wp = wp->w_next;
6580 }
6581 win_goto(wp);
6582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 close_others(TRUE, eap->forceit);
6584}
6585
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006587ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006589 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006590 if (eap->skip)
6591 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006592
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006593 if (window_layout_locked(CMD_hide))
6594 return;
6595#ifdef FEAT_GUI
6596 need_mouse_correct = TRUE;
6597#endif
6598 if (eap->addr_count == 0)
6599 win_close(curwin, FALSE); // don't free buffer
6600 else
6601 {
6602 int winnr = 0;
6603 win_T *win;
6604
6605 FOR_ALL_WINDOWS(win)
6606 {
6607 winnr++;
6608 if (winnr == eap->line2)
6609 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006611 if (win == NULL)
6612 win = lastwin;
6613 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615}
6616
6617/*
6618 * ":stop" and ":suspend": Suspend Vim.
6619 */
dbivolaruab16ad32021-12-29 19:41:47 +00006620 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006621ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 /*
6624 * Disallow suspending for "rvim".
6625 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006626 if (check_restricted())
6627 return;
6628
6629 if (!eap->forceit)
6630 autowrite_all();
6631 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6632 windgoto((int)Rows - 1, 0);
6633 out_char('\n');
6634 out_flush();
6635 stoptermcap();
6636 out_flush(); // needed for SUN to restore xterm buffer
6637 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6638 ui_suspend(); // call machine specific function
6639 maketitle();
6640 resettitle(); // force updating the title
6641 starttermcap();
6642 scroll_start(); // scroll screen before redrawing
6643 redraw_later_clear();
6644 shell_resized(); // may have resized window
6645 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646}
6647
6648/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006649 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 */
6651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006652ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006654#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006655 if (not_in_vim9(eap) == FAIL)
6656 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 if (cmdwin_type != 0)
6659 {
6660 cmdwin_result = Ctrl_C;
6661 return;
6662 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006663 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006664 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006665 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006666 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006667 return;
6668 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006671 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 */
6673 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6674 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006675
6676 // Write the buffer for ":wq" or when it was changed.
6677 // Trigger QuitPre and ExitPre.
6678 // Check if we can exit now, after autocommands have changed things.
6679 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6680 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006682 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {
6684 not_exiting();
6685 }
6686 else
6687 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006688 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006690 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691# ifdef FEAT_GUI
6692 need_mouse_correct = TRUE;
6693# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006694 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006695 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 }
6697}
6698
6699/*
6700 * ":print", ":list", ":number".
6701 */
6702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006703ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006705 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006706 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006707 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006709 for ( ;!got_int; ui_breakcheck())
6710 {
6711 print_line(eap->line1,
6712 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6713 || (eap->flags & EXFLAG_NR)),
6714 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6715 if (++eap->line1 > eap->line2)
6716 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006717 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006718 }
6719 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006720 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006721 curwin->w_cursor.lnum = eap->line2;
6722 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 }
6724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726}
6727
6728#ifdef FEAT_BYTEOFF
6729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006730ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 goto_byte(eap->line2);
6733}
6734#endif
6735
6736/*
6737 * ":shell".
6738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006740ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742 do_shell(NULL, 0);
6743}
6744
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006745#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006747static int drop_busy = FALSE;
6748static int drop_filec;
6749static char_u **drop_filev = NULL;
6750static int drop_split;
6751static void (*drop_callback)(void *);
6752static void *drop_cookie;
6753
6754 static void
6755handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 exarg_T ea;
6758 int save_msg_scroll = msg_scroll;
6759
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006760 // Setting the argument list may cause screen updates and being called
6761 // recursively. Avoid that by setting drop_busy.
6762 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006764 // Check whether the current buffer is changed. If so, we will need
6765 // to split the current window or data could be lost.
6766 // We don't need to check if the 'hidden' option is set, as in this
6767 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006768 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
6770 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006771 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 --emsg_off;
6773 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006774 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 if (win_split(0, 0) == FAIL)
6777 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006778 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006780 // When splitting the window, create a new alist. Otherwise the
6781 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 alist_unlink(curwin->w_alist);
6783 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 }
6785
6786 /*
6787 * Set up the new argument list.
6788 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006789 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791 /*
6792 * Move to the first file.
6793 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006794 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006795 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 ea.cmd = (char_u *)"next";
6797 do_argfile(&ea, 0);
6798
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006799 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6800 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 need_start_insertmode = FALSE;
6802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006803 // Restore msg_scroll, otherwise a following command may cause scrolling
6804 // unexpectedly. The screen will be redrawn by the caller, thus
6805 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006807
6808 if (drop_callback != NULL)
6809 drop_callback(drop_cookie);
6810
6811 drop_filev = NULL;
6812 drop_busy = FALSE;
6813}
6814
6815/*
6816 * Handle a file drop. The code is here because a drop is *nearly* like an
6817 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006818 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006819 * code is very similar to :args and hence needs access to a lot of the static
6820 * functions in this file.
6821 *
6822 * The "filev" list must have been allocated using alloc(), as should each item
6823 * in the list. This function takes over responsibility for freeing the "filev"
6824 * list.
6825 */
6826 void
6827handle_drop(
6828 int filec, // the number of files dropped
6829 char_u **filev, // the list of files dropped
6830 int split, // force splitting the window
6831 void (*callback)(void *), // to be called after setting the argument
6832 // list
6833 void *cookie) // argument for "callback" (allocated)
6834{
6835 // Cannot handle recursive drops, finish the pending one.
6836 if (drop_busy)
6837 {
6838 FreeWild(filec, filev);
6839 vim_free(cookie);
6840 return;
6841 }
6842
6843 // When calling handle_drop() more than once in a row we only use the last
6844 // one.
6845 if (drop_filev != NULL)
6846 {
6847 FreeWild(drop_filec, drop_filev);
6848 vim_free(drop_cookie);
6849 }
6850
6851 drop_filec = filec;
6852 drop_filev = filev;
6853 drop_split = split;
6854 drop_callback = callback;
6855 drop_cookie = cookie;
6856
6857 // Postpone this when:
6858 // - editing the command line
6859 // - not possible to change the current buffer
6860 // - updating the screen
6861 // As it may change buffers and window structures that are in use and cause
6862 // freed memory to be used.
6863 if (text_locked() || curbuf_locked() || updating_screen)
6864 return;
6865
6866 handle_drop_internal();
6867}
6868
6869/*
6870 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6871 * reset: Handle a postponed drop.
6872 */
6873 void
6874handle_any_postponed_drop(void)
6875{
6876 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006877 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006878 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879}
6880#endif
6881
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 * ":preserve".
6884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006886ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006888 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 ml_preserve(curbuf, TRUE);
6890}
6891
6892/*
6893 * ":recover".
6894 */
6895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006896ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006898 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006900 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6901 | CCGD_MULTWIN
6902 | (eap->forceit ? CCGD_FORCEIT : 0)
6903 | CCGD_EXCMD)
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 && (*eap->arg == NUL
6906 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006907 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 recoverymode = FALSE;
6909}
6910
6911/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006912 * Command modifier used in a wrong way. Also for other commands that can't
6913 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 */
6915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006916ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006918 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919}
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921/*
6922 * :sview [+command] file split window with new file, read-only
6923 * :split [[+command] file] split window with current or new file
6924 * :vsplit [[+command] file] split window vertically with current or new file
6925 * :new [[+command] file] split window with no or new file
6926 * :vnew [[+command] file] split vertically window with no or new file
6927 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006928 *
6929 * :tabedit open new Tab page with empty window
6930 * :tabedit [+command] file open new Tab page and edit "file"
6931 * :tabnew [[+command] file] just like :tabedit
6932 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 */
6934 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006935ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006937 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006939#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006940 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006941 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006942#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006943 int use_tab = eap->cmdidx == CMD_tabedit
6944 || eap->cmdidx == CMD_tabfind
6945 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006947 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006948 return;
6949
Bram Moolenaar4033c552017-09-16 20:54:51 +02006950#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
Bram Moolenaar4033c552017-09-16 20:54:51 +02006954#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006955 // A ":split" in the quickfix window works like ":new". Don't want two
6956 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006957 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 {
6959 if (eap->cmdidx == CMD_split)
6960 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (eap->cmdidx == CMD_vsplit)
6962 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006966 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006968 char_u *file_to_find = NULL;
6969 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00006971 FNAME_MESS, TRUE, curbuf->b_ffname,
6972 &file_to_find, &search_ctx);
6973 vim_free(file_to_find);
6974 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 if (fname == NULL)
6976 goto theend;
6977 eap->arg = fname;
6978 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006979# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01006980 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 && eap->cmdidx != CMD_new)
6983 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006984 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006985# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006986 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006987# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006988 au_has_group((char_u *)"FileExplorer"))
6989 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006990 // No browsing supported but we do have the file explorer:
6991 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006992 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006993 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006994 }
6995 else
6996 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006997 fname = do_browse(0, (char_u *)(use_tab
6998 ? _("Edit File in new tab page")
6999 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007001 if (fname == NULL)
7002 goto theend;
7003 eap->arg = fname;
7004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007006 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007009 /*
7010 * Either open new tab page or split the window.
7011 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007012 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007013 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007014 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007015 : eap->addr_count == 0 ? 0
7016 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007017 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007018 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007020 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007021 if (curwin != old_curwin
7022 && win_valid(old_curwin)
7023 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007024 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007025 old_curwin->w_alt_fnum = curbuf->b_fnum;
7026 }
7027 }
7028 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7030 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007031 // Reset 'scrollbind' when editing another file, but keep it when
7032 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007033 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007034 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 else
7036 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 do_exedit(eap, old_curwin);
7038 }
7039
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007040# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007041 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044theend:
7045 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007047
7048/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007049 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007050 */
7051 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007052tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007053{
7054 exarg_T ea;
7055
Bram Moolenaara80faa82020-04-12 19:37:17 +02007056 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007057 ea.cmdidx = CMD_tabnew;
7058 ea.cmd = (char_u *)"tabn";
7059 ea.arg = (char_u *)"";
7060 ex_splitview(&ea);
7061}
7062
7063/*
7064 * :tabnext command
7065 */
7066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007067ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007068{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007069 int tab_number;
7070
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007071 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007072 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007073 switch (eap->cmdidx)
7074 {
7075 case CMD_tabfirst:
7076 case CMD_tabrewind:
7077 goto_tabpage(1);
7078 break;
7079 case CMD_tablast:
7080 goto_tabpage(9999);
7081 break;
7082 case CMD_tabprevious:
7083 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007084 if (eap->arg && *eap->arg != NUL)
7085 {
7086 char_u *p = eap->arg;
7087 char_u *p_save = p;
7088
7089 tab_number = getdigits(&p);
7090 if (p == p_save || *p_save == '-' || *p != NUL
7091 || tab_number == 0)
7092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007093 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007094 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007095 return;
7096 }
7097 }
7098 else
7099 {
7100 if (eap->addr_count == 0)
7101 tab_number = 1;
7102 else
7103 {
7104 tab_number = eap->line2;
7105 if (tab_number < 1)
7106 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007107 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007108 return;
7109 }
7110 }
7111 }
7112 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007113 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007114 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007115 tab_number = get_tabpage_arg(eap);
7116 if (eap->errmsg == NULL)
7117 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007118 break;
7119 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007120}
7121
7122/*
7123 * :tabmove command
7124 */
7125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007126ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007127{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007128 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007129
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007130 tab_number = get_tabpage_arg(eap);
7131 if (eap->errmsg == NULL)
7132 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007133}
7134
7135/*
7136 * :tabs command: List tabs and their contents.
7137 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007139ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007140{
7141 tabpage_T *tp;
7142 win_T *wp;
7143 int tabcount = 1;
7144
7145 msg_start();
7146 msg_scroll = TRUE;
7147 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7148 {
7149 msg_putchar('\n');
7150 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007151 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007152 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007153 ui_breakcheck();
7154
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007155 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007156 wp = firstwin;
7157 else
7158 wp = tp->tp_firstwin;
7159 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7160 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007161 msg_putchar('\n');
7162 msg_putchar(wp == curwin ? '>' : ' ');
7163 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007164 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7165 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007166 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007167 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007168 else
7169 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7170 IObuff, IOSIZE, TRUE);
7171 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007172 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007173 ui_breakcheck();
7174 }
7175 }
7176}
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178/*
7179 * ":mode": Set screen mode.
7180 * If no argument given, just get the screen size and redraw.
7181 */
7182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007183ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185 if (*eap->arg == NUL)
7186 shell_resized();
7187 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007188 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189}
7190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191/*
7192 * ":resize".
7193 * set, increment or decrement current window height
7194 */
7195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007196ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 int n;
7199 win_T *wp = curwin;
7200
7201 if (eap->addr_count > 0)
7202 {
7203 n = eap->line2;
7204 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7205 ;
7206 }
7207
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007208# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007212 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
7214 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007215 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007216 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007218 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
7220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
7222 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007223 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007224 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007226 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
7228}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
7230/*
7231 * ":find [+command] <file>" command.
7232 */
7233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007234ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
Colin Kennedy21570352024-03-03 16:16:47 +01007236 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007237 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 char_u *fname;
7240 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007241 char_u *file_to_find = NULL;
7242 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
7244 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007245 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 if (eap->addr_count > 0)
7247 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007248 // Repeat finding the file "count" times. This matters when it appears
7249 // several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 count = eap->line2;
7251 while (fname != NULL && --count > 0)
7252 {
7253 vim_free(fname);
7254 fname = find_file_in_path(NULL, 0, FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007255 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007258 VIM_CLEAR(file_to_find);
7259 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007261 if (fname == NULL)
7262 return;
7263
7264 eap->arg = fname;
7265 do_exedit(eap, NULL);
7266 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267}
7268
7269/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007270 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007271 */
7272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007273ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007274{
7275 regmatch_T regmatch;
7276 char_u *p;
7277
Bram Moolenaar65088802021-03-13 21:07:21 +01007278#ifdef FEAT_EVAL
7279 if (not_in_vim9(eap) == FAIL)
7280 return;
7281#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007282 curwin->w_cursor.lnum = eap->line2;
7283 beginline(BL_SOL | BL_FIX);
7284 if (*eap->arg == '/')
7285 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007286 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007287 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007288 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007289 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007290 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007291 if (regmatch.regprog != NULL)
7292 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007293 // make a copy of the line, when searching for a mark it might be
7294 // flushed
7295 char_u *line = vim_strsave(ml_get_curline());
7296
Bram Moolenaardf177f62005-02-22 08:39:57 +00007297 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007298 if (vim_regexec(&regmatch, line, (colnr_T)0))
7299 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007300 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007301 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007302 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007303 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007304 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007305 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007306 eap->arg += STRLEN(eap->arg);
7307 }
7308 check_cursor();
7309
7310 eap->cmdidx = CMD_visual;
7311 do_exedit(eap, NULL);
7312}
7313
7314/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007315 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 */
7317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007318ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007320 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7321
Colin Kennedy21570352024-03-03 16:16:47 +01007322 // Exclude commands which keep the window's current buffer
7323 if (
7324 eap->cmdidx != CMD_badd
7325 && eap->cmdidx != CMD_balt
7326 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007327 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7328 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007329 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 do_exedit(eap, NULL);
7332}
7333
7334/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007335 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007338do_exedit(
7339 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007340 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007344 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007346 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7347 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007348 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 /*
7350 * ":vi" command ends Ex mode.
7351 */
7352 if (exmode_active && (eap->cmdidx == CMD_visual
7353 || eap->cmdidx == CMD_view))
7354 {
7355 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007356 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007358 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007359 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007360 if (global_busy)
7361 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007362 if (eap->nextcmd != NULL)
7363 {
7364 stuffReadbuff(eap->nextcmd);
7365 eap->nextcmd = NULL;
7366 }
7367
7368 if (exmode_was != EXMODE_VIM)
7369 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007370 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007371 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007372 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007373 no_wait_return = 0;
7374 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007375 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007376 msg_scroll = 0;
7377#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007378 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007379 hold_gui_events = 0;
7380#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007381 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007382 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007383
7384 main_loop(FALSE, TRUE);
7385
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007386 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007387 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007388 no_wait_return = save_nwr;
7389 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007390#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007391 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007392#endif
7393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397
7398 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007399 || eap->cmdidx == CMD_tabnew
7400 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007401 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007403 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 setpcmark();
7405 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007406 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7407 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007409 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 || *eap->arg != NUL
7411#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007412 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413#endif
7414 )
7415 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007416 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7417 // Only ":edit" or ":script" can bring us here, others are stopped
7418 // earlier.
7419 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007420 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007421
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 n = readonlymode;
7423 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7424 readonlymode = TRUE;
7425 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007426 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7427 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007428 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7429 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7431 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007432 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7434 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7435 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007436 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007438 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007439 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007440 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7441 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007442 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007444 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 if (old_curwin != NULL)
7446 {
7447 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007448 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007450#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007451 cleanup_T cs;
7452
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007453 // Reset the error/interrupt/exception state here so that
7454 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007455 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007456#endif
7457#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007459#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007460 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007461
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007462#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007463 // Restore the error/interrupt/exception state if not
7464 // discarded by a new aborting error, interrupt, or
7465 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007466 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 }
7469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 }
7471 else if (readonlymode && curbuf->b_nwindows == 1)
7472 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007473 // When editing an already visited buffer, 'readonly' won't be set
7474 // but the previous value is kept. With ":view" and ":sview" we
7475 // want the file to be readonly, except when another window is
7476 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 curbuf->b_p_ro = TRUE;
7478 }
7479 readonlymode = n;
7480 }
7481 else
7482 {
7483 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007484 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 if (n != curwin->w_arg_idx_invalid)
7488 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
7490
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 /*
7492 * if ":split file" worked, set alternate file name in old window to new
7493 * file
7494 */
7495 if (old_curwin != NULL
7496 && *eap->arg != NUL
7497 && curwin != old_curwin
7498 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007499 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007500 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
7503 ex_no_reprint = TRUE;
7504}
7505
7506#ifndef FEAT_GUI
7507/*
7508 * ":gui" and ":gvim" when there is no GUI.
7509 */
7510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007511ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007513 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514}
7515#endif
7516
Bram Moolenaar4f974752019-02-17 17:44:42 +01007517#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007519ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 gui_make_tearoff(eap->arg);
7522}
7523#endif
7524
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007525#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7526 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007528ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007530# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7531 if (gui.in_use)
7532 gui_make_popup(eap->arg, eap->forceit);
7533# ifdef FEAT_TERM_POPUP_MENU
7534 else
7535# endif
7536# endif
7537# ifdef FEAT_TERM_POPUP_MENU
7538 pum_make_popup(eap->arg, eap->forceit);
7539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540}
7541#endif
7542
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007544ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545{
7546 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007547 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007549 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550}
7551
7552/*
7553 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7554 * offset.
7555 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007558ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007561 win_T *save_curwin = curwin;
7562 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 long topline;
7564 long y;
7565 linenr_T old_linenr = curwin->w_cursor.lnum;
7566
7567 setpcmark();
7568
7569 /*
7570 * determine max topline
7571 */
7572 if (curwin->w_p_scb)
7573 {
7574 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007575 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 {
7577 if (wp->w_p_scb && wp->w_buffer)
7578 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007579 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (topline > y)
7581 topline = y;
7582 }
7583 }
7584 if (topline < 1)
7585 topline = 1;
7586 }
7587 else
7588 {
7589 topline = 1;
7590 }
7591
7592
7593 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007594 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007596 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {
7598 if (curwin->w_p_scb)
7599 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007600 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 y = topline - curwin->w_topline;
7602 if (y > 0)
7603 scrollup(y, TRUE);
7604 else
7605 scrolldown(-y, TRUE);
7606 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007607 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 }
7611 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007612 curwin = save_curwin;
7613 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 if (curwin->w_p_scb)
7615 {
7616 did_syncbind = TRUE;
7617 checkpcmark();
7618 if (old_linenr != curwin->w_cursor.lnum)
7619 {
7620 char_u ctrl_o[2];
7621
7622 ctrl_o[0] = Ctrl_O;
7623 ctrl_o[1] = 0;
7624 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7625 }
7626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627}
7628
7629
7630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007631ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007633 int i;
7634 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7635 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007637 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007639 do_bang(1, eap, FALSE, FALSE, TRUE);
7640 return;
7641 }
7642
7643 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7644 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645
7646#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007647 if (cmdmod.cmod_flags & CMOD_BROWSE)
7648 {
7649 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007651 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7652 NULL, NULL, NULL, curbuf);
7653 if (browseFile != NULL)
7654 {
7655 i = readfile(browseFile, NULL,
7656 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7657 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007660 i = OK;
7661 }
7662 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007664 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007666 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 return;
7668 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007669 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
7671 else
7672 {
7673 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7674 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7675 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007676 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007679 if (i != OK)
7680 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007681#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007682 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007684 semsg(_(e_cant_open_file_str), eap->arg);
7685 }
7686 else
7687 {
7688 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007689 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007690 // Delete the empty line that remains. Historically ex does
7691 // this but vi doesn't.
7692 if (eap->line2 == 0)
7693 lnum = curbuf->b_ml.ml_line_count;
7694 else
7695 lnum = 1;
7696 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007697 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007698 ml_delete(lnum);
7699 if (curwin->w_cursor.lnum > 1
7700 && curwin->w_cursor.lnum >= lnum)
7701 --curwin->w_cursor.lnum;
7702 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007703 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007704 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007705 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 }
7707}
7708
Bram Moolenaarea408852005-06-25 22:49:46 +00007709static char_u *prev_dir = NULL;
7710
7711#if defined(EXITFREE) || defined(PROTO)
7712 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007713free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007714{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007715 VIM_CLEAR(prev_dir);
7716 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007717}
7718#endif
7719
Bram Moolenaarf4258302013-06-02 18:20:17 +02007720/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007721 * Get the previous directory for the given chdir scope.
7722 */
7723 static char_u *
7724get_prevdir(cdscope_T scope)
7725{
7726 if (scope == CDSCOPE_WINDOW)
7727 return curwin->w_prevdir;
7728 else if (scope == CDSCOPE_TABPAGE)
7729 return curtab->tp_prevdir;
7730 return prev_dir;
7731}
7732
7733/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007734 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007735 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7736 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007737 */
7738 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007739post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007740{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007741 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007742 // Clear tab local directory for both :cd and :tcd
7743 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007744 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007745 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007746 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007747 char_u *pdir = get_prevdir(scope);
7748
7749 // If still in the global directory, need to remember current
7750 // directory as the global directory.
7751 if (globaldir == NULL && pdir != NULL)
7752 globaldir = vim_strsave(pdir);
7753
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007754 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007755 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007756 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007757 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007758 curtab->tp_localdir = vim_strsave(NameBuff);
7759 else
7760 curwin->w_localdir = vim_strsave(NameBuff);
7761 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007762 }
7763 else
7764 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007765 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007766 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007767 }
7768
zeertzjq64be6aa2021-11-19 11:59:08 +00007769 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007770 shorten_fnames(TRUE);
7771}
7772
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007773/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007774 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7775 */
7776 void
7777trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7778{
7779#ifdef FEAT_EVAL
7780 dict_T *v_event;
7781 save_v_event_T save_v_event;
7782
7783 v_event = get_v_event(&save_v_event);
7784 (void)dict_add_string(v_event, "directory", new_dir);
7785 dict_set_items_ro(v_event);
7786#endif
7787 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7788#ifdef FEAT_EVAL
7789 restore_v_event(v_event, &save_v_event);
7790#endif
7791}
7792
7793/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007794 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007795 * chdir() function.
7796 * scope == CDSCOPE_WINDOW: changes the window-local directory
7797 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7798 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007799 * Returns TRUE if the directory is successfully changed.
7800 */
7801 int
7802changedir_func(
7803 char_u *new_dir,
7804 int forceit,
7805 cdscope_T scope)
7806{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007807 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007808 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007809 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007810 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007811 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007812
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007813 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007814 return FALSE;
7815
7816 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7817 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007818 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007819 return FALSE;
7820 }
7821
7822 // ":cd -": Change to previous directory
7823 if (STRCMP(new_dir, "-") == 0)
7824 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007825 pdir = get_prevdir(scope);
7826 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007827 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007828 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007829 return FALSE;
7830 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007831 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007832 }
7833
7834 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007835 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007836 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007837 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007838 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007839
Bakudankun29f3a452021-12-11 12:28:08 +00007840 // For UNIX ":cd" means: go to home directory.
7841 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007842#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007843 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007844#else
7845 if (*new_dir == NUL && p_cdh)
7846#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007847 {
7848 // use NameBuff for home directory name
7849# ifdef VMS
7850 char_u *p;
7851
7852 p = mch_getenv((char_u *)"SYS$LOGIN");
7853 if (p == NULL || *p == NUL) // empty is the same as not set
7854 NameBuff[0] = NUL;
7855 else
7856 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7857# else
7858 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7859# endif
7860 new_dir = NameBuff;
7861 }
zeertzjqf38aad82021-12-30 13:45:57 +00007862 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007863 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7864 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007865 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007866 if (scope == CDSCOPE_WINDOW)
7867 acmd_fname = (char_u *)"window";
7868 else if (scope == CDSCOPE_TABPAGE)
7869 acmd_fname = (char_u *)"tabpage";
7870 else
7871 acmd_fname = (char_u *)"global";
7872 trigger_DirChangedPre(acmd_fname, new_dir);
7873
7874 if (vim_chdir(new_dir))
7875 {
7876 emsg(_(e_command_failed));
7877 vim_free(pdir);
7878 return FALSE;
7879 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007880 }
zeertzjq73257142022-02-02 13:16:37 +00007881
7882 if (scope == CDSCOPE_WINDOW)
7883 pp = &curwin->w_prevdir;
7884 else if (scope == CDSCOPE_TABPAGE)
7885 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007886 else
zeertzjq73257142022-02-02 13:16:37 +00007887 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007888 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00007889 *pp = pdir;
7890
7891 post_chdir(scope);
7892
7893 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007894 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01007895 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00007896 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007897}
Bram Moolenaarea408852005-06-25 22:49:46 +00007898
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007900 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007902 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007903ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007905 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906
7907 new_dir = eap->arg;
7908#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007909 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7910 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007912 ex_pwd(NULL);
7913 return;
7914 }
7915#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007916
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007917 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007918
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007919 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7920 scope = CDSCOPE_WINDOW;
7921 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7922 scope = CDSCOPE_TABPAGE;
7923
7924 if (changedir_func(new_dir, eap->forceit, scope))
7925 {
7926 // Echo the new current directory if the command was typed.
7927 if (KeyTyped || p_verbose >= 5)
7928 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930}
7931
7932/*
7933 * ":pwd".
7934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007936ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7939 {
7940#ifdef BACKSLASH_IN_FILENAME
7941 slash_adjust(NameBuff);
7942#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007943 if (p_verbose > 0)
7944 {
7945 char *context = "global";
7946
Bram Moolenaar05268152021-11-18 18:53:45 +00007947 if (last_chdir_reason != NULL)
7948 context = last_chdir_reason;
7949 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007950 context = "window";
7951 else if (curtab->tp_localdir != NULL)
7952 context = "tabpage";
7953 smsg("[%s] %s", context, (char *)NameBuff);
7954 }
7955 else
7956 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 }
7958 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007959 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960}
7961
7962/*
7963 * ":=".
7964 */
7965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007966ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007968 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007969 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970}
7971
7972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007973ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007975 int n;
7976 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977
7978 if (cursor_valid())
7979 {
7980 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7981 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007982 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007984
7985 len = eap->line2;
7986 switch (*eap->arg)
7987 {
7988 case 'm': break;
7989 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007990 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007991 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007992
7993 // Hide the cursor if invoked with !
7994 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995}
7996
7997/*
7998 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007999 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 */
8001 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008002do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
Bram Moolenaar52953192019-08-16 10:27:13 +02008004 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008005 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008006# ifdef ELAPSED_FUNC
8007 elapsed_T start_tv;
8008
8009 // Remember at what time we started, so that we know how much longer we
8010 // should wait after waiting for a bit.
8011 ELAPSED_INIT(start_tv);
8012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008014 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008015 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008016 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008017 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008018
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008019 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008020 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008022 wait_now = msec - done > 1000L ? 1000L : msec - done;
8023#ifdef FEAT_TIMERS
8024 {
8025 long due_time = check_due_timer();
8026
8027 if (due_time > 0 && due_time < wait_now)
8028 wait_now = due_time;
8029 }
8030#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008031#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008032 if (has_any_channel() && wait_now > 20L)
8033 wait_now = 20L;
8034#endif
8035#ifdef FEAT_SOUND
8036 if (has_any_sound_callback() && wait_now > 20L)
8037 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008038#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008039 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008040
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008041#ifdef FEAT_JOB_CHANNEL
8042 if (has_any_channel())
8043 ui_breakcheck_force(TRUE);
8044 else
8045#endif
8046 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008047#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008048 // Process the netbeans and clientserver messages that may have been
8049 // received in the call to ui_breakcheck() when the GUI is in use. This
8050 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008051 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008052#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008053
8054# ifdef ELAPSED_FUNC
8055 // actual time passed
8056 done = ELAPSED_FUNC(start_tv);
8057# else
8058 // guestimate time passed (will actually be more)
8059 done += wait_now;
8060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008062
8063 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8064 // input buffer, otherwise a following call to input() fails.
8065 if (got_int)
8066 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008067
8068 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008069 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070}
8071
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072/*
8073 * ":winsize" command (obsolete).
8074 */
8075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008076ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078 int w, h;
8079 char_u *arg = eap->arg;
8080 char_u *p;
8081
Keith Thompson184f71c2024-01-04 21:19:04 +01008082 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008083 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008084 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008085 return;
8086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 w = getdigits(&arg);
8088 arg = skipwhite(arg);
8089 p = arg;
8090 h = getdigits(&arg);
8091 if (*p != NUL && *arg == NUL)
8092 set_shellsize(w, h, TRUE);
8093 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008094 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095}
8096
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008098ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 int xchar = NUL;
8101 char_u *p;
8102
8103 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8104 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008105 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 if (eap->arg[1] == NUL)
8107 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008108 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 return;
8110 }
8111 xchar = eap->arg[1];
8112 p = eap->arg + 2;
8113 }
8114 else
8115 p = eap->arg + 1;
8116
Bram Moolenaar63b91732021-08-05 20:40:03 +02008117 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008119 if (*p != NUL && *p != (
8120#ifdef FEAT_EVAL
8121 in_vim9script() ? '#' :
8122#endif
8123 '"')
8124 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008125 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008126 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008128 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008129 postponed_split_flags = cmdmod.cmod_split;
8130 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8132 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008133 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 }
8135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
Bram Moolenaar843ee412004-06-30 16:16:41 +00008137#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138/*
8139 * ":winpos".
8140 */
8141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008142ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143{
8144 int x, y;
8145 char_u *arg = eap->arg;
8146 char_u *p;
8147
8148 if (*arg == NUL)
8149 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008150# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008151# ifdef VIMDLL
8152 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8153 mch_get_winpos(&x, &y) != FAIL)
8154# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008156# else
8157 if (mch_get_winpos(&x, &y) != FAIL)
8158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {
8160 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008161 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 }
8163 else
8164# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008165 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 }
8167 else
8168 {
8169 x = getdigits(&arg);
8170 arg = skipwhite(arg);
8171 p = arg;
8172 y = getdigits(&arg);
8173 if (*p == NUL || *arg != NUL)
8174 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008175 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 return;
8177 }
8178# ifdef FEAT_GUI
8179 if (gui.in_use)
8180 gui_mch_set_winpos(x, y);
8181 else if (gui.starting)
8182 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008183 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 gui_win_x = x;
8185 gui_win_y = y;
8186 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008187# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 else
8189# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008190# endif
8191# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008192 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193# endif
8194# ifdef HAVE_TGETENT
8195 if (*T_CWP)
8196 term_set_winpos(x, y);
8197# endif
8198 }
8199}
8200#endif
8201
8202/*
8203 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8204 */
8205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008206ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207{
8208 oparg_T oa;
8209
8210 clear_oparg(&oa);
8211 oa.regname = eap->regname;
8212 oa.start.lnum = eap->line1;
8213 oa.end.lnum = eap->line2;
8214 oa.line_count = eap->line2 - eap->line1 + 1;
8215 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008217 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 setpcmark();
8220 curwin->w_cursor.lnum = eap->line1;
8221 beginline(BL_SOL | BL_FIX);
8222 }
8223
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008224 if (VIsual_active)
8225 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008226
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 switch (eap->cmdidx)
8228 {
8229 case CMD_delete:
8230 oa.op_type = OP_DELETE;
8231 op_delete(&oa);
8232 break;
8233
8234 case CMD_yank:
8235 oa.op_type = OP_YANK;
8236 (void)op_yank(&oa, FALSE, TRUE);
8237 break;
8238
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008239 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008240 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008242 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8243#else
8244 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008246 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 oa.op_type = OP_RSHIFT;
8248 else
8249 oa.op_type = OP_LSHIFT;
8250 op_shift(&oa, FALSE, eap->amount);
8251 break;
8252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008254 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255}
8256
8257/*
8258 * ":put".
8259 */
8260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008261ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008263 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 if (eap->line2 == 0)
8265 {
8266 eap->line2 = 1;
8267 eap->forceit = TRUE;
8268 }
8269 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008270 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008271 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008272 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273}
8274
8275/*
8276 * Handle ":copy" and ":move".
8277 */
8278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008279ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
8281 long n;
8282
Bram Moolenaar491799b2020-08-01 19:23:43 +02008283#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008284 if (not_in_vim9(eap) == FAIL)
8285 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008286#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008287 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008288 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {
8290 eap->nextcmd = NULL;
8291 return;
8292 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008293 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294
8295 /*
8296 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8297 */
8298 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8299 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008300 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 return;
8302 }
8303
8304 if (eap->cmdidx == CMD_move)
8305 {
8306 if (do_move(eap->line1, eap->line2, n) == FAIL)
8307 return;
8308 }
8309 else
8310 ex_copy(eap->line1, eap->line2, n);
8311 u_clearline();
8312 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008313 ex_may_print(eap);
8314}
8315
8316/*
8317 * Print the current line if flags were given to the Ex command.
8318 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008319 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008320ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008321{
8322 if (eap->flags != 0)
8323 {
8324 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8325 (eap->flags & EXFLAG_LIST));
8326 ex_no_reprint = TRUE;
8327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328}
8329
8330/*
8331 * ":smagic" and ":snomagic".
8332 */
8333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008334ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008336 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008338 magic_overruled = eap->cmdidx == CMD_smagic
8339 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008340 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008341 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342}
8343
8344/*
8345 * ":join".
8346 */
8347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008348ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 curwin->w_cursor.lnum = eap->line1;
8351 if (eap->line1 == eap->line2)
8352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 return;
8355 if (eap->line2 == curbuf->b_ml.ml_line_count)
8356 {
8357 beep_flush();
8358 return;
8359 }
8360 ++eap->line2;
8361 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008362 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008364 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365}
8366
8367/*
8368 * ":[addr]@r" or ":[addr]*r": execute register
8369 */
8370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008371ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372{
8373 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008374 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375
8376 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008377 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008380 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381#endif
8382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008383 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 c = *eap->arg;
8385 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8386 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008387 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008388 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8389 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008392 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008395 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008397 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008399 /*
8400 * Execute from the typeahead buffer.
8401 * Continue until the stuff buffer is empty and all added characters
8402 * have been consumed.
8403 */
8404 while (!stuff_empty() || typebuf.tb_len > prev_len)
8405 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8406
8407 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408}
8409
8410/*
8411 * ":!".
8412 */
8413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008414ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415{
8416 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8417}
8418
8419/*
8420 * ":undo".
8421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008423ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008425 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008426 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008427 else
8428 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429}
8430
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008431#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008433ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008434{
8435 char_u hash[UNDO_HASH_SIZE];
8436
8437 u_compute_hash(hash);
8438 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8439}
8440
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008442ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008443{
8444 char_u hash[UNDO_HASH_SIZE];
8445
8446 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008447 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008448}
8449#endif
8450
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451/*
8452 * ":redo".
8453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008455ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
8457 u_redo(1);
8458}
8459
8460/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008461 * ":earlier" and ":later".
8462 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008464ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008465{
8466 long count = 0;
8467 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008468 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008469 char_u *p = eap->arg;
8470
8471 if (*p == NUL)
8472 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008473 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008474 {
8475 count = getdigits(&p);
8476 switch (*p)
8477 {
8478 case 's': ++p; sec = TRUE; break;
8479 case 'm': ++p; sec = TRUE; count *= 60; break;
8480 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008481 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8482 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008483 }
8484 }
8485
8486 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008487 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008488 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008489 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8490 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008491}
8492
8493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 * ":redir": start/stop redirection.
8495 */
8496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008497ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
8499 char *mode;
8500 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008501 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502
Bram Moolenaarba768492016-07-08 15:32:54 +02008503#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008504 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008505 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008506 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008507 return;
8508 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008509#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008510
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 if (STRICMP(eap->arg, "END") == 0)
8512 close_redir();
8513 else
8514 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008515 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008517 ++arg;
8518 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008520 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 mode = "a";
8522 }
8523 else
8524 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008525 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526
8527 close_redir();
8528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008530 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 if (fname == NULL)
8532 return;
8533#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008534 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 {
8536 char_u *browseFile;
8537
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008538 browseFile = do_browse(BROWSE_SAVE,
8539 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008540 fname, NULL, NULL,
8541 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008543 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 vim_free(fname);
8545 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008546 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 }
8548#endif
8549
8550 redir_fd = open_exfile(fname, eap->forceit, mode);
8551 vim_free(fname);
8552 }
8553#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008554 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008556 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008558 ++arg;
8559 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008561 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008562 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008564 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008566 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008567 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008568 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008571 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008572 if (*arg == '>')
8573 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008574 // Make register empty when not using @A-@Z and the
8575 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008576 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008577 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008579 }
8580 if (*arg != NUL)
8581 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008582 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008583 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008584 }
8585 }
8586 else if (*arg == '=' && arg[1] == '>')
8587 {
8588 int append;
8589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008590 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008591 close_redir();
8592 arg += 2;
8593
8594 if (*arg == '>')
8595 {
8596 ++arg;
8597 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 }
8599 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008600 append = FALSE;
8601
8602 if (var_redir_start(skipwhite(arg), append) == OK)
8603 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 }
8605#endif
8606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008607 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008610 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008612
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008613 // Make sure redirection is not off. Can happen for cmdline completion
8614 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008615 if (redir_fd != NULL
8616#ifdef FEAT_EVAL
8617 || redir_reg || redir_vname
8618#endif
8619 )
8620 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621}
8622
8623/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008624 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008626 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008627ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008629 redraw_cmd(eap->forceit);
8630}
8631
8632/*
8633 * ":redraw": force redraw, with clear if "clear" is TRUE.
8634 */
8635 void
8636redraw_cmd(int clear)
8637{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008638 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008640
8641 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008643
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008644 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008646 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 if (need_maketitle)
8648 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008649#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8650# ifdef VIMDLL
8651 if (!gui.in_use)
8652# endif
8653 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008654#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008655 RedrawingDisabled = save_RedrawingDisabled;
8656 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657
Bram Moolenaar5017c662022-04-07 18:06:08 +01008658 // After drawing the statusline screen_attr may still be set.
8659 screen_stop_highlight();
8660
Bram Moolenaara2a89732022-08-31 14:46:18 +01008661 // Reset msg_didout, so that a message that's there is overwritten.
8662 msg_didout = FALSE;
8663 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008665 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008666 need_wait_return = FALSE;
8667
Bram Moolenaara653e532022-04-19 11:38:24 +01008668 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008669 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008670 redrawcmdline();
8671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 out_flush();
8673}
8674
8675/*
8676 * ":redrawstatus": force redraw of status line(s)
8677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008679ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 if (eap->forceit)
8682 status_redraw_all();
8683 else
8684 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008685 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008686 return; // redraw later
8687
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008688 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008689 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008690
8691 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008692 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008693
zeertzjq320d9102022-09-20 17:12:13 +01008694 if (State & MODE_CMDLINE)
8695 redraw_statuslines();
8696 else
8697 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008698 RedrawingDisabled = save_RedrawingDisabled;
8699 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701}
8702
Bram Moolenaare12bab32019-01-08 22:02:56 +01008703/*
8704 * ":redrawtabline": force redraw of the tabline
8705 */
8706 static void
8707ex_redrawtabline(exarg_T *eap UNUSED)
8708{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008709 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008710 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008711
8712 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008713 p_lz = FALSE;
8714
8715 draw_tabline();
8716
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008717 RedrawingDisabled = save_RedrawingDisabled;
8718 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008719 out_flush();
8720}
8721
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008723close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725 if (redir_fd != NULL)
8726 {
8727 fclose(redir_fd);
8728 redir_fd = NULL;
8729 }
8730#ifdef FEAT_EVAL
8731 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008732 if (redir_vname)
8733 {
8734 var_redir_stop();
8735 redir_vname = 0;
8736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737#endif
8738}
8739
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008740#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008741 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008742vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008743{
8744 if (vim_mkdir(name, prot) != 0)
8745 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008746 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008747 return FAIL;
8748 }
8749 return OK;
8750}
8751#endif
8752
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753/*
8754 * Open a file for writing for an Ex command, with some checks.
8755 * Return file descriptor, or NULL on failure.
8756 */
8757 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008758open_exfile(
8759 char_u *fname,
8760 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008761 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 FILE *fd;
8764
8765#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008766 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 if (mch_isdir(fname))
8768 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008769 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 return NULL;
8771 }
8772#endif
8773 if (!forceit && *mode != 'a' && vim_fexists(fname))
8774 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008775 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 return NULL;
8777 }
8778
8779 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008780 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
8782 return fd;
8783}
8784
8785/*
8786 * ":mark" and ":k".
8787 */
8788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008789ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
8791 pos_T pos;
8792
Bram Moolenaar10b94212021-02-19 21:42:57 +01008793#ifdef FEAT_EVAL
8794 if (not_in_vim9(eap) == FAIL)
8795 return;
8796#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008797 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008799 emsg(_(e_argument_required));
8800 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008802
8803 if (eap->arg[1] != NUL) // more than one character?
8804 {
8805 semsg(_(e_trailing_characters_str), eap->arg);
8806 return;
8807 }
8808
8809 pos = curwin->w_cursor; // save curwin->w_cursor
8810 curwin->w_cursor.lnum = eap->line2;
8811 beginline(BL_WHITE | BL_FIX);
8812 if (setmark(*eap->arg) == FAIL) // set mark
8813 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
8814 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815}
8816
8817/*
8818 * Update w_topline, w_leftcol and the cursor position.
8819 */
8820 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008821update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008823 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 update_topline();
8825 if (!curwin->w_p_wrap)
8826 validate_cursor();
8827 update_curswant();
8828}
8829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008831 * Save the current State and go to Normal mode.
8832 * Return TRUE if the typeahead could be saved.
8833 */
8834 int
8835save_current_state(save_state_T *sst)
8836{
8837 sst->save_msg_scroll = msg_scroll;
8838 sst->save_restart_edit = restart_edit;
8839 sst->save_msg_didout = msg_didout;
8840 sst->save_State = State;
8841 sst->save_insertmode = p_im;
8842 sst->save_finish_op = finish_op;
8843 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008844 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008845 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008846
Bram Moolenaar4324d872020-12-01 20:12:24 +01008847 msg_scroll = FALSE; // no msg scrolling in Normal mode
8848 restart_edit = 0; // don't go to Insert mode
8849 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008850
Bram Moolenaara452b802020-12-01 21:47:59 +01008851 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008852 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008853
8854 /*
8855 * Save the current typeahead. This is required to allow using ":normal"
8856 * from an event handler and makes sure we don't hang when the argument
8857 * ends with half a command.
8858 */
8859 save_typeahead(&sst->tabuf);
8860 return sst->tabuf.typebuf_valid;
8861}
8862
8863 void
8864restore_current_state(save_state_T *sst)
8865{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008866 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008867 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008868
8869 msg_scroll = sst->save_msg_scroll;
8870 restart_edit = sst->save_restart_edit;
8871 p_im = sst->save_insertmode;
8872 finish_op = sst->save_finish_op;
8873 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008874 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01008875 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008876 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008877 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008879 // Restore the state (needed when called from a function executed for
8880 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008881 State = sst->save_State;
8882#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008883 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008884#endif
8885}
8886
8887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 * ":normal[!] {commands}": Execute normal mode commands.
8889 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008890 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008891ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008893 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 char_u *arg = NULL;
8895 int l;
8896 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008898 if (ex_normal_lock > 0)
8899 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008900 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008901 return;
8902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 if (ex_normal_busy >= p_mmd)
8904 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008905 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 return;
8907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 /*
8910 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8911 * this for the K_SPECIAL leading byte, otherwise special keys will not
8912 * work.
8913 */
8914 if (has_mbyte)
8915 {
8916 int len = 0;
8917
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008918 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 for (p = eap->arg; *p != NUL; ++p)
8920 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008921#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008922 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008924#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008925 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008926 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008927#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 )
8931 len += 2;
8932 }
8933 if (len > 0)
8934 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008935 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 if (arg != NULL)
8937 {
8938 len = 0;
8939 for (p = eap->arg; *p != NUL; ++p)
8940 {
8941 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008942#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 if (*p == CSI)
8944 {
8945 arg[len++] = KS_EXTRA;
8946 arg[len++] = (int)KE_CSI;
8947 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008948#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008949 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 {
8951 arg[len++] = *++p;
8952 if (*p == K_SPECIAL)
8953 {
8954 arg[len++] = KS_SPECIAL;
8955 arg[len++] = KE_FILLER;
8956 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008957#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 else if (*p == CSI)
8959 {
8960 arg[len++] = KS_EXTRA;
8961 arg[len++] = (int)KE_CSI;
8962 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 }
8965 arg[len] = NUL;
8966 }
8967 }
8968 }
8969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008971 ++ex_normal_busy;
8972 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 {
8974 /*
8975 * Repeat the :normal command for each line in the range. When no
8976 * range given, execute it just once, without positioning the cursor
8977 * first.
8978 */
8979 do
8980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 if (eap->addr_count != 0)
8982 {
8983 curwin->w_cursor.lnum = eap->line1++;
8984 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008985 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 }
8987
Bram Moolenaar13505972019-01-24 15:04:48 +01008988 exec_normal_cmd(arg != NULL
8989 ? arg
8990 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 }
8992 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8993 }
8994
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008995 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 update_topline_cursor();
8997
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008998 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009000 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009001#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009002 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009003#endif
9004
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006}
9007
9008/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009009 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 */
9011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009012ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009014 if (eap->forceit)
9015 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009016 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009017 if (!curwin->w_cursor.lnum)
9018 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009019 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009020 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009021#ifdef FEAT_TERMINAL
9022 // Ignore this when running in an active terminal.
9023 if (term_job_running(curbuf->b_term))
9024 return;
9025#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009026
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009027 // Ignore the command when already in Insert mode. Inserting an
9028 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009029 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009030 return;
9031
Bram Moolenaar64969662005-12-14 21:59:55 +00009032 if (eap->cmdidx == CMD_startinsert)
9033 restart_edit = 'a';
9034 else if (eap->cmdidx == CMD_startreplace)
9035 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009037 restart_edit = 'V';
9038
9039 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009041 if (eap->cmdidx == CMD_startinsert)
9042 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009043 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009045
9046 if (VIsual_active)
9047 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048}
9049
9050/*
9051 * ":stopinsert"
9052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009054ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055{
9056 restart_edit = 0;
9057 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009058 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009061/*
9062 * Execute normal mode command "cmd".
9063 * "remap" can be REMAP_NONE or REMAP_YES.
9064 */
9065 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009066exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009067{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009068 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009069 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009070 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009071}
Bram Moolenaar25281632016-01-21 23:32:32 +01009072
Bram Moolenaar25281632016-01-21 23:32:32 +01009073/*
9074 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009075 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009076 */
9077 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009078exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009079{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009080 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009081 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009082
Bram Moolenaar905dd902019-04-07 14:21:47 +02009083 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9084 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009085 clear_oparg(&oa);
9086 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009087 while ((!stuff_empty()
9088 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009089 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009090 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009091 {
9092 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009093#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009094 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009095 && oa.op_type == OP_NOP && oa.regname == NUL
9096 && !VIsual_active)
9097 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009098 // If terminal_loop() returns OK we got a key that is handled
9099 // in Normal model. With FAIL we first need to position the
9100 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009101 if (terminal_loop(TRUE) == OK)
9102 normal_cmd(&oa, TRUE);
9103 }
9104 else
9105#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009106 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009107 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009108 }
9109}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009110
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111#ifdef FEAT_FIND_ID
9112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009113ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114{
9115 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9116 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009117 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118}
9119
Bram Moolenaar4033c552017-09-16 20:54:51 +02009120#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121/*
9122 * ":psearch"
9123 */
9124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009125ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126{
9127 g_do_tagpreview = p_pvh;
9128 ex_findpat(eap);
9129 g_do_tagpreview = 0;
9130}
9131#endif
9132
9133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009134ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
9136 int whole = TRUE;
9137 long n;
9138 char_u *p;
9139 int action;
9140
9141 switch (cmdnames[eap->cmdidx].cmd_name[2])
9142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009143 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9145 action = ACTION_GOTO;
9146 else
9147 action = ACTION_SHOW;
9148 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009149 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 action = ACTION_SHOW_ALL;
9151 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009152 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 action = ACTION_GOTO;
9154 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009155 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 action = ACTION_SPLIT;
9157 break;
9158 }
9159
9160 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009161 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 {
9163 n = getdigits(&eap->arg);
9164 eap->arg = skipwhite(eap->arg);
9165 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009166 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 {
9168 whole = FALSE;
9169 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009170 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (*p)
9172 {
9173 *p++ = NUL;
9174 p = skipwhite(p);
9175
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009176 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009177 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009178 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009180 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 }
9182 }
9183 if (!eap->skip)
9184 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9185 whole, !eap->forceit,
9186 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009187 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188}
9189#endif
9190
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191
Bram Moolenaar4033c552017-09-16 20:54:51 +02009192#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193/*
9194 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9195 */
9196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009197ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009199 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9201}
9202
9203/*
9204 * ":pedit"
9205 */
9206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009207ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
9209 win_T *curwin_save = curwin;
9210
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009211 if (ERROR_IF_ANY_POPUP_WINDOW)
9212 return;
9213
Bram Moolenaar026587b2019-08-17 15:08:00 +02009214 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009216 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009217
Bram Moolenaar026587b2019-08-17 15:08:00 +02009218 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009220
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 if (curwin != curwin_save && win_valid(curwin_save))
9222 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009223 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009225 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 win_enter(curwin_save, TRUE);
9227 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009228# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009229 else if (WIN_IS_POPUP(curwin))
9230 {
9231 // can't keep focus in popup window
9232 win_enter(firstwin, TRUE);
9233 }
9234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 g_do_tagpreview = 0;
9236}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
9239/*
9240 * ":stag", ":stselect" and ":stjump".
9241 */
9242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009243ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244{
9245 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009246 postponed_split_flags = cmdmod.cmod_split;
9247 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9249 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009250 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252
9253/*
9254 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9255 */
9256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009257ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9260}
9261
9262 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009263ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264{
9265 int cmd;
9266
9267 switch (name[1])
9268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009269 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009271 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009273 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009275 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009277 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009279 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009281 case 'f': // ":tfirst"
9282 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009284 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009286 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009288 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009290 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 return;
9292 }
9293#endif
9294 cmd = DT_TAG;
9295 break;
9296 }
9297
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009298 if (name[0] == 'l')
9299 {
9300#ifndef FEAT_QUICKFIX
9301 ex_ni(eap);
9302 return;
9303#else
9304 cmd = DT_LTAG;
9305#endif
9306 }
9307
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9309 eap->forceit, TRUE);
9310}
9311
John Marriotted908f72024-04-18 22:46:56 +02009312enum {
9313 SPEC_PERC = 0,
9314 SPEC_HASH,
9315 SPEC_CWORD, // cursor word
9316 SPEC_CCWORD, // cursor WORD
9317 SPEC_CEXPR, // expr under cursor
9318 SPEC_CFILE, // cursor path name
9319 SPEC_SFILE, // ":so" file name
9320 SPEC_SLNUM, // ":so" file line number
9321 SPEC_STACK, // call stack
9322 SPEC_SCRIPT, // script file name
9323 SPEC_AFILE, // autocommand file name
9324 SPEC_ABUF, // autocommand buffer number
9325 SPEC_AMATCH, // autocommand match name
9326 SPEC_SFLNUM, // script file line number
9327 SPEC_SID // script ID: <SNR>123_
9328#ifdef FEAT_CLIENTSERVER
9329 , SPEC_CLIENT
9330#endif
9331};
9332
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009334 * Check "str" for starting with a special cmdline variable.
9335 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9336 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9337 */
9338 int
Mike Williams51024bb2024-05-30 07:46:30 +02009339find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009340{
John Marriotted908f72024-04-18 22:46:56 +02009341 // must be sorted by the 'value' field because it is used by bsearch()!
9342 static keyvalue_T spec_str_tab[] = {
9343 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9344 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9345 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9346 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9347 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9348 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9349 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009350#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009351 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009352#endif
John Marriotted908f72024-04-18 22:46:56 +02009353 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9354 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9355 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9356 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9357 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9358 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009359 };
John Marriotted908f72024-04-18 22:46:56 +02009360 keyvalue_T target;
9361 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009362
John Marriotted908f72024-04-18 22:46:56 +02009363 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009364 {
John Marriotted908f72024-04-18 22:46:56 +02009365 case '%':
9366 *usedlen = 1;
9367 return SPEC_PERC;
9368
9369 case '#':
9370 *usedlen = 1;
9371 return SPEC_HASH;
9372
9373 case '<':
9374 target.key = 0;
9375 target.value = (char *)src + 1; // skip over '<'
9376 target.length = 0; // not used, see cmp_keyvalue_value_n()
9377
9378 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab, ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]), cmp_keyvalue_value_n);
9379 if (entry == NULL)
9380 return -1;
9381
9382 *usedlen = entry->length + 1;
9383 return entry->key;
9384
9385 default:
9386 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009387 }
John Marriotted908f72024-04-18 22:46:56 +02009388
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009389 return -1;
9390}
9391
9392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 * Evaluate cmdline variables.
9394 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009395 * change "%" to curbuf->b_ffname
9396 * "#" to curwin->w_alt_fnum
9397 * "%%" to curwin->w_alt_fnum in Vim9 script
9398 * "<cword>" to word under the cursor
9399 * "<cWORD>" to WORD under the cursor
9400 * "<cexpr>" to C-expression under the cursor
9401 * "<cfile>" to path name under the cursor
9402 * "<sfile>" to sourced file name
9403 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009404 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009405 * "<slnum>" to sourced file line number
9406 * "<afile>" to file name for autocommand
9407 * "<abuf>" to buffer number for autocommand
9408 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 *
9410 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9411 * "" for error without a message) and NULL is returned.
9412 * Returns an allocated string if a valid match was found.
9413 * Returns NULL if no match was found. "usedlen" then still contains the
9414 * number of characters to skip.
9415 */
9416 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009417eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009418 char_u *src, // pointer into commandline
9419 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009420 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009421 linenr_T *lnump, // line number for :e command, or NULL
9422 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009423 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009424 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009425 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426{
9427 int i;
9428 char_u *s;
9429 char_u *result;
9430 char_u *resultbuf = NULL;
9431 int resultlen;
9432 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009433 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009435 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438
9439 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009440 if (escaped != NULL)
9441 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442
9443 /*
9444 * Check if there is something to do.
9445 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009446 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009447 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 {
9449 *usedlen = 1;
9450 return NULL;
9451 }
9452
9453 /*
9454 * Skip when preceded with a backslash "\%" and "\#".
9455 * Note: In "\\%" the % is also not recognized!
9456 */
9457 if (src > srcstart && src[-1] == '\\')
9458 {
9459 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009460 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 return NULL;
9462 }
9463
9464 /*
9465 * word or WORD under cursor
9466 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009467 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9468 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009470 resultlen = find_ident_under_cursor(&result,
9471 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9472 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9473 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 if (resultlen == 0)
9475 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009476 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 return NULL;
9478 }
9479 }
9480
9481 /*
9482 * '#': Alternate file name
9483 * '%': Current file name
9484 * File name under the cursor
9485 * File name for autocommand
9486 * and following modifiers
9487 */
9488 else
9489 {
Mike Williams51024bb2024-05-30 07:46:30 +02009490 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009491
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 switch (spec_idx)
9493 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009494 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009495#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009496 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009499 // '%': current file
9500 if (curbuf->b_fname == NULL)
9501 {
9502 result = (char_u *)"";
9503 valid = 0; // Must have ":p:h" to be valid
9504 }
9505 else
9506 {
9507 result = curbuf->b_fname;
9508 tilde_file = STRCMP(result, "~") == 0;
9509 }
9510 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009512#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009513 // "%%" alternate file
9514 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009515#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009516 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009517 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009518 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009520 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 result = arg_all();
9522 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009523 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009524 if (escaped != NULL)
9525 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 break;
9528 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009529 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009530 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009531 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009533 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009534 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009535 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009536 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009538 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009540 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009542 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009543 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009544 return NULL;
9545 }
9546#ifdef FEAT_EVAL
9547 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9548 (long)i);
9549 if (result == NULL)
9550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009551 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009552 return NULL;
9553 }
9554#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009555 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 }
9559 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009560 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009561 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9562 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009563 buf = buflist_findnr(i);
9564 if (buf == NULL)
9565 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009566 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009567 return NULL;
9568 }
9569 if (lnump != NULL)
9570 *lnump = ECMD_LAST;
9571 if (buf->b_fname == NULL)
9572 {
9573 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009574 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009575 }
9576 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009577 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009578 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009579 tilde_file = STRCMP(result, "~") == 0;
9580 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 break;
9583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009584 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009585 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (result == NULL)
9587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009588 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 return NULL;
9590 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009591 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009594 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009596 if (result != NULL && !autocmd_fname_full)
9597 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009598 // Still need to turn the fname into a full path. It is
9599 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009600 autocmd_fname_full = TRUE;
9601 result = FullName_save(autocmd_fname, FALSE);
9602 vim_free(autocmd_fname);
9603 autocmd_fname = result;
9604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 if (result == NULL)
9606 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009607 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 return NULL;
9609 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009610 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 break;
9612
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009613 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 if (autocmd_bufnr <= 0)
9615 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009616 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 return NULL;
9618 }
9619 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9620 result = strbuf;
9621 break;
9622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009623 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 result = autocmd_match;
9625 if (result == NULL)
9626 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009627 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 return NULL;
9629 }
9630 break;
9631
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009632 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009633 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 if (result == NULL)
9635 {
LemonBoy6013d002022-04-09 21:42:10 +01009636 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9637 return NULL;
9638 }
9639 resultbuf = result; // remember allocated string
9640 break;
9641 case SPEC_STACK: // call stack
9642 result = estack_sfile(ESTACK_STACK);
9643 if (result == NULL)
9644 {
9645 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9646 return NULL;
9647 }
9648 resultbuf = result; // remember allocated string
9649 break;
9650 case SPEC_SCRIPT: // script file name
9651 result = estack_sfile(ESTACK_SCRIPT);
9652 if (result == NULL)
9653 {
9654 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 return NULL;
9656 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009657 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009660 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009661 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009662 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009663 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009664 return NULL;
9665 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009666 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009667 result = strbuf;
9668 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009669
9670#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009671 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009672 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009673 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009674 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009675 return NULL;
9676 }
9677 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009678 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009679 result = strbuf;
9680 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009681
Bram Moolenaar90944302020-08-01 20:45:11 +02009682 case SPEC_SID:
9683 if (current_sctx.sc_sid <= 0)
9684 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009685 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009686 return NULL;
9687 }
9688 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9689 result = strbuf;
9690 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009691#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009692
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009693#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009694 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009695 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9696 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 result = strbuf;
9698 break;
9699#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009700
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009701 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009702 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009703 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 }
9705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009706 resultlen = (int)STRLEN(result); // length of new string
9707 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 {
9709 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 resultlen = (int)(s - result);
9712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 else if (!skip_mod)
9714 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009715 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 &resultlen);
9717 if (result == NULL)
9718 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009719 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 return NULL;
9721 }
9722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 }
9724
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009725 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +01009727 if (empty_is_error)
9728 {
9729 if (valid != VALID_HEAD + VALID_PATH)
9730 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
9731 else
9732 *errormsg = _(e_evaluates_to_an_empty_string);
9733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 result = NULL;
9735 }
9736 else
9737 result = vim_strnsave(result, resultlen);
9738 vim_free(resultbuf);
9739 return result;
9740}
9741
9742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 * Expand the <sfile> string in "arg".
9744 *
9745 * Returns an allocated string, or NULL for any error.
9746 */
9747 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009748expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009750 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +02009752 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +02009754 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +02009756 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +02009757 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 char_u *p;
9759
John Marriotted908f72024-04-18 22:46:56 +02009760 resultlen = STRLEN(arg);
9761 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 if (result == NULL)
9763 return NULL;
9764
9765 for (p = result; *p; )
9766 {
9767 if (STRNCMP(p, "<sfile>", 7) != 0)
9768 ++p;
9769 else
9770 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009771 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +01009772 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 if (errormsg != NULL)
9774 {
9775 if (*errormsg)
9776 emsg(errormsg);
9777 vim_free(result);
9778 return NULL;
9779 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009780 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 {
9782 p += srclen;
9783 continue;
9784 }
John Marriotted908f72024-04-18 22:46:56 +02009785 repllen = STRLEN(repl);
9786 resultlen += repllen - srclen;
9787 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (newres == NULL)
9789 {
9790 vim_free(repl);
9791 vim_free(result);
9792 return NULL;
9793 }
John Marriotted908f72024-04-18 22:46:56 +02009794 len = p - result;
9795 mch_memmove(newres, result, len);
9796 STRCPY(newres + len, repl);
9797 len += repllen;
9798 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 vim_free(repl);
9800 vim_free(result);
9801 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009802 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 }
9804 }
9805
9806 return result;
9807}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009810/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009811 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009812 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009815dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 if (fname == NULL)
9818 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009819 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820}
9821#endif
9822
9823/*
9824 * ":behave {mswin,xterm}"
9825 */
9826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009827ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828{
9829 if (STRCMP(eap->arg, "mswin") == 0)
9830 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009831 set_option_value_give_err((char_u *)"selection",
9832 0L, (char_u *)"exclusive", 0);
9833 set_option_value_give_err((char_u *)"selectmode",
9834 0L, (char_u *)"mouse,key", 0);
9835 set_option_value_give_err((char_u *)"mousemodel",
9836 0L, (char_u *)"popup", 0);
9837 set_option_value_give_err((char_u *)"keymodel",
9838 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 }
9840 else if (STRCMP(eap->arg, "xterm") == 0)
9841 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01009842 set_option_value_give_err((char_u *)"selection",
9843 0L, (char_u *)"inclusive", 0);
9844 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
9845 set_option_value_give_err((char_u *)"mousemodel",
9846 0L, (char_u *)"extend", 0);
9847 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 }
9849 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009850 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851}
9852
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853static int filetype_detect = FALSE;
9854static int filetype_plugin = FALSE;
9855static int filetype_indent = FALSE;
9856
9857/*
9858 * ":filetype [plugin] [indent] {on,off,detect}"
9859 * on: Load the filetype.vim file to install autocommands for file types.
9860 * off: Load the ftoff.vim file to remove all autocommands for file types.
9861 * plugin on: load filetype.vim and ftplugin.vim
9862 * plugin off: load ftplugof.vim
9863 * indent on: load filetype.vim and indent.vim
9864 * indent off: load indoff.vim
9865 */
9866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009867ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868{
9869 char_u *arg = eap->arg;
9870 int plugin = FALSE;
9871 int indent = FALSE;
9872
9873 if (*eap->arg == NUL)
9874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009875 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009876 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 filetype_detect ? "ON" : "OFF",
9878 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9879 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9880 return;
9881 }
9882
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009883 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 for (;;)
9885 {
9886 if (STRNCMP(arg, "plugin", 6) == 0)
9887 {
9888 plugin = TRUE;
9889 arg = skipwhite(arg + 6);
9890 continue;
9891 }
9892 if (STRNCMP(arg, "indent", 6) == 0)
9893 {
9894 indent = TRUE;
9895 arg = skipwhite(arg + 6);
9896 continue;
9897 }
9898 break;
9899 }
9900 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9901 {
9902 if (*arg == 'o' || !filetype_detect)
9903 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009904 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 filetype_detect = TRUE;
9906 if (plugin)
9907 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009908 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 filetype_plugin = TRUE;
9910 }
9911 if (indent)
9912 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009913 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 filetype_indent = TRUE;
9915 }
9916 }
9917 if (*arg == 'd')
9918 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009919 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009920 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 }
9922 }
9923 else if (STRCMP(arg, "off") == 0)
9924 {
9925 if (plugin || indent)
9926 {
9927 if (plugin)
9928 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009929 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 filetype_plugin = FALSE;
9931 }
9932 if (indent)
9933 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009934 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 filetype_indent = FALSE;
9936 }
9937 }
9938 else
9939 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009940 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 filetype_detect = FALSE;
9942 }
9943 }
9944 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009945 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946}
9947
9948/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009949 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 */
9951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009952ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
zeertzjq5bf6c212024-03-31 18:41:27 +02009954 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009955 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009956
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009957 char_u *arg = eap->arg;
9958 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9959 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +02009960
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009961 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9962 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +02009963 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009967ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968{
9969#ifdef FEAT_DIGRAPHS
9970 if (*eap->arg != NUL)
9971 putdigraph(eap->arg);
9972 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009973 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974#else
mityu61065042021-07-19 20:07:21 +02009975 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976#endif
9977}
9978
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009979#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9980 void
9981set_no_hlsearch(int flag)
9982{
9983 no_hlsearch = flag;
9984# ifdef FEAT_EVAL
9985 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9986# endif
9987}
9988
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989/*
9990 * ":nohlsearch"
9991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009993ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009995 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009996 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998#endif
9999
10000#ifdef FEAT_CRYPT
10001/*
10002 * ":X": Get crypt key
10003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010005ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010007 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010008 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009}
10010#endif
10011
10012#ifdef FEAT_FOLDING
10013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010014ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
10016 if (foldManualAllowed(TRUE))
10017 foldCreate(eap->line1, eap->line2);
10018}
10019
10020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010021ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022{
10023 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10024 eap->forceit, FALSE);
10025}
10026
10027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010028ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
10030 linenr_T lnum;
10031
Bram Moolenaar4facea32019-10-12 20:17:40 +020010032# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010033 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010034# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010035
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010036 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10038 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10039 ml_setmarked(lnum);
10040
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010041 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010043 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010044# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010045 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047}
10048#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010050#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010051/*
10052 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10053 * instead of a quickfix command.
10054 */
10055 int
10056is_loclist_cmd(int cmdidx)
10057{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010058 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010059 return FALSE;
10060 return cmdnames[cmdidx].cmd_name[0] == 'l';
10061}
10062#endif
10063
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010064 int
10065get_pressedreturn(void)
10066{
10067 return ex_pressedreturn;
10068}
10069
10070 void
10071set_pressedreturn(int val)
10072{
10073 ex_pressedreturn = val;
10074}