blob: a9294655871420fe3a509dcb88b6d769b0d16665 [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);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +010039static void do_exbuffer(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010040static void ex_bmodified(exarg_T *eap);
41static void ex_bnext(exarg_T *eap);
42static void ex_bprevious(exarg_T *eap);
43static void ex_brewind(exarg_T *eap);
44static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifndef FEAT_QUICKFIX
48# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000049# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# define ex_cc ex_ni
51# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020052# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define ex_cfile ex_ni
54# define qf_list ex_ni
55# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020056# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000058# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# define ex_cclose ex_ni
60# define ex_copen ex_ni
61# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020062# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000064#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
65# define ex_cexpr ex_ni
66#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020068static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020069static 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 +020070static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020072#if !defined(FEAT_PERL) \
73 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
74 || !defined(FEAT_TCL) \
75 || !defined(FEAT_RUBY) \
76 || !defined(FEAT_LUA) \
77 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000078# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010081static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010082static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000083#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000085#endif
John Marriotted908f72024-04-18 22:46:56 +020086static 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 +010087static void ex_highlight(exarg_T *eap);
88static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void ex_cquit(exarg_T *eap);
90static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static void ex_close(exarg_T *eap);
92static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
93static void ex_only(exarg_T *eap);
94static void ex_resize(exarg_T *eap);
95static void ex_stag(exarg_T *eap);
96static void ex_tabclose(exarg_T *eap);
97static void ex_tabonly(exarg_T *eap);
98static void ex_tabnext(exarg_T *eap);
99static void ex_tabmove(exarg_T *eap);
100static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200101#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100102static void ex_pclose(exarg_T *eap);
103static void ex_ptag(exarg_T *eap);
104static void ex_pedit(exarg_T *eap);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100105static void ex_pbuffer(exarg_T *eap);
106static void prepare_preview_window(void);
107static void back_to_current_window(win_T *curwin_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#else
109# define ex_pclose ex_ni
110# define ex_ptag ex_ni
111# define ex_pedit ex_ni
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100112# define ex_pbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_exit(exarg_T *eap);
116static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#else
120# define ex_goto ex_ni
121#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_shell(exarg_T *eap);
123static void ex_preserve(exarg_T *eap);
124static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100125static void ex_mode(exarg_T *eap);
126static void ex_wrongmodifier(exarg_T *eap);
127static void ex_find(exarg_T *eap);
128static void ex_open(exarg_T *eap);
129static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifndef FEAT_GUI
131# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100134#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100135static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#else
137# define ex_tearoff ex_ni
138#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100139#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
140 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#else
143# define ex_popup ex_ni
144#endif
145#ifndef FEAT_GUI_MSWIN
146# define ex_simalt ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define gui_mch_find_dialog ex_ni
150# define gui_mch_replace_dialog ex_ni
151#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000152#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define ex_helpfind ex_ni
154#endif
155#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100156# define ex_cscope ex_ni
157# define ex_scscope ex_ni
158# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
160#ifndef FEAT_SYN_HL
161# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200162# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200164#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200165# define ex_syntime ex_ni
166#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000167#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000168# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000170# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000171# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000172# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000173#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200174#ifndef FEAT_PERSISTENT_UNDO
175# define ex_rundo ex_ni
176# define ex_wundo ex_ni
177#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200178#ifndef FEAT_LUA
179# define ex_lua ex_script_ni
180# define ex_luado ex_ni
181# define ex_luafile ex_ni
182#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000183#ifndef FEAT_MZSCHEME
184# define ex_mzscheme ex_script_ni
185# define ex_mzfile ex_ni
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#ifndef FEAT_PERL
188# define ex_perl ex_script_ni
189# define ex_perldo ex_ni
190#endif
191#ifndef FEAT_PYTHON
192# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200193# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# define ex_pyfile ex_ni
195#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200197# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200198# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200199# define ex_py3file ex_ni
200#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100201#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
202# define ex_pyx ex_script_ni
203# define ex_pyxdo ex_ni
204# define ex_pyxfile ex_ni
205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifndef FEAT_TCL
207# define ex_tcl ex_script_ni
208# define ex_tcldo ex_ni
209# define ex_tclfile ex_ni
210#endif
211#ifndef FEAT_RUBY
212# define ex_ruby ex_script_ni
213# define ex_rubydo ex_ni
214# define ex_rubyfile ex_ni
215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifndef FEAT_KEYMAP
217# define ex_loadkeymap ex_ni
218#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void ex_swapname(exarg_T *eap);
220static void ex_syncbind(exarg_T *eap);
221static void ex_read(exarg_T *eap);
222static void ex_pwd(exarg_T *eap);
223static void ex_equal(exarg_T *eap);
224static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000227#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#else
230# define ex_winpos ex_ni
231#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ex_operators(exarg_T *eap);
233static void ex_put(exarg_T *eap);
64-bitmane08f10a2025-03-18 22:14:34 +0100234static void ex_iput(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100235static void ex_copymove(exarg_T *eap);
236static void ex_submagic(exarg_T *eap);
237static void ex_join(exarg_T *eap);
238static void ex_at(exarg_T *eap);
239static void ex_bang(exarg_T *eap);
240static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_wundo(exarg_T *eap);
243static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200244#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redo(exarg_T *eap);
246static void ex_later(exarg_T *eap);
247static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100249static void ex_redrawtabline(exarg_T *eap);
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200250static void ex_redrawtabpanel(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ex_startinsert(exarg_T *eap);
254static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ex_checkpath(exarg_T *eap);
257static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#else
259# define ex_findpat ex_ni
260# define ex_checkpath ex_ni
261#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200262#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#else
265# define ex_psearch ex_ni
266#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ex_tag(exarg_T *eap);
268static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200270# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_break ex_ni
272# define ex_breakadd ex_ni
273# define ex_breakdel ex_ni
274# define ex_breaklist ex_ni
275# define ex_call ex_ni
276# define ex_catch ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000277# define ex_class ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200279# define ex_continue ex_ni
280# define ex_debug ex_ni
281# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200282# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_echo ex_ni
286# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200288# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200289# define ex_endfunction ex_ni
290# define ex_endif ex_ni
291# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200293# define ex_eval ex_ni
294# define ex_execute ex_ni
295# define ex_finally ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000296# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_finish ex_ni
298# define ex_function ex_ni
299# define ex_if ex_ni
300# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200301# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200302# define ex_lockvar ex_ni
303# define ex_oldfiles ex_ni
304# define ex_options ex_ni
305# define ex_packadd ex_ni
306# define ex_packloadall ex_ni
307# define ex_return ex_ni
308# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_throw ex_ni
310# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000311# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200313# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314# define ex_import ex_ni
315# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200317#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318# define ex_loadview ex_ni
319#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200320#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321# define ex_viminfo ex_ni
322#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_filetype(exarg_T *eap);
325static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000327# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328# define ex_diffpatch ex_ni
329# define ex_diffgetput ex_ni
330# define ex_diffsplit ex_ni
331# define ex_diffthis ex_ni
332# define ex_diffupdate ex_ni
333#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_nohlsearch ex_ni
339# define ex_match ex_ni
340#endif
341#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_X ex_ni
345#endif
346#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100347static void ex_fold(exarg_T *eap);
348static void ex_foldopen(exarg_T *eap);
349static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#else
351# define ex_fold ex_ni
352# define ex_foldopen ex_ni
353# define ex_folddo ex_ni
354#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100355#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356# define ex_language ex_ni
357#endif
358#ifndef FEAT_SIGNS
359# define ex_sign ex_ni
360#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000361#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200362# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000363# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200364# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
Bram Moolenaar05159a02005-02-26 23:04:13 +0000367#ifndef FEAT_PROFILE
368# define ex_profile ex_ni
369#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200370#ifndef FEAT_TERMINAL
371# define ex_terminal ex_ni
372#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200373#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
374# define ex_xrestore ex_ni
375#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100376#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200377# define ex_popupclear ex_ni
378#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Declare cmdnames[].
382 */
383#define DO_DECLARE_EXCMD
384#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200385#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static char_u dollar_command[2] = {'$', 0};
388
389
390#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392typedef struct
393{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100394 char_u *line; // command line
395 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396} wcmd_T;
397
398/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 garray_T *lines_gap; // growarray with line info
406 int current_line; // last read line from growarray
407 int repeating; // TRUE when looping a second time
408 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100409 char_u *(*lc_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 void *cookie;
411};
412
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200413static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static int store_loop_line(garray_T *gap, char_u *line);
415static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000418struct dbg_stuff
419{
420 int trylevel;
421 int force_abort;
422 except_T *caught_stack;
423 char_u *vv_exception;
424 char_u *vv_throwpoint;
425 int did_emsg;
426 int got_int;
427 int did_throw;
428 int need_rethrow;
429 int check_cstack;
430 except_T *current_exception;
431};
432
Bram Moolenaared203462004-06-16 11:19:22 +0000433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100434save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000435{
436 dsp->trylevel = trylevel; trylevel = 0;
437 dsp->force_abort = force_abort; force_abort = FALSE;
438 dsp->caught_stack = caught_stack; caught_stack = NULL;
439 dsp->vv_exception = v_exception(NULL);
440 dsp->vv_throwpoint = v_throwpoint(NULL);
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000443 dsp->did_emsg = did_emsg; did_emsg = FALSE;
444 dsp->got_int = got_int; got_int = FALSE;
445 dsp->did_throw = did_throw; did_throw = FALSE;
446 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
447 dsp->check_cstack = check_cstack; check_cstack = FALSE;
448 dsp->current_exception = current_exception; current_exception = NULL;
449}
450
451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100452restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000453{
454 suppress_errthrow = FALSE;
455 trylevel = dsp->trylevel;
456 force_abort = dsp->force_abort;
457 caught_stack = dsp->caught_stack;
458 (void)v_exception(dsp->vv_exception);
459 (void)v_throwpoint(dsp->vv_throwpoint);
460 did_emsg = dsp->did_emsg;
461 got_int = dsp->got_int;
462 did_throw = dsp->did_throw;
463 need_rethrow = dsp->need_rethrow;
464 check_cstack = dsp->check_cstack;
465 current_exception = dsp->current_exception;
466}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467#endif
468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469/*
Colin Kennedy65e580b2024-03-26 18:29:30 +0100470 * Check if ffname differs from fnum.
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200471 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid
472 * buffer ID.
Colin Kennedy65e580b2024-03-26 18:29:30 +0100473 * ffname is a full path to where a buffer lives on-disk or would live on-disk.
474 *
475 */
476 static int
477is_other_file(int fnum, char_u *ffname)
478{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200479 if (fnum != 0)
480 {
481 if (fnum == curbuf->b_fnum)
482 return FALSE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100483
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200484 return TRUE;
485 }
Colin Kennedy65e580b2024-03-26 18:29:30 +0100486
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200487 if (ffname == NULL)
488 return TRUE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100489
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200490 if (*ffname == NUL)
491 return FALSE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100492
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200493 // TODO: Need a reliable way to know whether a buffer is meant to live
494 // on-disk !curbuf->b_dev_valid is not always available (example: missing
495 // on Windows)
496 if (curbuf->b_sfname != NULL
497 && *curbuf->b_sfname != NUL)
498 // This occurs with unsaved buffers. In which case `ffname` actually
499 // corresponds to curbuf->b_sfname
500 return fnamecmp(ffname, curbuf->b_sfname) != 0;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100501
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200502 return otherfile(ffname);
Colin Kennedy65e580b2024-03-26 18:29:30 +0100503}
504
505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
507 * command is given.
508 */
509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100510do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100511 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512{
513 int save_msg_scroll;
514 int prev_msg_row;
515 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100516 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000517
518 if (improved)
519 exmode_active = EXMODE_VIM;
520 else
521 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100522 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100523 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000524
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100525 // When using ":global /pat/ visual" and then "Q" we return to continue
526 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if (global_busy)
528 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
530 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 ++RedrawingDisabled; // don't redisplay the window
532 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100534 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 ++hold_gui_events;
536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
Bram Moolenaar32526b32019-01-19 17:43:09 +0100538 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 while (exmode_active)
540 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100541 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000542 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
543 {
544 exmode_active = FALSE;
545 break;
546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 msg_scroll = TRUE;
548 need_wait_return = FALSE;
549 ex_pressedreturn = FALSE;
550 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100551 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 prev_msg_row = msg_row;
553 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (improved)
555 {
556 cmdline_row = msg_row;
557 do_cmdline(NULL, getexline, NULL, 0);
558 }
559 else
560 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
561 lines_left = Rows - 1;
562
Bram Moolenaardf177f62005-02-22 08:39:57 +0000563 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100564 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000566 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000567 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000568 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000570 if (ex_pressedreturn)
571 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100572 // go up one line, to overwrite the ":<CR>" line, so the
573 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000574 msg_row = prev_msg_row;
575 if (prev_msg_row == Rows - 1)
576 msg_row--;
577 }
578 msg_col = 0;
579 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
580 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100583 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000584 {
585 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000586 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000587 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000588 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 }
591
592#ifdef FEAT_GUI
593 --hold_gui_events;
594#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100595 if (RedrawingDisabled > 0)
596 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100598 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 need_wait_return = FALSE;
600 msg_scroll = save_msg_scroll;
601}
602
603/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200604 * Print the executed command for when 'verbose' is set.
605 * When "lnum" is 0 only print the command.
606 */
607 static void
608msg_verbose_cmd(linenr_T lnum, char_u *cmd)
609{
610 ++no_wait_return;
611 verbose_enter_scroll();
612
613 if (lnum == 0)
614 smsg(_("Executing: %s"), cmd);
615 else
616 smsg(_("line %ld: %s"), (long)lnum, cmd);
617 if (msg_silent == 0)
618 msg_puts("\n"); // don't overwrite this
619
620 verbose_leave_scroll();
621 --no_wait_return;
622}
623
624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 * Execute a simple command line. Used for translated commands like "*".
626 */
627 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100628do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 return do_cmdline(cmd, NULL, NULL,
631 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
632}
633
634/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100635 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
636 * This allows for using a range without ":" in Vim9 script.
637 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200638 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100639do_cmd_argument(char_u *cmd)
640{
641 return do_cmdline(cmd, NULL, NULL,
642 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
643}
644
645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 * do_cmdline(): execute one Ex command line
647 *
648 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 * 2. Split up in parts separated with '|'.
651 *
652 * This function can be called recursively!
653 *
654 * flags:
655 * DOCMD_VERBOSE - The command will be included in the error message.
656 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100657 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 * DOCMD_KEYTYPED - Don't reset KeyTyped.
659 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
660 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
661 *
662 * return FAIL if cmdline could not be executed, OK otherwise
663 */
664 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100665do_cmdline(
666 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200667 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100669 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100671 char_u *next_cmdline; // next cmd to execute
672 char_u *cmdline_copy = NULL; // copy of cmd line
673 int used_getline = FALSE; // used "fgetline" to obtain command
674 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100677 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 int retval = OK;
679#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100680 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100681 garray_T lines_ga; // keep lines for ":while"/":for"
682 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200683 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100684 char_u *fname = NULL; // function or script name
685 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
686 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
687 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200689 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200690 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100692 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200693 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000695 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100699# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700# define cmd_cookie cookie
701#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200703#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
705 // location for storing error messages to be converted to an exception.
706 // This ensures that the do_errthrow() call in do_one_cmd() does not
707 // combine the messages stored by an earlier invocation of do_one_cmd()
708 // with the command name of the later one. This would happen when
709 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 saved_msg_list = msg_list;
711 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#endif
713
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100714 // It's possible to create an endless loop with ":execute", catch that
715 // here. The value of 200 allows nested function calls, ":source", etc.
716 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100717 if (call_depth >= 200
718#ifdef FEAT_EVAL
719 && call_depth >= p_mfd
720#endif
721 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000723 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100725 // When converting to an exception, we do not include the command name
726 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100727 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 msg_list = saved_msg_list;
729#endif
730 return FAIL;
731 }
732 ++call_depth;
733
734#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100735 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000737 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100741 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100742 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000743 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 ++ex_nesting_level;
745
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100746 // Get the function or script name and the address where the next breakpoint
747 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000748 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
750 fname = func_name(real_cookie);
751 breakpoint = func_breakpoint(real_cookie);
752 dbg_tick = func_dbg_tick(real_cookie);
753 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100756 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 breakpoint = source_breakpoint(real_cookie);
758 dbg_tick = source_dbg_tick(real_cookie);
759 }
760
761 /*
762 * Initialize "force_abort" and "suppress_errthrow" at the top level.
763 */
764 if (!recursive)
765 {
766 force_abort = FALSE;
767 suppress_errthrow = FALSE;
768 }
769
770 /*
771 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000772 * exception handling (used when debugging). Otherwise clear it to avoid
773 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000775 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000776 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000777 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200778 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 initial_trylevel = trylevel;
781
782 /*
783 * "did_throw" will be set to TRUE when an exception is being thrown.
784 */
785 did_throw = FALSE;
786#endif
787 /*
788 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * If force_abort is set, we cancel everything.
791 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100792#ifdef FEAT_EVAL
793 did_emsg_cumul += did_emsg;
794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 did_emsg = FALSE;
796
797 /*
798 * KeyTyped is only set when calling vgetc(). Reset it here when not
799 * calling vgetc() (sourced command lines).
800 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100801 if (!(flags & DOCMD_KEYTYPED)
802 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 KeyTyped = FALSE;
804
805 /*
806 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000807 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 * - for multiple commands on one line, separated with '|'
809 * - when repeating until there are no more lines (for ":source")
810 */
811 next_cmdline = cmdline;
812 do
813 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816#endif
817
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100818 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (next_cmdline == NULL
820#ifdef FEAT_EVAL
821 && !force_abort
822 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000823 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#endif
825 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100826 {
827#ifdef FEAT_EVAL
828 did_emsg_cumul += did_emsg;
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000834 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * 3. If a line is given: Make a copy, so we can mess with it.
837 */
838
839#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000841 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100843 // Each '|' separated command is stored separately in lines_ga, to
844 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100845 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100847 // Check if a function has returned or, unless it has an unclosed
848 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000852 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853 func_line_end(real_cookie);
854# endif
855 if (func_has_ended(real_cookie))
856 {
857 retval = FAIL;
858 break;
859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000862 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864 script_line_end();
865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100867 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 {
870 retval = FAIL;
871 break;
872 }
873
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (breakpoint != NULL && dbg_tick != NULL
876 && *dbg_tick != debug_tick)
877 {
878 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100879 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100880 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 *dbg_tick = debug_tick;
882 }
883
884 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100885 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100887 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100889 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100891 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100892 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100894 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100895 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 *dbg_tick = debug_tick;
897 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000899 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900 {
901 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100902 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100903 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904 script_line_start();
905 }
906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#endif
909
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100910 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (next_cmdline == NULL)
912 {
913 /*
914 * Need to set msg_didout for the first line after an ":if",
915 * otherwise the ":if" will be overwritten.
916 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100917 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100919 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920#ifdef FEAT_EVAL
921 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
922#else
923 0
924#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100925 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
926 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100928 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100929 // returned for the end of a sourced file or executed function
930 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (KeyTyped && !(flags & DOCMD_REPEAT))
932 need_wait_return = FALSE;
933 retval = FAIL;
934 break;
935 }
936 used_getline = TRUE;
937
938 /*
939 * Keep the first typed line. Clear it when more lines are typed.
940 */
941 if (flags & DOCMD_KEEPLINE)
942 {
943 vim_free(repeat_cmdline);
944 if (count == 0)
945 repeat_cmdline = vim_strsave(next_cmdline);
946 else
947 repeat_cmdline = NULL;
948 }
949 }
950
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100951 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 else if (cmdline_copy == NULL)
953 {
954 next_cmdline = vim_strsave(next_cmdline);
955 if (next_cmdline == NULL)
956 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200957 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = FAIL;
959 break;
960 }
961 }
962 cmdline_copy = next_cmdline;
963
964#ifdef FEAT_EVAL
965 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200966 * Inside a while/for loop, and when the command looks like a ":while"
967 * or ":for", the line is stored, because we may need it later when
968 * looping.
969 *
970 * When there is a '|' and another command, it is stored separately,
971 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000972 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200973 *
974 * Pass a different "fgetline" function to do_one_cmd() below,
975 * that it stores lines in or reads them from "lines_ga". Makes it
976 * possible to define a function inside a while/for loop and handles
977 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200979 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200981 cmd_getline = get_loop_line;
982 cmd_cookie = (void *)&cmd_loop_cookie;
983 cmd_loop_cookie.lines_gap = &lines_ga;
984 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100985 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200986 cmd_loop_cookie.cookie = cookie;
987 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
988
989 // Save the current line when encountering it the first time.
990 if (current_line == lines_ga.ga_len
991 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
993 retval = FAIL;
994 break;
995 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200996 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200998 else
999 {
1000 cmd_getline = fgetline;
1001 cmd_cookie = cookie;
1002 }
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 did_endif = FALSE;
1005#endif
1006
1007 if (count++ == 0)
1008 {
1009 /*
1010 * All output from the commands is put below each other, without
1011 * waiting for a return. Don't do this when executing commands
1012 * from a script or when being called recursive (e.g. for ":e
1013 * +command file").
1014 */
1015 if (!(flags & DOCMD_NOWAIT) && !recursive)
1016 {
1017 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001018 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001020 msg_scroll = TRUE; // put messages below each other
1021 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001023 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
1025 }
1026
Bram Moolenaar823654b2020-05-29 23:03:09 +02001027 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001028 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 /*
1031 * 2. Execute one '|' separated command.
1032 * do_one_cmd() will return NULL if there is no trailing '|'.
1033 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1034 */
1035 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001036 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037#ifdef FEAT_EVAL
1038 &cstack,
1039#endif
1040 cmd_getline, cmd_cookie);
1041 --recursive;
1042
1043#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001045 // Use "current_line" from "cmd_loop_cookie", it may have been
1046 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#endif
1049
1050 if (next_cmdline == NULL)
1051 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001052 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 /*
1055 * If the command was typed, remember it for the ':' register.
1056 * Do this AFTER executing the command to make :@: work.
1057 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001058 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 && new_last_cmdline != NULL)
1060 {
1061 vim_free(last_cmdline);
1062 last_cmdline = new_last_cmdline;
1063 new_last_cmdline = NULL;
1064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 else
1067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // need to copy the command after the '|' to cmdline_copy, for the
1069 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001070 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 next_cmdline = cmdline_copy;
1072 }
1073
1074
1075#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001078 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001080 {
1081 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
1087 ++current_line;
1088
1089 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 * An ":endwhile", ":endfor" and ":continue" is handled here.
1091 * If we were executing commands, jump back to the ":while" or
1092 * ":for".
1093 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001099 // Jump back to the matching ":while" or ":for". Be careful
1100 // not to use a cs_line[] from an entry that isn't a ":while"
1101 // or ":for": It would make "current_line" invalid and can
1102 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (!did_emsg && !got_int && !did_throw
1104 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001105 && (cstack.cs_flags[cstack.cs_idx]
1106 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 && cstack.cs_line[cstack.cs_idx] >= 0
1108 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1109 {
1110 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001111 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001113 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001115 // Check for the next breakpoint at or after the ":while"
1116 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001117 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001120 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 fname,
1122 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1123 *dbg_tick = debug_tick;
1124 }
1125 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001126 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001128 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001130 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1131 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133 }
1134
1135 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001141 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 }
1144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001145 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001146 if (breakpoint != NULL && has_watchexpr())
1147 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001148 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001149 *dbg_tick = debug_tick;
1150 }
1151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 /*
1153 * When not inside any ":while" loop, clear remembered lines.
1154 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
1157 if (lines_ga.ga_len > 0)
1158 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001159 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1161 free_cmdlines(&lines_ga);
1162 }
1163 current_line = 0;
1164 }
1165
1166 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1168 * being restored at the ":endtry". Reset them here and set the
1169 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1170 * This includes the case where a missing ":endif", ":endwhile" or
1171 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 cstack.cs_lflags &= ~CSL_HAD_FINA;
1176 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1177 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 did_throw ? (void *)current_exception : NULL);
1179 did_emsg = got_int = did_throw = FALSE;
1180 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1181 }
1182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001183 // Update global "trylevel" for recursive calls to do_cmdline() from
1184 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 trylevel = initial_trylevel + cstack.cs_trylevel;
1186
1187 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001188 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 * files) is aborted because of an error, an interrupt, or an uncaught
1190 * exception, cancel everything. If it is left normally, reset
1191 * force_abort to get the non-EH compatible abortion behavior for
1192 * the rest of the script.
1193 */
1194 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1195 force_abort = FALSE;
1196
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001197 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001199#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201 }
1202 /*
1203 * Continue executing command lines when:
1204 * - no CTRL-C typed, no aborting error, no exception thrown or try
1205 * conditionals need to be checked for executing finally clauses or
1206 * catching an interrupt exception
1207 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 * looping for ":source" command or function call.
1210 */
1211 while (!((got_int
1212#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001213 || (did_emsg && (force_abort || in_vim9script()))
1214 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#endif
1216 )
1217#ifdef FEAT_EVAL
1218 && cstack.cs_trylevel == 0
1219#endif
1220 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001221 && !(did_emsg
1222#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001223 // Keep going when inside try/catch, so that the error can be
1224 // deal with, except when it is a syntax error, it may cause
1225 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001226 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1227#endif
1228 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001229 && (getline_equal(fgetline, cookie, getexmodeline)
1230 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 && (next_cmdline != NULL
1232#ifdef FEAT_EVAL
1233 || cstack.cs_idx >= 0
1234#endif
1235 || (flags & DOCMD_REPEAT)));
1236
1237 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001238 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#ifdef FEAT_EVAL
1240 free_cmdlines(&lines_ga);
1241 ga_clear(&lines_ga);
1242
1243 if (cstack.cs_idx >= 0)
1244 {
1245 /*
1246 * If a sourced file or executed function ran to its end, report the
1247 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001248 * In Vim9 script do not give a second error, executing aborts after
1249 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001251 if (!got_int && !did_throw && !aborting()
1252 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001253 && ((getline_equal(fgetline, cookie, getsourceline)
1254 && !source_finished(fgetline, cookie))
1255 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 && !func_has_ended(real_cookie))))
1257 {
1258 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001259 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001261 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001262 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001263 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001265 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267
1268 /*
1269 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1270 * ":endtry" in a sourced file or executed function. If the try
1271 * conditional is in its finally clause, ignore anything pending.
1272 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001273 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 */
1275 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001276 {
1277 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1278
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001279 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001280 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001281 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1282 &cstack.cs_looplevel);
1283 }
1284 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 trylevel = initial_trylevel;
1286 }
1287
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001288 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1289 // lack was reported above and the error message is to be converted to an
1290 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001291 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ? (char_u *)"endfunction" : (char_u *)NULL);
1293
1294 if (trylevel == 0)
1295 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001296 // Just in case did_throw got set but current_exception wasn't.
1297 if (current_exception == NULL)
1298 did_throw = FALSE;
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 /*
1301 * When an exception is being thrown out of the outermost try
1302 * conditional, discard the uncaught exception, disable the conversion
1303 * of interrupts or errors to exceptions, and ensure that no more
1304 * commands are executed.
1305 */
1306 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001307 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308
1309 /*
1310 * On an interrupt or an aborting error not converted to an exception,
1311 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001312 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * when force_abort is set and did_emsg unset in case of an interrupt
1314 * from a finally clause after an error.
1315 */
1316 else if (got_int || (did_emsg && force_abort))
1317 suppress_errthrow = TRUE;
1318 }
1319
1320 /*
1321 * The current cstack will be freed when do_cmdline() returns. An uncaught
1322 * exception will have to be rethrown in the previous cstack. If a function
1323 * has just returned or a script file was just finished and the previous
1324 * cstack belongs to the same function or, respectively, script file, it
1325 * will have to be checked for finally clauses to be executed due to the
1326 * ":return" or ":finish". This is done in do_one_cmd().
1327 */
1328 if (did_throw)
1329 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001330 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 && ex_nesting_level > func_level(real_cookie) + 1))
1334 {
1335 if (!did_throw)
1336 check_cstack = TRUE;
1337 }
1338 else
1339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001340 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001341 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 --ex_nesting_level;
1343 /*
1344 * Go to debug mode when returning from a function in which we are
1345 * single-stepping.
1346 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001347 if ((getline_equal(fgetline, cookie, getsourceline)
1348 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001350 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 ? (char_u *)_("End of sourced file")
1352 : (char_u *)_("End of function"));
1353 }
1354
1355 /*
1356 * Restore the exception environment (done after returning from the
1357 * debugger).
1358 */
1359 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001360 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
1362 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001363
1364 // Cleanup if "cs_emsg_silent_list" remains.
1365 if (cstack.cs_emsg_silent_list != NULL)
1366 {
1367 eslist_T *elem, *temp;
1368
1369 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1370 {
1371 temp = elem->next;
1372 vim_free(elem);
1373 }
1374 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001375#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 /*
1378 * If there was too much output to fit on the command line, ask the user to
1379 * hit return before redrawing the screen. With the ":global" command we do
1380 * this only once after the command is finished.
1381 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001382 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001384 if (RedrawingDisabled > 0)
1385 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 --no_wait_return;
1387 msg_scroll = FALSE;
1388
1389 /*
1390 * When just finished an ":if"-":else" which was typed, no need to
1391 * wait for hit-return. Also for an error situation.
1392 */
1393 if (retval == FAIL
1394#ifdef FEAT_EVAL
1395 || (did_endif && KeyTyped && !did_emsg)
1396#endif
1397 )
1398 {
1399 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001400 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402 else if (need_wait_return)
1403 {
1404 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001405 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * here should not overwrite the command that may be shown before
1407 * doing that.
1408 */
1409 msg_didout |= msg_didout_before_start;
1410 wait_return(FALSE);
1411 }
1412 }
1413
Bram Moolenaar2a942252012-11-28 23:03:07 +01001414#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001415 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001416#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 /*
1418 * Reset if_level, in case a sourced script file contains more ":if" than
1419 * ":endif" (could be ":if x | foo | endif").
1420 */
1421 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001422#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 --call_depth;
1425 return retval;
1426}
1427
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001428#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001429/*
1430 * Handle when "did_throw" is set after executing commands.
1431 */
1432 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001433handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001434{
1435 char *p = NULL;
1436 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001437 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001438
1439 /*
1440 * If the uncaught exception is a user exception, report it as an
1441 * error. If it is an error exception, display the saved error
1442 * message now. For an interrupt exception, do nothing; the
1443 * interrupt message is given elsewhere.
1444 */
1445 switch (current_exception->type)
1446 {
1447 case ET_USER:
1448 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001449 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001450 current_exception->value);
1451 p = (char *)vim_strsave(IObuff);
1452 break;
1453 case ET_ERROR:
1454 messages = current_exception->messages;
1455 current_exception->messages = NULL;
1456 break;
1457 case ET_INTERRUPT:
1458 break;
1459 }
1460
1461 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1462 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001463 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001464 current_exception->throw_name = NULL;
1465
1466 discard_current_exception(); // uses IObuff if 'verbose'
LemonBoy749ba0f2024-07-04 19:23:16 +02001467
1468 // If "silent!" is active the uncaught exception is not fatal.
1469 if (emsg_silent == 0)
1470 {
1471 suppress_errthrow = TRUE;
1472 force_abort = TRUE;
1473 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001474
1475 if (messages != NULL)
1476 {
1477 do
1478 {
1479 msglist_T *next = messages->next;
1480 int save_compiling = estack_compiling;
1481
1482 estack_compiling = messages->msg_compiling;
1483 emsg(messages->msg);
1484 vim_free(messages->msg);
1485 vim_free(messages->sfile);
1486 vim_free(messages);
1487 messages = next;
1488 estack_compiling = save_compiling;
1489 }
1490 while (messages != NULL);
1491 }
1492 else if (p != NULL)
1493 {
1494 emsg(p);
1495 vim_free(p);
1496 }
1497 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001498 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001499 estack_pop();
1500}
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001503 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 */
1505 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001506get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001508 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 wcmd_T *wp;
1510 char_u *line;
1511
1512 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1513 {
1514 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001515 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001517 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001518 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001519 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001521 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001522 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 ++cp->current_line;
1524
1525 return line;
1526 }
1527
1528 KeyTyped = FALSE;
1529 ++cp->current_line;
1530 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001531 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 return vim_strsave(wp->line);
1533}
1534
1535/*
1536 * Store a line in "gap" so that a ":while" loop can execute it again.
1537 */
1538 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001539store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
1541 if (ga_grow(gap, 1) == FAIL)
1542 return FAIL;
1543 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001544 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 return OK;
1547}
1548
1549/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001550 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
1552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001553free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 while (gap->ga_len > 0)
1556 {
1557 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1558 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 }
1560}
1561#endif
1562
1563/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001564 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1565 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001568getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001569 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001570 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001571 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001574 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001575 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001577 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1578 // function that's originally used to obtain the lines. This may be
1579 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001580 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001581 cp = (struct loop_cookie *)cookie;
1582 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001584 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 cp = cp->cookie;
1586 }
1587 return gp == func;
1588#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001589 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590#endif
1591}
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001594 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * getline function. Otherwise return "cookie".
1596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001598getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001599 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001600 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
Bram Moolenaar13505972019-01-24 15:04:48 +01001602#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001603 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001604 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1607 // cookie that's originally used to obtain the lines. This may be nested
1608 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001610 cp = (struct loop_cookie *)cookie;
1611 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001613 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 cp = cp->cookie;
1615 }
1616 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001617#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001622#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001623/*
1624 * Get the next line source line without advancing.
1625 */
1626 char_u *
1627getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001628 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001629 void *cookie) // argument for fgetline()
1630{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001631 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001632 struct loop_cookie *cp;
1633 wcmd_T *wp;
1634
1635 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1636 // cookie that's originally used to obtain the lines. This may be nested
1637 // several levels.
1638 gp = fgetline;
1639 cp = (struct loop_cookie *)cookie;
1640 while (gp == get_loop_line)
1641 {
1642 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1643 {
1644 // executing lines a second time, use the stored copy
1645 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1646 return wp->line;
1647 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001648 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001649 cp = cp->cookie;
1650 }
1651 if (gp == getsourceline)
1652 return source_nextline(cp);
1653 return NULL;
1654}
1655#endif
1656
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001657
1658/*
1659 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1660 * ":bwipeout", etc.
1661 * Returns the buffer number.
1662 */
1663 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001664compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665{
1666 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001667 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001668 int count = offset;
1669
1670 buf = firstbuf;
1671 while (buf->b_next != NULL && buf->b_fnum < lnum)
1672 buf = buf->b_next;
1673 while (count != 0)
1674 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001675 count += (offset < 0) ? 1 : -1;
1676 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1677 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001678 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001679 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001680 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001681 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001682 while (buf->b_ml.ml_mfp == NULL)
1683 {
1684 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1685 if (nextbuf == NULL)
1686 break;
1687 buf = nextbuf;
1688 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001689 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001690 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001691 if (addr_type == ADDR_LOADED_BUFFERS)
1692 while (buf->b_ml.ml_mfp == NULL)
1693 {
1694 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1695 if (nextbuf == NULL)
1696 break;
1697 buf = nextbuf;
1698 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699 return buf->b_fnum;
1700}
1701
Bram Moolenaarb7316892019-05-01 18:08:42 +02001702/*
1703 * Return the window number of "win".
1704 * When "win" is NULL return the number of windows.
1705 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001706 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001707current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001708{
1709 win_T *wp;
1710 int nr = 0;
1711
Bram Moolenaar29323592016-07-24 22:04:11 +02001712 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001713 {
1714 ++nr;
1715 if (wp == win)
1716 break;
1717 }
1718 return nr;
1719}
1720
1721 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001722current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001723{
1724 tabpage_T *tp;
1725 int nr = 0;
1726
Bram Moolenaar29323592016-07-24 22:04:11 +02001727 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001728 {
1729 ++nr;
1730 if (tp == tab)
1731 break;
1732 }
1733 return nr;
1734}
1735
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001736 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001737comment_start(char_u *p, int starts_with_colon UNUSED)
1738{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001739 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001740 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001741 return *p == '"';
1742}
1743
Bram Moolenaarf240e182014-11-27 18:33:02 +01001744# define CURRENT_WIN_NR current_win_nr(curwin)
1745# define LAST_WIN_NR current_win_nr(NULL)
1746# define CURRENT_TAB_NR current_tab_nr(curtab)
1747# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749/*
1750 * Execute one Ex command.
1751 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001752 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1753 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 *
1755 * 1. skip comment lines and leading space
1756 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001757 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001759 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001760 * 6. parse arguments
1761 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001763 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 *
1765 * This function may be called recursively!
1766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001768do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001769 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001770 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001772 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001774 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001775 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001777 char_u *p;
1778 linenr_T lnum;
1779 long n;
1780 char *errormsg = NULL; // error message
1781 char_u *after_modifier = NULL;
1782 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001783 cmdmod_T save_cmdmod;
1784 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001785 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001786 int ni; // set when Not Implemented
1787 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001788 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001789 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001790#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001791 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001792#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001793 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001794 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaara80faa82020-04-12 19:37:17 +02001796 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 ea.line1 = 1;
1798 ea.line2 = 1;
1799#ifdef FEAT_EVAL
1800 ++ex_nesting_level;
1801#endif
1802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001803 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (quitmore
1805#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001806 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001807 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001808#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001809 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001810 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 --quitmore;
1812
1813 /*
1814 * Reset browse, confirm, etc.. They are restored when returning, for
1815 * recursive calls.
1816 */
1817 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001819 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001820 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1821 goto doend;
1822
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001823/*
1824 * 1. Skip comment lines and leading white space and colons.
1825 * 2. Handle command modifiers.
1826 */
1827 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001829 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001830 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001831 ea.cookie = cookie;
1832#ifdef FEAT_EVAL
1833 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001834 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001836 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001837 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001838 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001839 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841#ifdef FEAT_EVAL
1842 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1843 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1844#else
1845 ea.skip = (if_level > 0);
1846#endif
1847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001849 * 3. Skip over the range to find the command. Let "p" point to after it.
1850 *
1851 * We need the command to know what kind of range it uses.
1852 */
1853 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001854
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001855 // In Vim9 script a colon is required before the range. This may also be
1856 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001857 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001858 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001859 {
1860 may_have_range = FALSE;
1861 for (p = ea.cmd; p >= *cmdlinep; --p)
1862 {
1863 if (*p == ':')
1864 may_have_range = TRUE;
1865 if (p < ea.cmd && !VIM_ISWHITE(*p))
1866 break;
1867 }
1868 }
1869 else
1870 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001871 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001872 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001874#ifdef FEAT_EVAL
1875 // Handle ":export" - it functions almost like a command modifier.
1876 // ":export var Name: type"
1877 // ":export def Name(..."
1878 // etc.
1879 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1880 is_export = TRUE;
1881#endif
1882
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001883 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001884 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001885 if (ea.cmd == cmd + 1 && *cmd == '$')
1886 // should be "$VAR = val"
1887 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001888#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001889 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001890#else
1891 p = find_ex_command(&ea, NULL, NULL, NULL);
1892#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001893 if (ea.cmdidx == CMD_SIZE)
1894 {
1895 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1896
1897 // If a ':' before the range is missing, give a clearer error
1898 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001899 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001900 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001901 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001902 goto doend;
1903 }
1904 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001908
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001909#ifdef FEAT_EVAL
1910# ifdef FEAT_PROFILE
1911 // Count this line for profiling if skip is TRUE.
1912 if (do_profiling == PROF_YES
1913 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1914 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1915 {
1916 int skip = did_emsg || got_int || did_throw;
1917
1918 if (ea.cmdidx == CMD_catch)
1919 skip = !skip && !(cstack->cs_idx >= 0
1920 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1921 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1922 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1923 skip = skip || !(cstack->cs_idx >= 0
1924 && !(cstack->cs_flags[cstack->cs_idx]
1925 & (CSF_ACTIVE | CSF_TRUE)));
1926 else if (ea.cmdidx == CMD_finally)
1927 skip = FALSE;
1928 else if (ea.cmdidx != CMD_endif
1929 && ea.cmdidx != CMD_endfor
1930 && ea.cmdidx != CMD_endtry
1931 && ea.cmdidx != CMD_endwhile)
1932 skip = ea.skip;
1933
1934 if (!skip)
1935 {
1936 if (getline_equal(fgetline, cookie, get_func_line))
1937 func_line_exec(getline_cookie(fgetline, cookie));
1938 else if (getline_equal(fgetline, cookie, getsourceline))
1939 script_line_exec();
1940 }
1941 }
1942# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001943#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001944
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001945 ea.cmd = cmd;
1946
1947#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001948 // May go to debug mode. If this happens and the ">quit" debug command is
1949 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001950 dbg_check_breakpoint(&ea);
1951 if (!ea.skip && got_int)
1952 {
1953 ea.skip = TRUE;
1954 (void)do_intthrow(cstack);
1955 }
1956#endif
1957
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001958/*
1959 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 *
1961 * where 'addr' is:
1962 *
1963 * % (entire file)
1964 * $ [+-NUM]
1965 * 'x [+-NUM] (where x denotes a currently defined mark)
1966 * . [+-NUM]
1967 * [+-NUM]..
1968 * NUM
1969 *
1970 * The ea.cmd pointer is updated to point to the first character following the
1971 * range spec. If an initial address is found, but no second, the upper bound
1972 * is equal to the lower.
1973 */
1974
Bram Moolenaar25190db2019-05-04 15:05:28 +02001975 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001976 if (!IS_USER_CMDIDX(ea.cmdidx))
1977 {
1978 if (ea.cmdidx != CMD_SIZE)
1979 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1980 else
1981 ea.addr_type = ADDR_LINES;
1982
Bram Moolenaar25190db2019-05-04 15:05:28 +02001983 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001984 if (ea.cmdidx == CMD_wincmd && p != NULL)
1985 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001986#ifdef FEAT_QUICKFIX
1987 // :.cc in quickfix window uses line number
1988 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1989 ea.addr_type = ADDR_OTHER;
1990#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001991 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001992
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001993 if (!may_have_range)
1994 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001995 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1996 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001999 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 */
2001
2002 /*
2003 * Skip ':' and any white space
2004 */
2005 ea.cmd = skipwhite(ea.cmd);
2006 while (*ea.cmd == ':')
2007 ea.cmd = skipwhite(ea.cmd + 1);
2008
2009 /*
2010 * If we got a line, but no command, then go to the line.
2011 * If we find a '|' or '\n' we set ea.nextcmd.
2012 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002013 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2014 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
2016 /*
2017 * strange vi behaviour:
2018 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002019 * ":3|..." prints line 3 (not in Vim9 script)
2020 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002022 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002024 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 goto doend;
2026 }
2027
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002028 // If this looks like an undefined user command and there are CmdUndefined
2029 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002030 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2031 && ASCII_ISUPPER(*ea.cmd)
2032 && has_cmdundefined())
2033 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002034 int ret;
2035
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002036 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002037 while (ASCII_ISALNUM(*p))
2038 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002039 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002040 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2041 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002042 // If the autocommands did something and didn't cause an error, try
2043 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002044 p = (ret
2045#ifdef FEAT_EVAL
2046 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002047#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002049 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (p == NULL)
2052 {
2053 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002054 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 goto doend;
2056 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002057 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002058 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2059 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
2061 errormsg = uc_fun_cmd();
2062 goto doend;
2063 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 if (ea.cmdidx == CMD_SIZE)
2066 {
2067 if (!ea.skip)
2068 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002069 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // If the modifier was parsed OK the error must be in the
2073 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002074 if (after_modifier != NULL)
2075 append_command(after_modifier);
2076 else
2077 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002078 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002079 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002081 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 goto doend;
2084 }
2085
Bram Moolenaar958636c2014-10-21 20:01:58 +02002086 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2087 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002088#ifdef HAVE_EX_SCRIPT_NI
2089 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2090#endif
2091 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093#ifndef FEAT_EVAL
2094 /*
2095 * When the expression evaluation is disabled, recognize the ":if" and
2096 * ":endif" commands and ignore everything in between it.
2097 */
2098 if (ea.cmdidx == CMD_if)
2099 ++if_level;
2100 if (if_level)
2101 {
2102 if (ea.cmdidx == CMD_endif)
2103 --if_level;
2104 goto doend;
2105 }
2106
2107#endif
2108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002110 if (*p == '!' && ea.cmdidx != CMD_substitute
2111 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
2113 ++p;
2114 ea.forceit = TRUE;
2115 }
2116 else
2117 ea.forceit = FALSE;
2118
2119/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002120 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002122 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002123 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 if (!ea.skip)
2126 {
2127#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002128 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002130 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002131 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 goto doend;
2133 }
2134#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002135 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002136 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002137 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002138 goto doend;
2139 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002140 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002142 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002143 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 goto doend;
2145 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002146
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002147 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002149 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2150 {
2151 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002152 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002153 goto doend;
2154 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002155 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2156 {
2157 // Command not allowed when text is locked
2158 errormsg = _(get_text_locked_msg());
2159 goto doend;
2160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002163 // Disallow editing another buffer when "curbuf_lock" is set.
2164 // Do allow ":checktime" (it is postponed).
2165 // Do allow ":edit" (check for an argument later).
2166 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002167 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002168 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002169 && ea.cmdidx != CMD_edit
2170 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002171 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002172 && curbuf_locked())
2173 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002175 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002177 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 goto doend;
2179 }
2180 }
2181
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002182 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002184 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 goto doend;
2186 }
2187
2188 /*
2189 * Don't complain about the range if it is not used
2190 * (could happen if line_count is accidentally set to 0).
2191 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002192 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 /*
2195 * If the range is backwards, ask for confirmation and, if given, swap
2196 * ea.line1 & ea.line2 so it's forwards again.
2197 * When global command is busy, don't ask, will fail below.
2198 */
2199 if (!global_busy && ea.line1 > ea.line2)
2200 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002201 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002203 if (sourcing || exmode_active)
2204 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002205 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002206 goto doend;
2207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (ask_yesno((char_u *)
2209 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002210 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212 lnum = ea.line1;
2213 ea.line1 = ea.line2;
2214 ea.line2 = lnum;
2215 }
2216 if ((errormsg = invalid_range(&ea)) != NULL)
2217 goto doend;
2218 }
2219
Bram Moolenaarb7316892019-05-01 18:08:42 +02002220 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2221 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ea.line2 = 1;
2223
2224 correct_range(&ea);
2225
2226#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002227 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002228 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002230 // Put the first line at the start of a closed fold, put the last line
2231 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 (void)hasFolding(ea.line1, &ea.line1, NULL);
2233 (void)hasFolding(ea.line2, NULL, &ea.line2);
2234 }
2235#endif
2236
2237#ifdef FEAT_QUICKFIX
2238 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002239 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 * option here, so things like % get expanded.
2241 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002242 p = replace_makeprg(&ea, p, cmdlinep);
2243 if (p == NULL)
2244 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245#endif
2246
2247 /*
2248 * Skip to start of argument.
2249 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2250 */
2251 if (ea.cmdidx == CMD_bang)
2252 ea.arg = p;
2253 else
2254 ea.arg = skipwhite(p);
2255
Bram Moolenaar379fb762018-08-30 15:58:28 +02002256 // ":file" cannot be run with an argument when "curbuf_lock" is set
2257 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2258 goto doend;
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 /*
2261 * Check for "++opt=val" argument.
2262 * Must be first, allow ":w ++enc=utf8 !cmd"
2263 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2266 if (getargopt(&ea) == FAIL && !ni)
2267 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002268 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 goto doend;
2270 }
2271
2272 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002274 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002276 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002278 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 goto doend;
2280 }
2281 ea.arg = skipwhite(ea.arg + 1);
2282 ea.append = TRUE;
2283 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002284 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 ++ea.arg;
2287 ea.usefilter = TRUE;
2288 }
2289 }
2290
2291 if (ea.cmdidx == CMD_read)
2292 {
2293 if (ea.forceit)
2294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002295 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 ea.forceit = FALSE;
2297 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002298 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 ++ea.arg;
2301 ea.usefilter = TRUE;
2302 }
2303 }
2304
2305 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2306 {
2307 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002308 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 ++ea.arg;
2311 ++ea.amount;
2312 }
2313 ea.arg = skipwhite(ea.arg);
2314 }
2315
2316 /*
2317 * Check for "+command" argument, before checking for next command.
2318 * Don't do this for ":read !cmd" and ":write !cmd".
2319 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2322
2323 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002324 * For commands that do not use '|' inside their argument: Check for '|' to
2325 * separate commands and '"' or '#' to start comments.
2326 *
2327 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002328 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002329 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002332 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2333 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002334 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002335 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002336 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002337 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002338 || ea.cmdidx == CMD_global
2339 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002340 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002341#ifdef FEAT_EVAL
2342 || inside_block(&ea)
2343#endif
2344 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 for (p = ea.arg; *p; ++p)
2347 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002348 // Remove one backslash before a newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002350 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002351 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 ea.nextcmd = p + 1;
2354 *p = NUL;
2355 break;
2356 }
2357 }
2358 }
2359
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002361 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002364 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002366 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002367 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002368 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002370#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002371 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002372 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002374 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002375 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377#endif
64-bitmane08f10a2025-03-18 22:14:34 +01002378 if (valid_yank_reg(*ea.arg, (!IS_USER_CMDIDX(ea.cmdidx)
2379 && ea.cmdidx != CMD_put && ea.cmdidx != CMD_iput)))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002380 {
2381 ea.regname = *ea.arg++;
2382#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002383 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002384 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2385 {
kuuote08d7b1c2021-10-04 22:17:36 +01002386 if (!ea.skip)
2387 {
2388 set_expr_line(vim_strsave(ea.arg), &ea);
2389 did_set_expr_line = TRUE;
2390 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002391 ea.arg += STRLEN(ea.arg);
2392 }
2393#endif
2394 ea.arg = skipwhite(ea.arg);
2395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397
2398 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002399 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * count, it's a buffer name.
2401 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002402 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002403 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002404 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002406 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002408 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002410 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 goto doend;
2412 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002413 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 ea.line2 = n;
2416 if (ea.addr_count == 0)
2417 ea.addr_count = 1;
2418 }
2419 else
2420 {
2421 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002422 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002423 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002424 else
2425 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 ++ea.addr_count;
2427 /*
2428 * Be vi compatible: no error message for out of range.
2429 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002430 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 ea.line2 = curbuf->b_ml.ml_line_count;
2432 }
2433 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002434
2435 /*
2436 * Check for flags: 'l', 'p' and '#'.
2437 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002438 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002439 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002440 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2441 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002443 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002444 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 goto doend;
2446 }
2447
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002448 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002450 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 goto doend;
2452 }
2453
2454#ifdef FEAT_EVAL
2455 /*
2456 * Skip the command when it's not going to be executed.
2457 * The commands like :if, :endif, etc. always need to be executed.
2458 * Also make an exception for commands that handle a trailing command
2459 * themselves.
2460 */
2461 if (ea.skip)
2462 {
2463 switch (ea.cmdidx)
2464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002465 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_while:
2467 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002468 case CMD_for:
2469 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_if:
2471 case CMD_elseif:
2472 case CMD_else:
2473 case CMD_endif:
2474 case CMD_try:
2475 case CMD_catch:
2476 case CMD_finally:
2477 case CMD_endtry:
2478 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002479 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 break;
2481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002482 // Commands that handle '|' themselves. Check: A command should
2483 // either have the EX_TRLBAR flag, appear in this list or appear in
2484 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_aboveleft:
2486 case CMD_and:
2487 case CMD_belowright:
2488 case CMD_botright:
2489 case CMD_browse:
2490 case CMD_call:
2491 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002492 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 case CMD_delfunction:
2494 case CMD_djump:
2495 case CMD_dlist:
2496 case CMD_dsearch:
2497 case CMD_dsplit:
2498 case CMD_echo:
2499 case CMD_echoerr:
2500 case CMD_echomsg:
2501 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002502 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002504 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002505 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 case CMD_help:
2507 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002508 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_ijump:
2510 case CMD_ilist:
2511 case CMD_isearch:
2512 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002513 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 case CMD_keepjumps:
2515 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002516 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_leftabove:
2518 case CMD_let:
2519 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002520 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002521 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002523 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002524 case CMD_noautocmd:
2525 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 case CMD_perl:
2527 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002528 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002529 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002530 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 case CMD_return:
2532 case CMD_rightbelow:
2533 case CMD_ruby:
2534 case CMD_silent:
2535 case CMD_smagic:
2536 case CMD_snomagic:
2537 case CMD_substitute:
2538 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002539 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 case CMD_tcl:
2541 case CMD_throw:
2542 case CMD_tilde:
2543 case CMD_topleft:
2544 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002545 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002546 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 case CMD_verbose:
2548 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002549 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 break;
2551
2552 default: goto doend;
2553 }
2554 }
2555#endif
2556
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002557 if ((ea.argt & EX_XFILE)
2558 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2559 goto doend;
2560
2561#ifdef FEAT_EVAL
2562 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002564 emsg(_(e_invalid_command_after_export));
2565 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 /*
2570 * Accept buffer name. Cannot be used at the same time with a buffer
2571 * number. Don't do this for a user command.
2572 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002573 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002574 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 /*
2577 * :bdelete, :bwipeout and :bunload take several arguments, separated
2578 * by spaces: find next space (skipping over escaped characters).
2579 * The others take one argument: ignore trailing spaces.
2580 */
2581 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2582 || ea.cmdidx == CMD_bunload)
2583 p = skiptowhite_esc(ea.arg);
2584 else
2585 {
2586 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002587 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 --p;
2589 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002590 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002591 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002592 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 goto doend;
2594 ea.addr_count = 1;
2595 ea.arg = skipwhite(p);
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002598 // The :try command saves the emsg_silent flag, reset it here when
2599 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002600 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002601 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002602 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002603 if (emsg_silent < 0)
2604 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002605 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002606 }
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002609 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaar958636c2014-10-21 20:01:58 +02002612 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
2614 /*
2615 * Execute a user-defined command.
2616 */
2617 do_ucmd(&ea);
2618 }
2619 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 {
2621 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002622 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2625 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002626 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628
2629#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002630 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002631 // set something went wrong or the command was never executed.
2632 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002633 {
2634 if (errormsg == NULL)
2635 errormsg = _(e_export_with_invalid_argument);
2636 is_export = FALSE;
2637 }
2638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002640 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002641 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002642 && current_sctx.sc_sid > 0
2643 && ea.cmdidx != CMD_endif
2644 && (cstack->cs_idx < 0
2645 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002646 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 /*
2649 * If the command just executed called do_cmdline(), any throw or ":return"
2650 * or ":finish" encountered there must also check the cstack of the still
2651 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2652 * exception, or reanimate a returned function or finished script file and
2653 * return or finish it again.
2654 */
2655 if (need_rethrow)
2656 do_throw(cstack);
2657 else if (check_cstack)
2658 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002659 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002661 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 && current_func_returned())
2663 do_return(&ea, TRUE, FALSE, NULL);
2664 }
2665 need_rethrow = check_cstack = FALSE;
2666#endif
2667
2668doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002669 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002672 curwin->w_cursor.col = 0;
2673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2676 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002677 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002679 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
2681 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002682 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002684 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
2686 emsg(errormsg);
2687 }
2688#ifdef FEAT_EVAL
2689 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002690 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2691 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002692
2693 if (did_set_expr_line)
2694 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002695 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696#endif
2697
Bram Moolenaare1004402020-10-24 20:49:43 +02002698 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002700 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002701 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002703 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 ea.nextcmd = NULL;
2705
2706#ifdef FEAT_EVAL
2707 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002708 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#endif
2710
2711 return ea.nextcmd;
2712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002714static char ex_error_buf[MSG_BUF_LEN];
2715
2716/*
2717 * Return an error message with argument included.
2718 * Uses a static buffer, only the last error will be kept.
2719 * "msg" will be translated, caller should use N_().
2720 */
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02002721 char *
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002722ex_errmsg(char *msg, char_u *arg)
2723{
2724 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2725 return ex_error_buf;
2726}
2727
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002729 * The "+" string used in place of an empty command in Ex mode.
2730 * This string is used in pointer comparison.
2731 */
2732static char exmode_plus[] = "+";
2733
2734/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002735 * Handle a range without a command.
2736 * Returns an error message on failure.
2737 */
2738 char *
2739ex_range_without_command(exarg_T *eap)
2740{
2741 char *errormsg = NULL;
2742
zeertzjq7d664bf2024-07-14 10:22:54 +02002743 if ((*eap->cmd == '|' ||
2744 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002745#ifdef FEAT_EVAL
2746 && !in_vim9script()
2747#endif
2748 )
2749 {
2750 eap->cmdidx = CMD_print;
2751 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2752 if ((errormsg = invalid_range(eap)) == NULL)
2753 {
2754 correct_range(eap);
2755 ex_print(eap);
2756 }
2757 }
2758 else if (eap->addr_count != 0)
2759 {
2760 if (eap->line2 > curbuf->b_ml.ml_line_count)
2761 {
2762 // With '-' in 'cpoptions' a line number past the file is an
2763 // error, otherwise put it at the end of the file.
2764 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2765 eap->line2 = -1;
2766 else
2767 eap->line2 = curbuf->b_ml.ml_line_count;
2768 }
2769
2770 if (eap->line2 < 0)
2771 errormsg = _(e_invalid_range);
2772 else
2773 {
2774 if (eap->line2 == 0)
2775 curwin->w_cursor.lnum = 1;
2776 else
2777 curwin->w_cursor.lnum = eap->line2;
2778 beginline(BL_SOL | BL_FIX);
2779 }
2780 }
2781 return errormsg;
2782}
2783
2784/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002785 * Check for an Ex command with optional tail.
2786 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002787 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002788 */
2789 static int
2790checkforcmd_opt(
2791 char_u **pp, // start of command
2792 char *cmd, // name of command
2793 int len, // required length
2794 int noparen)
2795{
2796 int i;
2797
2798 for (i = 0; cmd[i] != NUL; ++i)
2799 if (((char_u *)cmd)[i] != (*pp)[i])
2800 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002801 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002802 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002803 {
2804 *pp = skipwhite(*pp + i);
2805 return TRUE;
2806 }
2807 return FALSE;
2808}
2809
2810/*
2811 * Check for an Ex command with optional tail.
2812 * If there is a match advance "pp" to the argument and return TRUE.
2813 */
2814 int
2815checkforcmd(
2816 char_u **pp, // start of command
2817 char *cmd, // name of command
2818 int len) // required length
2819{
2820 return checkforcmd_opt(pp, cmd, len, FALSE);
2821}
2822
2823/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002824 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002825 * If there is a match advance "pp" to the argument and return TRUE.
2826 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002827 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002828checkforcmd_noparen(
2829 char_u **pp, // start of command
2830 char *cmd, // name of command
2831 int len) // required length
2832{
2833 return checkforcmd_opt(pp, cmd, len, TRUE);
2834}
2835
2836/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 * Parse and skip over command modifiers:
2838 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002839 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002841 * When "skip_only" is TRUE the global variables are not changed, except for
2842 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002843 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2844 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002845 * Call apply_cmdmod() to get the side effects of the modifiers:
2846 * - Increment "sandbox" for ":sandbox"
2847 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002848 * - set msg_silent for ":silent"
2849 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 * Return FAIL when the command is not to be executed.
2851 * May set "errormsg" to an error message.
2852 */
2853 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002854parse_command_modifiers(
2855 exarg_T *eap,
2856 char **errormsg,
2857 cmdmod_T *cmod,
2858 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002859{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002860 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002861 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002862 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002863 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002864 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002865 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866
Bram Moolenaare1004402020-10-24 20:49:43 +02002867 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002868 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002869
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002870 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2871 {
2872 // The automatically inserted Visual area range is skipped, so that
2873 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002874 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2875 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002876 eap->cmd += 5;
2877 cmd_start = eap->cmd;
2878 has_visual_range = TRUE;
2879 }
2880
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002881 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 for (;;)
2883 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002884 char_u *p;
2885
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002887 {
2888 if (*eap->cmd == ':')
2889 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002890 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002891 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002892
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002893 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002895 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2896 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2898 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002899 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002900 if (!skip_only)
2901 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002902 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 }
2904
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002905 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002906 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002907 {
2908 // a comment ends at a NL
zeertzjqf7b86092024-09-05 17:26:30 +02002909 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2910 if (eap->nextcmd != NULL)
2911 ++eap->nextcmd;
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002912 if (vim9script)
2913 {
2914 if (has_cmdmod(cmod, FALSE))
2915 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002916#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002917 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2918 && eap->cmd[2] != '{')
2919 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002920#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002921 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002923 }
zeertzjqf7b86092024-09-05 17:26:30 +02002924 if (*eap->cmd == '\n')
zeertzjq2432b4a2024-09-03 22:58:30 +02002925 {
2926 eap->nextcmd = eap->cmd + 1;
2927 return FAIL;
2928 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002929 if (*eap->cmd == NUL)
2930 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002931 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002932 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002933 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002934 if (vim9script && has_cmdmod(cmod, FALSE))
2935 *errormsg = _(e_command_modifier_without_command);
2936 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002937 return FAIL;
2938 }
2939
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002940 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002941
2942 // In Vim9 script a variable can shadow a command modifier:
2943 // verbose = 123
2944 // verbose += 123
2945 // silent! verbose = func()
2946 // verbose.member = 2
2947 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002948 // But not:
2949 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002950 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002951 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002952 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002953
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002954 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002955 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002956 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002957 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2958 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002959 break;
2960 }
2961
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 switch (*p)
2963 {
John Marriotted908f72024-04-18 22:46:56 +02002964 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002965 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002966 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002967 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 continue;
2969
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002970 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002972 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 continue;
2974 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002975 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002976 {
2977#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002978 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002979#endif
2980 continue;
2981 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002982 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002983 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002984 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002985 continue;
2986
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002987 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002988 break;
2989#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002990 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002991#endif
2992 continue;
2993
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002994 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002995 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002996 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 continue;
2998 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002999 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003000 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 continue;
3003 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003004 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003005 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003006 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 continue;
3008 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003009 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003011 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003012 continue;
3013
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003014 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003015 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003016 char_u *reg_pat;
3017 char_u *nulp = NULL;
3018 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003019
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003020 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003021 || *p == NUL
3022 || (ends_excmd(*p)
3023#ifdef FEAT_EVAL
3024 // in ":filter #pat# cmd" # does not
3025 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003026 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003027#endif
3028 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003029 break;
3030 if (*p == '!')
3031 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003032 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003033 p = skipwhite(p + 1);
3034 if (*p == NUL || ends_excmd(*p))
3035 break;
3036 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003037#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003038 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003039 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003040 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003041#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003042 if (skip_only)
3043 p = skip_vimgrep_pat(p, NULL, NULL);
3044 else
3045 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003046 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3047 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003048 if (p == NULL || *p == NUL)
3049 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003050 if (!skip_only)
3051 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003052 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003053 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003054 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003055 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003056 // restore the character overwritten by NUL
3057 if (nulp != NULL)
3058 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003059 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003060 eap->cmd = p;
3061 continue;
3062 }
3063
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003064 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3065 {
3066 cmod->cmod_split |= WSP_HOR;
3067 continue;
3068 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003069 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003070 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003071 || *p == NUL || ends_excmd(*p))
3072 break;
3073 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003074 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003075 continue;
3076
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003077 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003078 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003079 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003080 continue;
3081 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003082 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3083 {
3084 if (ends_excmd2(p, eap->cmd))
3085 {
3086 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003087 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003088 return FAIL;
3089 }
3090 cmod->cmod_flags |= CMOD_LEGACY;
3091 continue;
3092 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003093
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003094 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003095 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003096 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 continue;
3098
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003099 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003100 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003101 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 continue;
3103 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003104 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003105 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003106 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003107 continue;
3108
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003109 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003110 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003111 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003112 continue;
3113
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003114 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003115 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003116 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003117 continue;
3118 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003119 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003120 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003121 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003122 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3123 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003124 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003125 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003126 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003127 }
3128 continue;
3129
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003130 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003131 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003132 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003133 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003134 long tabnr = get_address(eap, &eap->cmd,
3135 ADDR_TABS, eap->skip,
3136 skip_only, FALSE, 1);
3137 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003138 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003139 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003140 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003141 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3142 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003143 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003144 return FAIL;
3145 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003146 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003147 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003148 }
3149 eap->cmd = p;
3150 continue;
3151 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003152 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003153 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003154 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003155 continue;
3156
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003157 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003158 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003159 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003160 continue;
3161
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003162 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003163 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003164 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003165 continue;
3166 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003167 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003168 {
3169 if (ends_excmd2(p, eap->cmd))
3170 {
3171 *errormsg =
3172 _(e_vim9cmd_must_be_followed_by_command);
3173 return FAIL;
3174 }
3175 cmod->cmod_flags |= CMOD_VIM9CMD;
3176 continue;
3177 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003178 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003179 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003180 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003181 {
zeertzjqcd749632022-06-14 13:30:35 +01003182 // zero means not set, one is verbose == 0, etc.
3183 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003184 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003185 else
zeertzjqcd749632022-06-14 13:30:35 +01003186 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003187 eap->cmd = p;
3188 continue;
3189 }
3190 break;
3191 }
3192
Bram Moolenaar1501b632022-03-27 16:56:21 +01003193 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003194 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003195 if (eap->cmd > cmd_start)
3196 {
3197 // Move the '<,'> range to after the modifiers and insert a colon.
3198 // Since the modifiers have been parsed put the colon on top of the
3199 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3200 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003201 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003202 {
3203 size_t len = STRLEN(cmd_start);
3204
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003205 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003206 // "'<,'>mods" -> "mods *+
3207 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003208 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003209 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003210 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003211 }
3212 else
3213 {
3214 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3215 eap->cmd -= 5;
3216 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3217 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003218 }
3219 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003220 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003221 // Special case: change empty command to "+".
3222 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003223 eap->cmd = (char_u *)"'<,'>+";
3224 else
3225 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003226 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003227 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003228 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003229
Bram Moolenaareffed932018-08-14 13:38:17 +02003230 return OK;
3231}
3232
3233/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003234 * Return TRUE if "cmod" has anything set.
3235 */
3236 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003237has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003238{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003239 return (cmod->cmod_flags != 0 && (!ignore_silent
3240 || (cmod->cmod_flags
3241 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003242 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003243 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003244 || cmod->cmod_tab != 0
3245 || cmod->cmod_filter_regmatch.regprog != NULL;
3246}
3247
Bram Moolenaar8991be22022-02-14 21:51:46 +00003248#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003249/*
3250 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3251 */
3252 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003253cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003254{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003255 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003256 {
3257 emsg(_(e_misplaced_command_modifier));
3258 return TRUE;
3259 }
3260 return FALSE;
3261}
Dominique Pelle748b3082022-01-08 12:41:16 +00003262#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003263
3264/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003265 * Apply the command modifiers. Saves current state in "cmdmod", call
3266 * undo_cmdmod() later.
3267 */
3268 void
3269apply_cmdmod(cmdmod_T *cmod)
3270{
3271#ifdef HAVE_SANDBOX
3272 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3273 {
3274 ++sandbox;
3275 cmod->cmod_did_sandbox = TRUE;
3276 }
3277#endif
zeertzjqcd749632022-06-14 13:30:35 +01003278 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003279 {
3280 if (cmod->cmod_verbose_save == 0)
3281 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003282 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003283 }
3284
3285 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3286 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003287 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003288 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003289 cmod->cmod_save_msg_scroll = msg_scroll;
3290 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003291 if (cmod->cmod_flags & CMOD_SILENT)
3292 ++msg_silent;
3293 if (cmod->cmod_flags & CMOD_UNSILENT)
3294 msg_silent = 0;
3295
3296 if (cmod->cmod_flags & CMOD_ERRSILENT)
3297 {
3298 ++emsg_silent;
3299 ++cmod->cmod_did_esilent;
3300 }
3301
Bram Moolenaare1004402020-10-24 20:49:43 +02003302 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003303 {
3304 // Set 'eventignore' to "all".
3305 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003306 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003307 set_string_option_direct((char_u *)"ei", -1,
3308 (char_u *)"all", OPT_FREE, SID_NONE);
3309 }
3310}
3311
3312/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003313 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003314 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003315 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003316undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003317{
Bram Moolenaare1004402020-10-24 20:49:43 +02003318 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003319 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003320 p_verbose = cmod->cmod_verbose_save - 1;
3321 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003322 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003323
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003324#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003325 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003326 {
3327 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003328 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003329 }
3330#endif
3331
Bram Moolenaare1004402020-10-24 20:49:43 +02003332 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003334 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003335 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3336 OPT_FREE, SID_NONE);
3337 free_string_option(cmod->cmod_save_ei);
3338 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003339 }
3340
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003341 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003342
Bram Moolenaare1004402020-10-24 20:49:43 +02003343 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003344 {
3345 // messages could be enabled for a serious error, need to check if the
3346 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003347 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3348 msg_silent = cmod->cmod_save_msg_silent - 1;
3349 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003350 if (emsg_silent < 0)
3351 emsg_silent = 0;
3352 // Restore msg_scroll, it's set by file I/O commands, even when no
3353 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003354 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003355
3356 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3357 // somewhere in the line. Put it back in the first column.
3358 if (redirecting())
3359 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003360
Bram Moolenaare1004402020-10-24 20:49:43 +02003361 cmod->cmod_save_msg_silent = 0;
3362 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003363 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003364}
3365
3366/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003367 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003368 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003369 * Return FAIL and set "errormsg" or return OK.
3370 */
3371 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003372parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003373{
3374 int address_count = 1;
3375 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003376 int need_check_cursor = FALSE;
3377 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003378
3379 // Repeat for all ',' or ';' separated addresses.
3380 for (;;)
3381 {
3382 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003383 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003384 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003385 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003386 eap->addr_count == 0, address_count++);
3387 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003388 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003389 if (lnum == MAXLNUM)
3390 {
3391 if (*eap->cmd == '%') // '%' - all lines
3392 {
3393 ++eap->cmd;
3394 switch (eap->addr_type)
3395 {
3396 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003397 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003398 eap->line1 = 1;
3399 eap->line2 = curbuf->b_ml.ml_line_count;
3400 break;
3401 case ADDR_LOADED_BUFFERS:
3402 {
3403 buf_T *buf = firstbuf;
3404
3405 while (buf->b_next != NULL
3406 && buf->b_ml.ml_mfp == NULL)
3407 buf = buf->b_next;
3408 eap->line1 = buf->b_fnum;
3409 buf = lastbuf;
3410 while (buf->b_prev != NULL
3411 && buf->b_ml.ml_mfp == NULL)
3412 buf = buf->b_prev;
3413 eap->line2 = buf->b_fnum;
3414 break;
3415 }
3416 case ADDR_BUFFERS:
3417 eap->line1 = firstbuf->b_fnum;
3418 eap->line2 = lastbuf->b_fnum;
3419 break;
3420 case ADDR_WINDOWS:
3421 case ADDR_TABS:
3422 if (IS_USER_CMDIDX(eap->cmdidx))
3423 {
3424 eap->line1 = 1;
3425 eap->line2 = eap->addr_type == ADDR_WINDOWS
3426 ? LAST_WIN_NR : LAST_TAB_NR;
3427 }
3428 else
3429 {
3430 // there is no Vim command which uses '%' and
3431 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003432 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003433 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003434 }
3435 break;
3436 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003437 case ADDR_UNSIGNED:
3438 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003439 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003440 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003441 case ADDR_ARGUMENTS:
3442 if (ARGCOUNT == 0)
3443 eap->line1 = eap->line2 = 0;
3444 else
3445 {
3446 eap->line1 = 1;
3447 eap->line2 = ARGCOUNT;
3448 }
3449 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003450 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003451#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003452 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003453 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003454 if (eap->line2 == 0)
3455 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003456#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003457 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003458 case ADDR_NONE:
3459 // Will give an error later if a range is found.
3460 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003461 }
3462 ++eap->addr_count;
3463 }
3464 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3465 {
3466 pos_T *fp;
3467
3468 // '*' - visual area
3469 if (eap->addr_type != ADDR_LINES)
3470 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003471 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003472 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003473 }
3474
3475 ++eap->cmd;
3476 if (!eap->skip)
3477 {
3478 fp = getmark('<', FALSE);
3479 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003480 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003481 eap->line1 = fp->lnum;
3482 fp = getmark('>', FALSE);
3483 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003484 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003485 eap->line2 = fp->lnum;
3486 ++eap->addr_count;
3487 }
3488 }
3489 }
3490 else
3491 eap->line2 = lnum;
3492 eap->addr_count++;
3493
3494 if (*eap->cmd == ';')
3495 {
3496 if (!eap->skip)
3497 {
3498 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003499
Bram Moolenaar0e717042020-04-27 19:29:01 +02003500 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003501 // accept zero as address, so 0;/PATTERN/ works correctly
3502 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003503 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003504 if (eap->line2 > 0)
3505 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003506 else
3507 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003508 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003509 }
3510 }
3511 else if (*eap->cmd != ',')
3512 break;
3513 ++eap->cmd;
3514 }
3515
3516 // One address given: set start and end lines.
3517 if (eap->addr_count == 1)
3518 {
3519 eap->line1 = eap->line2;
3520 // ... but only implicit: really no address given
3521 if (lnum == MAXLNUM)
3522 eap->addr_count = 0;
3523 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003524 ret = OK;
3525
3526theend:
3527 if (need_check_cursor)
3528 check_cursor();
3529 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003530}
3531
3532/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003533 * Append "cmd" to the error message in IObuff.
3534 * Takes care of limiting the length and handling 0xa0, which would be
3535 * invisible otherwise.
3536 */
3537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003538append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003539{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003540 size_t len = STRLEN(IObuff);
3541 char_u *s = cmd;
3542 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003543
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003544 if (len > IOSIZE - 100)
3545 {
3546 // Not enough space, truncate and put in "...".
3547 d = IObuff + IOSIZE - 100;
3548 d -= mb_head_off(IObuff, d);
3549 STRCPY(d, "...");
3550 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003551 STRCAT(IObuff, ": ");
3552 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003553 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003554 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003555 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003556 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003557 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003558 STRCPY(d, "<a0>");
3559 d += 4;
3560 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003561 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3562 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003563 else
3564 MB_COPY_CHAR(s, d);
3565 }
3566 *d = NUL;
3567}
3568
Dominique Pelle748b3082022-01-08 12:41:16 +00003569#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003570/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003571 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3572 * the name. Otherwise just return "start".
3573 */
3574 char_u *
3575skip_option_env_lead(char_u *start)
3576{
3577 char_u *name = start;
3578
3579 if (*start == '&')
3580 {
3581 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3582 name += 3;
3583 else
3584 name += 1;
3585 }
3586 else if (*start == '$')
3587 name += 1;
3588 return name;
3589}
Dominique Pelle748b3082022-01-08 12:41:16 +00003590#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003591
3592/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003593 * Return TRUE and set "*idx" if "p" points to a one letter command.
3594 * If not in Vim9 script:
Doug Kearnsea842022024-09-29 17:17:41 +02003595 * - The 'k' command can directly be followed by any character
3596 * but :keepa[lt] is another command, as are :keepj[umps],
3597 * :kee[pmarks] and :keepp[atterns].
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003598 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3599 * but :sre[wind] is another command, as are :scr[iptnames],
3600 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3601 */
3602 static int
3603one_letter_cmd(char_u *p, cmdidx_T *idx)
3604{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003605 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003606 return FALSE;
Doug Kearnsea842022024-09-29 17:17:41 +02003607 if (p[0] == 'k'
3608 && (p[1] != 'e' || (p[1] == 'e' && p[2] != 'e')))
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003609 {
3610 *idx = CMD_k;
3611 return TRUE;
3612 }
3613 if (p[0] == 's'
3614 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3615 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3616 || p[1] == 'g'
3617 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3618 || p[1] == 'I'
3619 || (p[1] == 'r' && p[2] != 'e')))
3620 {
3621 *idx = CMD_substitute;
3622 return TRUE;
3623 }
3624 return FALSE;
3625}
3626
Dominique Pellee764d1b2023-03-12 21:20:59 +00003627#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003628/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003629 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3630 * call.
3631 */
3632 int
3633number_method(char_u *cmd)
3634{
3635 char_u *p = skipdigits(cmd);
3636
3637 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3638}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003639#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003640
3641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003644 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003645 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 *
3647 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3648 * assignment without "let". Sets eap->cmdidx to the command while returning
3649 * "eap->cmd".
3650 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 * Returns NULL for an ambiguous user command.
3652 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 char_u *
3654find_ex_command(
3655 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003656 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003657 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003658 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
3660 int len;
3661 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003662 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003663#ifndef FEAT_EVAL
3664 int vim9 = FALSE;
3665#else
3666 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 /*
3669 * Recognize a Vim9 script function/method call and assignment:
3670 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3671 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003672 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003673 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003675 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003677 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003678 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3679 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003681 int oplen;
3682 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003683 char_u *swp;
3684
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003685 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003686 || (eap->cmd[0] == '$'
3687 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003688 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003689 && (valid_yank_reg(eap->cmd[1], FALSE)
3690 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003691 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003692 if (*eap->cmd == '&')
3693 {
3694 p = eap->cmd + 1;
3695 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3696 p += 2;
3697 p = to_name_end(p, FALSE);
3698 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003699 else if (*eap->cmd == '$')
3700 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003701 else
3702 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003703 if (ends_excmd(*skipwhite(p)))
3704 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003705 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3706 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003707 eap->cmdidx = CMD_eval;
3708 return eap->cmd;
3709 }
3710 // "&option" can be followed by "->" or "=", check below
3711 }
3712
3713 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003715 if (
3716 // "(..." is an expression.
3717 // "funcname(" is always a function call.
3718 *p == '('
3719 || (p == eap->cmd
3720 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003721 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003722 *eap->cmd == '{'
3723 // "'string'->func()" is an expression.
3724 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003725 // '"string"->func()' is an expression.
3726 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003727 // '$"string"->func()' is an expression.
3728 // "$'string'->func()" is an expression.
3729 || (eap->cmd[0] == '$'
3730 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3731 // '0z1234->func()' is an expression.
3732 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003733 // "g:varname" is an expression.
3734 || eap->cmd[1] == ':'
3735 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003736 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003737 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003738 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003739 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3740 {
3741 // "{" by itself is the start of a block.
3742 eap->cmdidx = CMD_block;
3743 return eap->cmd + 1;
3744 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003745 eap->cmdidx = CMD_eval;
3746 return eap->cmd;
3747 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003748
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003749 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003750 // "varname[]" is an expression.
3751 *p == '['
3752 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003753 || (*p == '.'
3754 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3755 // g:[key] is an expression
3756 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003757 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003758 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003759
3760 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003761 // Skip over the whole thing, it can be:
3762 // name.member = val
3763 // name[a : b] = val
3764 // name[idx] = val
3765 // name[idx].member = val
3766 // etc.
3767 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003768 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003769 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003770 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003771 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003772 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003773 || (after[0] == '.' && after[1] == '.'
3774 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003775 eap->cmdidx = CMD_var;
3776 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003777 --emsg_silent;
3778 return eap->cmd;
3779 }
3780
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003781 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3782 // an assignment.
3783 // If there is no line break inside the "[...]" then "p" is
3784 // advanced to after the "]" by to_name_const_end(): check if a "="
3785 // follows.
3786 // If "[...]" has a line break "p" still points at the "[" and it
3787 // can't be an assignment.
3788 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003789 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003790 char_u *eq;
3791
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003792 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003793 if (p == eap->cmd && *p == '[')
3794 {
3795 int count = 0;
3796 int semicolon = FALSE;
3797
3798 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3799 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003800 eq = p;
3801 if (eq != NULL)
3802 {
3803 eq = skipwhite(eq);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003804 if (vim_strchr((char_u *)"+-*/%.", *eq) != NULL)
3805 {
3806 if (eq[0] == '.' && eq[1] == '.')
3807 ++eq;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003808 ++eq;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003809 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003810 }
3811 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003812 {
3813 eap->cmdidx = CMD_eval;
3814 return eap->cmd;
3815 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003816 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003817 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003818 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003819 return eap->cmd;
3820 }
3821 }
3822
3823 // Recognize an assignment if we recognize the variable name:
3824 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003825 // "@r = expr"
3826 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003827 // "var = expr" where "var" is a variable name or we are skipping
3828 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003829 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003830 oplen = assignment_len(skipwhite(p), &heredoc);
3831 if (oplen > 0)
3832 {
3833 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3834 || *eap->cmd == '&'
3835 || *eap->cmd == '$'
3836 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003837 || (eap->skip && IS_WHITE_OR_NUL(
3838 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003839 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003840 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003841 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003842 return eap->cmd;
3843 }
3844 }
3845
Bram Moolenaar9510d222022-09-11 15:14:05 +01003846 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003847 // "w:varname: number = 123".
3848 if (eap->cmd[1] == ':' && *p == ':')
3849 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003850 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003851 return eap->cmd;
3852 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003853 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003854
Bram Moolenaar31d99482022-05-26 22:24:43 +01003855 // 1234->func() is a method call
3856 if (number_method(eap->cmd))
3857 {
3858 eap->cmdidx = CMD_eval;
3859 return eap->cmd;
3860 }
3861
Bram Moolenaar7b829262021-10-13 15:04:34 +01003862 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3863 // an expression. A global/substitute/list command needs to use a
3864 // longer name.
3865 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3866 {
3867 eap->cmdidx = CMD_eval;
3868 return eap->cmd;
3869 }
3870
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003871 // If it is an ID it might be a variable with an operator on the next
3872 // line, if the variable exists it can't be an Ex command.
3873 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003874 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003875 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3876 {
3877 eap->cmdidx = CMD_eval;
3878 return eap->cmd;
3879 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003880
3881 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003882 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3883 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003884 {
3885 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3886 return eap->cmd + 2;
3887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 }
3889#endif
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 /*
3892 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 */
3894 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003895 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 ++p;
Doug Kearnsea842022024-09-29 17:17:41 +02003898 if (full != NULL)
3899 *full = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901 else
3902 {
3903 while (ASCII_ISALPHA(*p))
3904 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003905 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003906 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003908 while (ASCII_ISALNUM(*p))
3909 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 }
3911 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3912 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003913 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 ++p;
3915 while (ASCII_ISALPHA(*p))
3916 ++p;
3917 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003918
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003919 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003920 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 ++p;
3922 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003923 // The "d" command can directly be followed by 'l' or 'p' flag, when
3924 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003925 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003927 // Check for ":dl", ":dell", etc. to ":deletel": that's
3928 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003929 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003930 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003931 break;
3932 if (i == len - 1)
3933 {
3934 --len;
3935 if (p[-1] == 'l')
3936 eap->flags |= EXFLAG_LIST;
3937 else
3938 eap->flags |= EXFLAG_PRINT;
3939 }
3940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003942 if (ASCII_ISLOWER(eap->cmd[0]))
3943 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003944 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003945 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003946
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003947 if (command_count != (int)CMD_SIZE)
3948 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003949 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003950 getout(1);
3951 }
3952
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003953 // Use a precomputed index for fast look-up in cmdnames[]
3954 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003955 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3956 if (ASCII_ISLOWER(c2))
3957 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3958 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003959 else if (ASCII_ISUPPER(eap->cmd[0]))
3960 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003962 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3965 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3966 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3967 (size_t)len) == 0)
3968 {
3969#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003970 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 *full = TRUE;
3972#endif
3973 break;
3974 }
3975
Bram Moolenaar204852a2022-03-05 12:56:44 +00003976 // :Print and :mode are not supported in Vim9 script.
3977 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003978 if (vim9 && eap->cmdidx != CMD_SIZE)
3979 {
3980 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3981 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003982 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003983 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003984 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003985 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003986 eap->cmdidx = CMD_SIZE;
3987 }
3988 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003989
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003990 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003991 // 'cpoptions'.
3992 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3993 p = eap->cmd;
3994
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003995 // Look for a user defined command as a last resort. Let ":Print" be
3996 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003997 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3998 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004000 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 while (ASCII_ISALNUM(*p))
4002 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004003 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01004005 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 eap->cmdidx = CMD_SIZE;
4007 }
4008
Bram Moolenaar204852a2022-03-05 12:56:44 +00004009 // ":fina" means ":finally" in legacy script, for backwards compatibility.
4010 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02004011 eap->cmdidx = CMD_finally;
4012
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004013#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004014 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004015 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004016 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004017 && (eap->cmdidx < 0 ||
4018 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004019 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004020 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4021
4022 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004023 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004024 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004025 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004026#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return p;
4029}
4030
4031#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004032typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004033{
4034 char *name;
4035 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004036 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004037} cmdmod_info_T;
4038
4039static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004040 {"aboveleft", 3, FALSE},
4041 {"belowright", 3, FALSE},
4042 {"botright", 2, FALSE},
4043 {"browse", 3, FALSE},
4044 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004045 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004046 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004047 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004048 {"keepalt", 5, FALSE},
4049 {"keepjumps", 5, FALSE},
4050 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004051 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004052 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004053 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004054 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004055 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004056 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004057 {"rightbelow", 6, FALSE},
4058 {"sandbox", 3, FALSE},
4059 {"silent", 3, FALSE},
4060 {"tab", 3, TRUE},
4061 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004062 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004063 {"verbose", 4, TRUE},
4064 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004065 {"vim9cmd", 4, FALSE}
4066}; // cmdmod_info_tab
4067
Bram Moolenaared53fb92007-11-24 20:50:24 +00004068/*
4069 * Return length of a command modifier (including optional count).
4070 * Return zero when it's not a modifier.
4071 */
4072 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004073modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004074{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004075 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004076 char_u *p = cmd;
4077
4078 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004079 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004080 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004081 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004082 for (j = 0; p[j] != NUL; ++j)
4083 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004084 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004085 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4086 && (p == cmd || cmdmod_info_tab[i].has_count))
4087 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004088 }
4089 return 0;
4090}
4091
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092/*
4093 * Return > 0 if an Ex command "name" exists.
4094 * Return 2 if there is an exact match.
4095 * Return 3 if there is an ambiguous match.
4096 */
4097 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004098cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 exarg_T ea;
4101 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004102 int i;
4103 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004104 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105
Christian Brabandt70a11a62024-07-26 19:13:55 +02004106 // Check command modifiers.
4107 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004109 for (j = 0; name[j] != NUL; ++j)
4110 if (name[j] != cmdmod_info_tab[i].name[j])
4111 break;
4112 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4113 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004116 // Check built-in commands and user defined commands.
4117 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004118 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004120 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004122 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004124 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4125 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004126 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004127 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4129}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004130
4131/*
4132 * "fullcommand" function
4133 */
4134 void
4135f_fullcommand(typval_T *argvars, typval_T *rettv)
4136{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004137 exarg_T ea;
4138 char_u *name;
4139 char_u *p;
4140 int vim9script = in_vim9script();
4141 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004142
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004143 rettv->v_type = VAR_STRING;
4144 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004145
Bram Moolenaaraa534142022-09-15 21:46:02 +01004146 if (in_vim9script()
4147 && (check_for_string_arg(argvars, 0) == FAIL
4148 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004149 return;
4150
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004151 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004152 if (name == NULL)
4153 return;
4154
Bram Moolenaaraa534142022-09-15 21:46:02 +01004155 if (argvars[1].v_type != VAR_UNKNOWN)
4156 {
4157 vim9script = tv_get_bool(&argvars[1]);
4158 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4159 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4160 }
4161
zeertzjqc024ed92022-01-04 16:22:52 +00004162 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004163 name++;
4164 name = skip_range(name, TRUE, NULL);
4165
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004166 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4167 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004168 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004169 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004170 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004171 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004172 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004173 goto theend;
4174
4175 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004176 {
4177 int res;
4178
4179 ++emsg_silent;
4180 res = not_in_vim9(&ea);
4181 --emsg_silent;
4182
4183 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004184 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004185 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004186
4187 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004188 ? get_user_command_name(ea.useridx, ea.cmdidx)
4189 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004190theend:
4191 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#endif
4194
Bram Moolenaard0190392019-08-23 21:17:35 +02004195 cmdidx_T
4196excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
Bram Moolenaard0190392019-08-23 21:17:35 +02004198 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004200 if (!one_letter_cmd(cmd, &idx))
4201 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4202 idx = (cmdidx_T)((int)idx + 1))
4203 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4204 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
Bram Moolenaard0190392019-08-23 21:17:35 +02004206 return idx;
4207}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
Bram Moolenaard0190392019-08-23 21:17:35 +02004209 long
4210excmd_get_argt(cmdidx_T idx)
4211{
4212 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213}
4214
4215/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004216 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 *
4218 * Backslashed delimiters after / or ? will be skipped, and commands will
4219 * not be expanded between /'s and ?'s or after "'".
4220 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004221 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 * Returns the "cmd" pointer advanced to beyond the range.
4223 */
4224 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004225skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004226 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004227 int skip_star, // skip "*" used for Visual range
4228 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004230 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004231 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004233 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004235 if (*cmd == '\\')
4236 {
4237 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4238 ++cmd;
4239 else
4240 break;
4241 }
4242 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004244 char_u *p = cmd;
4245
4246 // a quote is only valid at the start or after a separator
4247 while (p > cmd_start)
4248 {
4249 --p;
4250 if (!VIM_ISWHITE(*p))
4251 break;
4252 }
4253 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4254 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (*++cmd == NUL && ctx != NULL)
4256 *ctx = EXPAND_NOTHING;
4257 }
4258 else if (*cmd == '/' || *cmd == '?')
4259 {
4260 delim = *cmd++;
4261 while (*cmd != NUL && *cmd != delim)
4262 if (*cmd++ == '\\' && *cmd != NUL)
4263 ++cmd;
4264 if (*cmd == NUL && ctx != NULL)
4265 *ctx = EXPAND_NOTHING;
4266 }
4267 if (*cmd != NUL)
4268 ++cmd;
4269 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004270
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004271 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004272 while (*cmd == ':')
4273 cmd = skipwhite(cmd + 1);
4274
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004275 // Skip "*" used for Visual range.
4276 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4277 cmd = skipwhite(cmd + 1);
4278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 return cmd;
4280}
4281
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004282 static void
4283addr_error(cmd_addr_T addr_type)
4284{
4285 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004286 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004287 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004288 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004289}
4290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004292 * Return the default address for an address type.
4293 */
4294 static linenr_T
4295default_address(exarg_T *eap)
4296{
4297 linenr_T lnum = 0;
4298
4299 switch (eap->addr_type)
4300 {
4301 case ADDR_LINES:
4302 case ADDR_OTHER:
4303 // Default is the cursor line number. Avoid using an invalid
4304 // line number though.
4305 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4306 lnum = curbuf->b_ml.ml_line_count;
4307 else
4308 lnum = curwin->w_cursor.lnum;
4309 break;
4310 case ADDR_WINDOWS:
4311 lnum = CURRENT_WIN_NR;
4312 break;
4313 case ADDR_ARGUMENTS:
4314 lnum = curwin->w_arg_idx + 1;
4315 if (lnum > ARGCOUNT)
4316 lnum = ARGCOUNT;
4317 break;
4318 case ADDR_LOADED_BUFFERS:
4319 case ADDR_BUFFERS:
4320 lnum = curbuf->b_fnum;
4321 break;
4322 case ADDR_TABS:
4323 lnum = CURRENT_TAB_NR;
4324 break;
4325 case ADDR_TABS_RELATIVE:
4326 case ADDR_UNSIGNED:
4327 lnum = 1;
4328 break;
4329 case ADDR_QUICKFIX:
4330#ifdef FEAT_QUICKFIX
4331 lnum = qf_get_cur_idx(eap);
4332#endif
4333 break;
4334 case ADDR_QUICKFIX_VALID:
4335#ifdef FEAT_QUICKFIX
4336 lnum = qf_get_cur_valid_idx(eap);
4337#endif
4338 break;
4339 case ADDR_NONE:
4340 // Will give an error later if a range is found.
4341 break;
4342 }
4343 return lnum;
4344}
4345
4346/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004347 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 *
4349 * Set ptr to the next character after the part that was interpreted.
4350 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004351 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 *
4353 * Return MAXLNUM when no Ex address was found.
4354 */
4355 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004356get_address(
4357 exarg_T *eap UNUSED,
4358 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004359 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004360 int skip, // only skip the address, don't use it
4361 int silent, // no errors or side effects
4362 int to_other_file, // flag: may jump to other file
4363 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
4365 int c;
4366 int i;
4367 long n;
4368 char_u *cmd;
4369 pos_T pos;
4370 pos_T *fp;
4371 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004372 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 cmd = skipwhite(*ptr);
4375 lnum = MAXLNUM;
4376 do
4377 {
4378 switch (*cmd)
4379 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004380 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004381 ++cmd;
4382 switch (addr_type)
4383 {
4384 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004385 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 lnum = curwin->w_cursor.lnum;
4387 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004389 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004390 break;
4391 case ADDR_ARGUMENTS:
4392 lnum = curwin->w_arg_idx + 1;
4393 break;
4394 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004395 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 lnum = curbuf->b_fnum;
4397 break;
4398 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004399 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004401 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004402 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004403 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004404 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004405 cmd = NULL;
4406 goto error;
4407 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004408 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004409#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004410 lnum = qf_get_cur_idx(eap);
4411#endif
4412 break;
4413 case ADDR_QUICKFIX_VALID:
4414#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004415 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004416#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004417 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004418 }
4419 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 ++cmd;
4423 switch (addr_type)
4424 {
4425 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004426 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 lnum = curbuf->b_ml.ml_line_count;
4428 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004430 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004431 break;
4432 case ADDR_ARGUMENTS:
4433 lnum = ARGCOUNT;
4434 break;
4435 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004436 buf = lastbuf;
4437 while (buf->b_ml.ml_mfp == NULL)
4438 {
4439 if (buf->b_prev == NULL)
4440 break;
4441 buf = buf->b_prev;
4442 }
4443 lnum = buf->b_fnum;
4444 break;
4445 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 lnum = lastbuf->b_fnum;
4447 break;
4448 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004449 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004451 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004452 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004453 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004454 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004455 cmd = NULL;
4456 goto error;
4457 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004458 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004459#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004460 lnum = qf_get_size(eap);
4461 if (lnum == 0)
4462 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004463#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004464 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004465 case ADDR_QUICKFIX_VALID:
4466#ifdef FEAT_QUICKFIX
4467 lnum = qf_get_valid_size(eap);
4468 if (lnum == 0)
4469 lnum = 1;
4470#endif
4471 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 }
4473 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004475 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 if (*++cmd == NUL)
4477 {
4478 cmd = NULL;
4479 goto error;
4480 }
4481 if (addr_type != ADDR_LINES)
4482 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004483 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004484 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 goto error;
4486 }
4487 if (skip)
4488 ++cmd;
4489 else
4490 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 // Only accept a mark in another file when it is
4492 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4494 ++cmd;
4495 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004497 lnum = curwin->w_cursor.lnum;
4498 else
4499 {
4500 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
4502 cmd = NULL;
4503 goto error;
4504 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 lnum = fp->lnum;
4506 }
4507 }
4508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004511 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004512 c = *cmd++;
4513 if (addr_type != ADDR_LINES)
4514 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004515 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004516 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 goto error;
4518 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004519 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004521 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004522 if (*cmd == c)
4523 ++cmd;
4524 }
4525 else
4526 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004527 int flags;
4528
4529 pos = curwin->w_cursor; // save curwin->w_cursor
4530
4531 // When '/' or '?' follows another address, start from
4532 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004533 if (lnum > 0 && lnum != MAXLNUM)
4534 curwin->w_cursor.lnum =
4535 lnum > curbuf->b_ml.ml_line_count
4536 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004537
4538 // Start a forward search at the end of the line (unless
4539 // before the first line).
4540 // Start a backward search at the start of the line.
4541 // This makes sure we never match in the current
4542 // line, and can match anywhere in the
4543 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004544 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 curwin->w_cursor.col = MAXCOL;
4546 else
4547 curwin->w_cursor.col = 0;
4548 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004549 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004550 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004551 {
4552 curwin->w_cursor = pos;
4553 cmd = NULL;
4554 goto error;
4555 }
4556 lnum = curwin->w_cursor.lnum;
4557 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004558 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 cmd += searchcmdlen;
4560 }
4561 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004563 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 ++cmd;
4565 if (addr_type != ADDR_LINES)
4566 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004567 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004568 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004569 goto error;
4570 }
4571 if (*cmd == '&')
4572 i = RE_SUBST;
4573 else if (*cmd == '?' || *cmd == '/')
4574 i = RE_SEARCH;
4575 else
4576 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004577 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 cmd = NULL;
4579 goto error;
4580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 if (!skip)
4583 {
4584 /*
4585 * When search follows another address, start from
4586 * there.
4587 */
4588 if (lnum != MAXLNUM)
4589 pos.lnum = lnum;
4590 else
4591 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 /*
4594 * Start the search just like for the above
4595 * do_search().
4596 */
4597 if (*cmd != '?')
4598 pos.col = MAXCOL;
4599 else
4600 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004601 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004602 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004604 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004605 lnum = pos.lnum;
4606 else
4607 {
4608 cmd = NULL;
4609 goto error;
4610 }
4611 }
4612 ++cmd;
4613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004616 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619
4620 for (;;)
4621 {
4622 cmd = skipwhite(cmd);
4623 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4624 break;
4625
4626 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004627 {
4628 switch (addr_type)
4629 {
4630 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004631 case ADDR_OTHER:
4632 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004633 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 break;
4635 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004636 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 break;
4638 case ADDR_ARGUMENTS:
4639 lnum = curwin->w_arg_idx + 1;
4640 break;
4641 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004642 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 lnum = curbuf->b_fnum;
4644 break;
4645 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004646 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004647 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004648 case ADDR_TABS_RELATIVE:
4649 lnum = 1;
4650 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004651 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004652#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004653 lnum = qf_get_cur_idx(eap);
4654#endif
4655 break;
4656 case ADDR_QUICKFIX_VALID:
4657#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004658 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004659#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004660 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004661 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004662 case ADDR_UNSIGNED:
4663 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004664 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004665 }
4666 }
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004669 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 else
4671 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004672 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 n = 1;
4674 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004675 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004676 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004678 if (n == MAXLNUM)
4679 {
4680 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004681 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004682 goto error;
4683 }
4684 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004685
4686 if (addr_type == ADDR_TABS_RELATIVE)
4687 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004688 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004689 cmd = NULL;
4690 goto error;
4691 }
4692 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004693 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004694 lnum = compute_buffer_local_count(
4695 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004697 {
4698#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004699 // Relative line addressing: need to adjust for lines in a
4700 // closed fold after the first address.
4701 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4702 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004703 (void)hasFolding(lnum, NULL, &lnum);
4704#endif
4705 if (i == '-')
4706 lnum -= n;
4707 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004708 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004709 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004710 {
4711 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004712 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004713 goto error;
4714 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004715 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004716 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 }
4719 } while (*cmd == '/' || *cmd == '?');
4720
4721error:
4722 *ptr = cmd;
4723 return lnum;
4724}
4725
4726/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004727 * Set eap->line1 and eap->line2 to the whole range.
4728 * Used for commands with the EX_DFLALL flag and no range given.
4729 */
4730 static void
4731address_default_all(exarg_T *eap)
4732{
4733 eap->line1 = 1;
4734 switch (eap->addr_type)
4735 {
4736 case ADDR_LINES:
4737 case ADDR_OTHER:
4738 eap->line2 = curbuf->b_ml.ml_line_count;
4739 break;
4740 case ADDR_LOADED_BUFFERS:
4741 {
4742 buf_T *buf = firstbuf;
4743
4744 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4745 buf = buf->b_next;
4746 eap->line1 = buf->b_fnum;
4747 buf = lastbuf;
4748 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4749 buf = buf->b_prev;
4750 eap->line2 = buf->b_fnum;
4751 }
4752 break;
4753 case ADDR_BUFFERS:
4754 eap->line1 = firstbuf->b_fnum;
4755 eap->line2 = lastbuf->b_fnum;
4756 break;
4757 case ADDR_WINDOWS:
4758 eap->line2 = LAST_WIN_NR;
4759 break;
4760 case ADDR_TABS:
4761 eap->line2 = LAST_TAB_NR;
4762 break;
4763 case ADDR_TABS_RELATIVE:
4764 eap->line2 = 1;
4765 break;
4766 case ADDR_ARGUMENTS:
4767 if (ARGCOUNT == 0)
4768 eap->line1 = eap->line2 = 0;
4769 else
4770 eap->line2 = ARGCOUNT;
4771 break;
4772 case ADDR_QUICKFIX_VALID:
4773#ifdef FEAT_QUICKFIX
4774 eap->line2 = qf_get_valid_size(eap);
4775 if (eap->line2 == 0)
4776 eap->line2 = 1;
4777#endif
4778 break;
4779 case ADDR_NONE:
4780 case ADDR_UNSIGNED:
4781 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004782 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004783 break;
4784 }
4785}
4786
4787
4788/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789 * Get flags from an Ex command argument.
4790 */
4791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004792get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004793{
4794 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4795 {
4796 if (*eap->arg == 'l')
4797 eap->flags |= EXFLAG_LIST;
4798 else if (*eap->arg == 'p')
4799 eap->flags |= EXFLAG_PRINT;
4800 else
4801 eap->flags |= EXFLAG_NR;
4802 eap->arg = skipwhite(eap->arg + 1);
4803 }
4804}
4805
4806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 * Function called for command which is Not Implemented. NI!
4808 */
4809 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004810ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004813 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004814 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815}
4816
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004817#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818/*
4819 * Function called for script command which is Not Implemented. NI!
4820 * Skips over ":perl <<EOF" constructs.
4821 */
4822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004823ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824{
4825 if (!eap->skip)
4826 ex_ni(eap);
4827 else
4828 vim_free(script_get(eap, eap->arg));
4829}
4830#endif
4831
4832/*
4833 * Check range in Ex command for validity.
4834 * Return NULL when valid, error message when invalid.
4835 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004836 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004837invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004839 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 if ( eap->line1 < 0
4842 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004844 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004845
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004846 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004847 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004848 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004849 {
4850 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004851 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004852#ifdef FEAT_DIFF
4853 + (eap->cmdidx == CMD_diffget)
4854#endif
4855 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004856 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004857 break;
4858 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004859 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004860 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004861 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004862 break;
4863 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004864 // Only a boundary check, not whether the buffers actually
4865 // exist.
4866 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004867 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004868 break;
4869 case ADDR_LOADED_BUFFERS:
4870 buf = firstbuf;
4871 while (buf->b_ml.ml_mfp == NULL)
4872 {
4873 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004874 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004875 buf = buf->b_next;
4876 }
4877 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004878 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004879 buf = lastbuf;
4880 while (buf->b_ml.ml_mfp == NULL)
4881 {
4882 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004883 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004884 buf = buf->b_prev;
4885 }
4886 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004887 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004888 break;
4889 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004890 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004891 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004892 break;
4893 case ADDR_TABS:
4894 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004895 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004896 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004897 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004898 case ADDR_OTHER:
4899 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004900 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004901 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004902#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004903 // No error for value that is too big, will use the last entry.
4904 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004905 {
4906 if (eap->addr_count == 0)
4907 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004908 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004909 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004910#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004911 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004912 case ADDR_QUICKFIX_VALID:
4913#ifdef FEAT_QUICKFIX
4914 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4915 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004916 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004917#endif
4918 break;
4919 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004920 case ADDR_NONE:
4921 // Will give an error elsewhere.
4922 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004923 }
4924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return NULL;
4926}
4927
4928/*
4929 * Correct the range for zero line number, if required.
4930 */
4931 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004932correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004934 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
4936 if (eap->line1 == 0)
4937 eap->line1 = 1;
4938 if (eap->line2 == 0)
4939 eap->line2 = 1;
4940 }
4941}
4942
Bram Moolenaar748bf032005-02-02 23:04:36 +00004943#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004944/*
4945 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4946 * pattern. Otherwise return eap->arg.
4947 */
4948 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004949skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004950{
4951 char_u *p = eap->arg;
4952
Bram Moolenaara37420f2006-02-04 22:37:47 +00004953 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4954 || eap->cmdidx == CMD_vimgrepadd
4955 || eap->cmdidx == CMD_lvimgrepadd
4956 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004957 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004958 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004959 if (p == NULL)
4960 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004961 }
4962 return p;
4963}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004964
4965/*
4966 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4967 * in the command line, so that things like % get expanded.
4968 */
4969 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004970replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004971{
4972 char_u *new_cmdline;
4973 char_u *program;
4974 char_u *pos;
4975 char_u *ptr;
4976 int len;
4977 int i;
4978
4979 /*
4980 * Don't do it when ":vimgrep" is used for ":grep".
4981 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004982 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4983 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4984 || eap->cmdidx == CMD_grepadd
4985 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004986 && !grep_internal(eap->cmdidx))
4987 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004988 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4989 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004990 {
4991 if (*curbuf->b_p_gp == NUL)
4992 program = p_gp;
4993 else
4994 program = curbuf->b_p_gp;
4995 }
4996 else
4997 {
4998 if (*curbuf->b_p_mp == NUL)
4999 program = p_mp;
5000 else
5001 program = curbuf->b_p_mp;
5002 }
5003
5004 p = skipwhite(p);
5005
5006 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005008 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005009 i = 1;
5010 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5011 ++i;
5012 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005013 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005014 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005015 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005016 ptr = new_cmdline;
5017 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5018 {
5019 i = (int)(pos - program);
5020 STRNCPY(ptr, program, i);
5021 STRCPY(ptr += i, p);
5022 ptr += len;
5023 program = pos + 2;
5024 }
5025 STRCPY(ptr, program);
5026 }
5027 else
5028 {
John Marriotted908f72024-04-18 22:46:56 +02005029 size_t program_len = STRLEN(program);
5030
5031 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005032 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005033 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005034 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005035 STRCPY(new_cmdline + program_len, " ");
5036 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005037 }
5038 msg_make(p);
5039
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005041 vim_free(*cmdlinep);
5042 *cmdlinep = new_cmdline;
5043 p = new_cmdline;
5044 }
5045 return p;
5046}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005047#endif
5048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049/*
5050 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005051 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 * Return FAIL for failure, OK otherwise.
5053 */
5054 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005055expand_filename(
5056 exarg_T *eap,
5057 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005058 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005060 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005062 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *p;
5064 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005065 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
Bram Moolenaar748bf032005-02-02 23:04:36 +00005067#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005068 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005069 p = skip_grep_pat(eap);
5070#else
5071 p = eap->arg;
5072#endif
5073
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 /*
5075 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5076 * the file name contains a wildcard it should not cause expanding.
5077 * (it will be expanded anyway if there is a wildcard before replacing).
5078 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005079 has_wildcards = mch_has_wildcard(p);
5080 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005082#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005083 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005084 if (p[0] == '`' && p[1] == '=')
5085 {
5086 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005087 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005088 if (*p == '`')
5089 ++p;
5090 continue;
5091 }
5092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 /*
5094 * Quick check if this cannot be the start of a special string.
5095 * Also removes backslash before '%', '#' and '<'.
5096 */
5097 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5098 {
5099 ++p;
5100 continue;
5101 }
5102
5103 /*
5104 * Try to find a match at this position.
5105 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005106 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005107 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005108 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005110 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 p += srclen;
5113 continue;
5114 }
5115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005116 // Wildcards won't be expanded below, the replacement is taken
5117 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005118 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5119 {
5120 char_u *l = repl;
5121
5122 repl = expand_env_save(repl);
5123 vim_free(l);
5124 }
5125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005126 // Need to escape white space et al. with a backslash.
5127 // Don't do this for:
5128 // - replacement that already has been escaped: "##"
5129 // - shell commands (may have to use quotes instead).
5130 // - non-unix systems when there is a single argument (spaces don't
5131 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005133 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 && eap->cmdidx != CMD_grep
5136 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005137 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005138 && eap->cmdidx != CMD_lgrep
5139 && eap->cmdidx != CMD_lgrepadd
5140 && eap->cmdidx != CMD_lmake
5141 && eap->cmdidx != CMD_make
5142 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005144 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145#endif
5146 )
5147 {
5148 char_u *l;
5149#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005150 // Don't escape a backslash here, because rem_backslash() doesn't
5151 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 static char_u *nobslash = (char_u *)" \t\"|";
5153# define ESCAPE_CHARS nobslash
5154#else
5155# define ESCAPE_CHARS escape_chars
5156#endif
5157
5158 for (l = repl; *l; ++l)
5159 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5160 {
5161 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5162 if (l != NULL)
5163 {
5164 vim_free(repl);
5165 repl = l;
5166 }
5167 break;
5168 }
5169 }
5170
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005171 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005172 if ((eap->usefilter || eap->cmdidx == CMD_bang
5173 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005174 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
5176 char_u *l;
5177
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005178 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if (l != NULL)
5180 {
5181 vim_free(repl);
5182 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184 }
5185
Mike Williams51024bb2024-05-30 07:46:30 +02005186 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 vim_free(repl);
5188 if (p == NULL)
5189 return FAIL;
5190 }
5191
5192 /*
5193 * One file argument: Expand wildcards.
5194 * Don't do this with ":r !command" or ":w !command".
5195 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005196 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
5198 /*
5199 * May do this twice:
5200 * 1. Replace environment variables.
5201 * 2. Replace any other wildcards, remove backslashes.
5202 */
5203 for (n = 1; n <= 2; ++n)
5204 {
5205 if (n == 2)
5206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 /*
5208 * Halve the number of backslashes (this is Vi compatible).
5209 * For Unix and OS/2, when wildcards are expanded, this is
5210 * done by ExpandOne() below.
5211 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005212#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (!has_wildcards)
5214#endif
5215 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217
5218 if (has_wildcards)
5219 {
5220 if (n == 1)
5221 {
5222 /*
5223 * First loop: May expand environment variables. This
5224 * can be done much faster with expand_env() than with
5225 * something else (e.g., calling a shell).
5226 * After expanding environment variables, check again
5227 * if there are still wildcards present.
5228 */
5229 if (vim_strchr(eap->arg, '$') != NULL
5230 || vim_strchr(eap->arg, '~') != NULL)
5231 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005232 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005233 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 has_wildcards = mch_has_wildcard(NameBuff);
5235 p = NameBuff;
5236 }
5237 else
5238 p = NULL;
5239 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005240 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005243 int options = WILD_LIST_NOTFOUND
5244 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 ExpandInit(&xpc);
5247 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005248 if (p_wic)
5249 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005251 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 if (p == NULL)
5253 return FAIL;
5254 }
5255 if (p != NULL)
5256 {
John Marriotted908f72024-04-18 22:46:56 +02005257 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005259 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 vim_free(p);
5261 }
5262 }
5263 }
5264 }
5265 return OK;
5266}
5267
5268/*
5269 * Replace part of the command line, keeping eap->cmd, eap->arg and
5270 * eap->nextcmd correct.
5271 * "src" points to the part that is to be replaced, of length "srclen".
5272 * "repl" is the replacement string.
5273 * Returns a pointer to the character after the replaced string.
5274 * Returns NULL for failure.
5275 */
5276 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005277repl_cmdline(
5278 exarg_T *eap,
5279 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005280 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005281 char_u *repl,
5282 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
John Marriotted908f72024-04-18 22:46:56 +02005284 size_t repllen;
5285 size_t taillen;
5286 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005288 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
5290 /*
5291 * The new command line is build in new_cmdline[].
5292 * First allocate it.
5293 * Careful: a "+cmd" argument may have been NUL terminated.
5294 */
John Marriotted908f72024-04-18 22:46:56 +02005295 repllen = STRLEN(repl);
5296 taillen = STRLEN(src + srclen);
5297 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005298 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005299 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005300 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005301 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
5303 /*
5304 * Copy the stuff before the expanded part.
5305 * Copy the expanded stuff.
5306 * Copy what came after the expanded part.
5307 * Copy the next commands, if there are any.
5308 */
John Marriotted908f72024-04-18 22:46:56 +02005309 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5310 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005311
John Marriotted908f72024-04-18 22:46:56 +02005312 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5313 new_cmdlinelen += repllen; // remember the end of the string
5314 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5315 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005317 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
John Marriotted908f72024-04-18 22:46:56 +02005319 new_cmdlinelen += taillen + 1;
5320 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5321 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
5323 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5324 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5325 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5326 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5327 vim_free(*cmdlinep);
5328 *cmdlinep = new_cmdline;
5329
5330 return src;
5331}
5332
5333/*
5334 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005335 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 */
5337 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005338separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
5340 char_u *p;
5341
Bram Moolenaar86b68352004-12-27 21:59:20 +00005342#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005343 p = skip_grep_pat(eap);
5344#else
5345 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005346#endif
5347
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005348 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 if (*p == Ctrl_V)
5351 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005352 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005353 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005355 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005356 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005357 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 break;
5359 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005360
5361#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005362 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005363 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005364 {
5365 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005366 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005367 if (*p == NUL) // stop at NUL after CTRL-V
5368 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005369 }
5370#endif
5371
Bram Moolenaareda29c92022-10-02 12:59:00 +01005372 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005373 // :@" and :*" do not start a comment!
5374 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005375 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005376 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005377 && !(eap->argt & EX_NOTRLCOM)
5378 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5379 || p != eap->arg)
5380 && (eap->cmdidx != CMD_redir
5381 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005382 || (*p == '#'
5383 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005384 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005385 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005386 || (*p == '|'
5387 && eap->cmdidx != CMD_append
5388 && eap->cmdidx != CMD_change
5389 && eap->cmdidx != CMD_insert)
5390 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
5392 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005393 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 * AND 'b' is present in 'cpoptions'.
5395 */
5396 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005397 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005399 if (!keep_backslash)
5400 {
5401 STRMOVE(p - 1, p); // remove the '\'
5402 --p;
5403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 }
5405 else
5406 {
5407 eap->nextcmd = check_nextcmd(p);
5408 *p = NUL;
5409 break;
5410 }
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005413
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005414 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 del_trailing_spaces(eap->arg);
5416}
5417
5418/*
5419 * get + command from ex argument
5420 */
5421 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005422getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 char_u *arg = *argp;
5425 char_u *command = NULL;
5426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005427 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
5429 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005430 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 command = dollar_command;
5432 else
5433 {
5434 command = arg;
5435 arg = skip_cmd_arg(command, TRUE);
5436 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005437 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
5439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 *argp = arg;
5442 }
5443 return command;
5444}
5445
5446/*
5447 * Find end of "+command" argument. Skip over "\ " and "\\".
5448 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005449 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005450skip_cmd_arg(
5451 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005452 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
5454 while (*p && !vim_isspace(*p))
5455 {
5456 if (*p == '\\' && p[1] != NUL)
5457 {
5458 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005459 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 else
5461 ++p;
5462 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005463 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465 return p;
5466}
5467
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005468 int
5469get_bad_opt(char_u *p, exarg_T *eap)
5470{
5471 if (STRICMP(p, "keep") == 0)
5472 eap->bad_char = BAD_KEEP;
5473 else if (STRICMP(p, "drop") == 0)
5474 eap->bad_char = BAD_DROP;
5475 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5476 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005477 else
5478 return FAIL;
5479 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005480}
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005483 * Function given to ExpandGeneric() to obtain the list of bad= names.
5484 */
5485 static char_u *
5486get_bad_name(expand_T *xp UNUSED, int idx)
5487{
5488 // Note: Keep this in sync with getargopt.
5489 static char *(p_bad_values[]) =
5490 {
5491 "?",
5492 "keep",
5493 "drop",
5494 };
5495
John Marriotted908f72024-04-18 22:46:56 +02005496 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5497 return NULL;
5498
5499 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005500}
5501
5502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 * Get "++opt=arg" argument.
5504 * Return FAIL or OK.
5505 */
5506 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 char_u *arg = eap->arg + 2;
5510 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005511 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005514 // Note: Keep this in sync with get_argopt_name.
5515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005516 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5518 {
5519 if (*arg == 'n')
5520 {
5521 arg += 2;
5522 eap->force_bin = FORCE_NOBIN;
5523 }
5524 else
5525 eap->force_bin = FORCE_BIN;
5526 if (!checkforcmd(&arg, "binary", 3))
5527 return FAIL;
5528 eap->arg = skipwhite(arg);
5529 return OK;
5530 }
5531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005532 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005533 if (STRNCMP(arg, "edit", 4) == 0)
5534 {
5535 eap->read_edit = TRUE;
5536 eap->arg = skipwhite(arg + 4);
5537 return OK;
5538 }
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 if (STRNCMP(arg, "ff", 2) == 0)
5541 {
5542 arg += 2;
5543 pp = &eap->force_ff;
5544 }
5545 else if (STRNCMP(arg, "fileformat", 10) == 0)
5546 {
5547 arg += 10;
5548 pp = &eap->force_ff;
5549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 else if (STRNCMP(arg, "enc", 3) == 0)
5551 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005552 if (STRNCMP(arg, "encoding", 8) == 0)
5553 arg += 8;
5554 else
5555 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 pp = &eap->force_enc;
5557 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005558 else if (STRNCMP(arg, "bad", 3) == 0)
5559 {
5560 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005561 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
5564 if (pp == NULL || *arg != '=')
5565 return FAIL;
5566
5567 ++arg;
5568 *pp = (int)(arg - eap->cmd);
5569 arg = skip_cmd_arg(arg, FALSE);
5570 eap->arg = skipwhite(arg);
5571 *arg = NUL;
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (pp == &eap->force_ff)
5574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5576 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005577 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005579 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005581 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5583 *p = TOLOWER_ASC(*p);
5584 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005585 else
5586 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005587 // Check ++bad= argument. Must be a single-byte character, "keep" or
5588 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005589 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005590 return FAIL;
5591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
5593 return OK;
5594}
5595
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005596/*
5597 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5598 */
5599 static char_u *
5600get_argopt_name(expand_T *xp UNUSED, int idx)
5601{
5602 // Note: Keep this in sync with getargopt.
5603 static char *(p_opt_values[]) =
5604 {
5605 "fileformat=",
5606 "encoding=",
5607 "binary",
5608 "nobinary",
5609 "bad=",
5610 "edit",
5611 };
5612
John Marriotted908f72024-04-18 22:46:56 +02005613 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5614 return NULL;
5615
5616 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005617}
5618
5619/*
5620 * Command-line expansion for ++opt=name.
5621 */
5622 int
5623expand_argopt(
5624 char_u *pat,
5625 expand_T *xp,
5626 regmatch_T *rmp,
5627 char_u ***matches,
5628 int *numMatches)
5629{
5630 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5631 {
5632 char_u *(*cb)(expand_T *, int) = NULL;
5633
5634 char_u *name_end = xp->xp_pattern - 1;
5635 if (name_end - xp->xp_line >= 2
5636 && STRNCMP(name_end - 2, "ff", 2) == 0)
5637 cb = get_fileformat_name;
5638 else if (name_end - xp->xp_line >= 10
5639 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5640 cb = get_fileformat_name;
5641 else if (name_end - xp->xp_line >= 3
5642 && STRNCMP(name_end - 3, "enc", 3) == 0)
5643 cb = get_encoding_name;
5644 else if (name_end - xp->xp_line >= 8
5645 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5646 cb = get_encoding_name;
5647 else if (name_end - xp->xp_line >= 3
5648 && STRNCMP(name_end - 3, "bad", 3) == 0)
5649 cb = get_bad_name;
5650
5651 if (cb != NULL)
5652 {
5653 return ExpandGeneric(
5654 pat,
5655 xp,
5656 rmp,
5657 matches,
5658 numMatches,
5659 cb,
5660 FALSE);
5661 }
5662 return FAIL;
5663 }
5664
5665 // Special handling of "ff" which acts as a short form of
5666 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005667 if (xp->xp_pattern_len == 2
5668 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005669 {
5670 *matches = ALLOC_MULT(char_u *, 1);
5671 if (*matches == NULL)
5672 return FAIL;
5673 *numMatches = 1;
5674 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5675 return OK;
5676 }
5677
5678 return ExpandGeneric(
5679 pat,
5680 xp,
5681 rmp,
5682 matches,
5683 numMatches,
5684 get_argopt_name,
5685 FALSE);
5686}
5687
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
5691 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005692 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 * directory for security reasons.
5694 */
5695 if (secure)
5696 {
5697 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005698 eap->errmsg =
5699 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
5701 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005702 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 else
5704 do_augroup(eap->arg, eap->forceit);
5705}
5706
5707/*
5708 * ":doautocmd": Apply the automatic commands to the current buffer.
5709 */
5710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005711ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005713 char_u *arg = eap->arg;
5714 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005715 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005716
Bram Moolenaar1610d052016-06-09 22:53:01 +02005717 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005718 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005719 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005720 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723/*
5724 * :[N]bunload[!] [N] [bufname] unload buffer
5725 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5726 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5727 */
5728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005729ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005731 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005732 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 eap->errmsg = do_bufdel(
5734 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5735 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5736 : DOBUF_UNLOAD, eap->arg,
5737 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5738}
5739
5740/*
5741 * :[N]buffer [N] to buffer N
5742 * :[N]sbuffer [N] to buffer N
5743 */
5744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005745ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005747 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005748 return;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01005749 do_exbuffer(eap);
5750}
5751
5752/*
5753 * ":buffer" command and alike.
5754 */
5755 static void
5756do_exbuffer(exarg_T *eap)
5757{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005759 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 else
5761 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005762 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5764 else
5765 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005766 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005767 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769}
5770
5771/*
5772 * :[N]bmodified [N] to next mod. buffer
5773 * :[N]sbmodified [N] to next mod. buffer
5774 */
5775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005776ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
5778 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005779 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005780 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781}
5782
5783/*
5784 * :[N]bnext [N] to next buffer
5785 * :[N]sbnext [N] split and to next buffer
5786 */
5787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005788ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005790 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005791 return;
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005794 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005795 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796}
5797
5798/*
5799 * :[N]bNext [N] to previous buffer
5800 * :[N]bprevious [N] to previous buffer
5801 * :[N]sbNext [N] split and to previous buffer
5802 * :[N]sbprevious [N] split and to previous buffer
5803 */
5804 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005805ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005807 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005808 return;
5809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005811 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005812 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
5815/*
5816 * :brewind to first buffer
5817 * :bfirst to first buffer
5818 * :sbrewind split and to first buffer
5819 * :sbfirst split and to first buffer
5820 */
5821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005822ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005824 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005825 return;
5826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005828 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005829 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830}
5831
5832/*
5833 * :blast to last buffer
5834 * :sblast split and to last buffer
5835 */
5836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005837ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005839 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005840 return;
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005843 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005844 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
Bram Moolenaar7a092242020-04-16 22:10:49 +02005847/*
5848 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005849 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005852ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005854 int comment_char = '"';
5855
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005856 if (in_vim9script())
5857 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005858 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005859}
5860
5861/*
5862 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5863 * to "cmd_start" or has a white space character before it.
5864 */
5865 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005866ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005867{
5868 int c = *cmd;
5869
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005870 if (c == NUL || c == '|' || c == '\n')
5871 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005872 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005873 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5874 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005875 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005876 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877}
5878
5879#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5880 || defined(PROTO)
5881/*
5882 * Return the next command, after the first '|' or '\n'.
5883 * Return NULL if not found.
5884 */
5885 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005886find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 while (*p != '|' && *p != '\n')
5889 {
5890 if (*p == NUL)
5891 return NULL;
5892 ++p;
5893 }
5894 return (p + 1);
5895}
5896#endif
5897
5898/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005899 * Check if *p is a separator between Ex commands, skipping over white space.
5900 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 */
5902 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005903check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005905 char_u *s = skipwhite(p);
5906
5907 if (*s == '|' || *s == '\n')
5908 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 else
5910 return NULL;
5911}
5912
5913/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005914 * If "eap->nextcmd" is not set, check for a next command at "p".
5915 */
5916 void
5917set_nextcmd(exarg_T *eap, char_u *arg)
5918{
5919 char_u *p = check_nextcmd(arg);
5920
5921 if (eap->nextcmd == NULL)
5922 eap->nextcmd = p;
5923 else if (p != NULL)
5924 // cannot use "| command" inside a {} block
5925 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5926}
5927
5928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 * - if there are more files to edit
5930 * - and this is the last window
5931 * - and forceit not used
5932 * - and not repeated twice on a row
5933 * return FAIL and give error message if 'message' TRUE
5934 * return OK otherwise
5935 */
5936 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005937check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005938 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005939 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
5941 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5942
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005943 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005944 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {
5946 if (message)
5947 {
5948#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005949 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5950 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005952 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005954 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5955 NGETTEXT("%d more file to edit. Quit anyway?",
5956 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5958 return OK;
5959 return FAIL;
5960 }
5961#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005962 semsg(NGETTEXT(e_nr_more_file_to_edit,
5963 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005964 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 }
5966 return FAIL;
5967 }
5968 return OK;
5969}
5970
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971/*
5972 * Function given to ExpandGeneric() to obtain the list of command names.
5973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005975get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005978 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005979 // the following are no real commands
5980 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02005981 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
Christian Brabandt70a11a62024-07-26 19:13:55 +02005982 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 return cmdnames[idx].cmd_name;
5984}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005987ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
Bram Moolenaare6850792010-05-14 15:28:44 +02005989 if (*eap->arg == NUL)
5990 {
5991#ifdef FEAT_EVAL
5992 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5993 char_u *p = NULL;
5994
5995 if (expr != NULL)
5996 {
5997 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005998 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005999 --emsg_off;
6000 vim_free(expr);
6001 }
6002 if (p != NULL)
6003 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006004 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02006005 vim_free(p);
6006 }
6007 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006008 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006009#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006010 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006011#endif
6012 }
6013 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006014 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006015
6016#ifdef FEAT_VTP
6017 else if (has_vtp_working())
6018 {
6019 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006020 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006021 redrawcmd();
6022 }
6023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024}
6025
6026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006027ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006030 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 do_highlight(eap->arg, eap->forceit, FALSE);
6032}
6033
6034
6035/*
6036 * Call this function if we thought we were going to exit, but we won't
6037 * (because of an error). May need to restore the terminal mode.
6038 */
6039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006040not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 exiting = FALSE;
6043 settmode(TMODE_RAW);
6044}
6045
Bram Moolenaar3552e742021-05-29 12:21:58 +02006046 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006047before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6048{
6049 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6050
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006051 // Bail out when autocommands closed the window.
6052 // Refuse to quit when the buffer in the last window is being closed (can
6053 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006054 if (!win_valid(wp)
6055 || curbuf_locked()
6056 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6057 return TRUE;
6058
6059 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6060 {
6061 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006062 // Refuse to quit when locked or when the window was closed or the
6063 // buffer in the last window is being closed (can only happen in
6064 // autocommands).
6065 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006066 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6067 return TRUE;
6068 }
6069
6070 return FALSE;
6071}
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006074 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006075 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006076 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006078 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006079ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006081 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (cmdwin_type != 0)
6084 {
6085 cmdwin_result = Ctrl_C;
6086 return;
6087 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006088 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006089 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006090 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006091 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006092 return;
6093 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006094 if (eap->addr_count > 0)
6095 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006096 int wnr = eap->line2;
6097
6098 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6099 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006100 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006101 }
6102 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006103 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006105 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006106 if (curbuf_locked())
6107 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006109 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006110 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006111 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
6113#ifdef FEAT_NETBEANS_INTG
6114 netbeansForcedQuit = eap->forceit;
6115#endif
6116
6117 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006118 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 */
6120 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6121 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006122 if ((!buf_hide(wp->w_buffer)
6123 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006124 | (eap->forceit ? CCGD_FORCEIT : 0)
6125 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006127 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
6129 not_exiting();
6130 }
6131 else
6132 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006133 // quit last window
6134 // Note: only_one_window() returns true, even so a help window is
6135 // still open. In that case only quit, if no address has been
6136 // specified. Example:
6137 // :h|wincmd w|1q - don't quit
6138 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006139 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006141 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006142#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006144#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006145 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006146 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 }
6148}
6149
6150/*
6151 * ":cquit".
6152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006154ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006156 // this does not always pass on the exit code to the Manx compiler. why?
6157 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158}
6159
6160/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006161 * Do preparations for "qall" and "wqall".
6162 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006164 int
6165before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 if (cmdwin_type != 0)
6168 {
6169 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006170 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 else
6172 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006173 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006175
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006176 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006177 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006178 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006179 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006180 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006181 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006182
6183 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006184 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006185
Bram Moolenaar411da642023-05-10 16:53:27 +01006186 return OK;
6187}
6188
6189/*
6190 * ":qall": try to quit all windows
6191 */
6192 static void
6193ex_quit_all(exarg_T *eap)
6194{
6195 if (before_quit_all(eap) == FAIL)
6196 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006198 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 getout(0);
6200 not_exiting();
6201}
6202
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203/*
6204 * ":close": close current window, unless it is the last one
6205 */
6206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006207ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006209 win_T *win;
6210 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006212 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006214 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006215 {
6216 if (eap->addr_count == 0)
6217 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006218 else
6219 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006220 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006221 {
6222 winnr++;
6223 if (winnr == eap->line2)
6224 break;
6225 }
6226 if (win == NULL)
6227 win = lastwin;
6228 ex_win_close(eap->forceit, win, NULL);
6229 }
6230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231}
6232
Bram Moolenaar4033c552017-09-16 20:54:51 +02006233#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234/*
6235 * ":pclose": Close any preview window.
6236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006238ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006239{
6240 win_T *win;
6241
Bram Moolenaar79648732019-07-18 21:43:07 +02006242 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006243 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006244 if (win->w_p_pvw)
6245 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006246 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006247 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006249# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006250 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006251 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006252# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006253}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006254#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006255
Bram Moolenaarf740b292006-02-16 22:11:02 +00006256/*
6257 * Close window "win" and take care of handling closing the last window for a
6258 * modified buffer.
6259 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006261ex_win_close(
6262 int forceit,
6263 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006264 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
6266 int need_hide;
6267 buf_T *buf = win->w_buffer;
6268
Bram Moolenaarcf844172020-06-26 19:44:06 +02006269 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006270 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006271 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006272 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006273 return;
6274 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006275 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006276 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006279 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006281#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006282 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006284# ifdef FEAT_TERMINAL
6285 if (term_job_running(buf->b_term))
6286 {
6287 if (term_confirm_stop(buf) == FAIL)
6288 return;
6289 // Manually kill the terminal here because this command will
6290 // hide it otherwise.
6291 free_terminal(buf);
6292 need_hide = FALSE;
6293 }
6294 else
6295# endif
6296 {
6297 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006298
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006299 set_bufref(&bufref, buf);
6300 dialog_changed(buf, FALSE);
6301 if (bufref_valid(&bufref) && bufIsChanged(buf))
6302 return;
6303 need_hide = FALSE;
6304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
6306 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006309 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 return;
6311 }
6312 }
6313
Bram Moolenaar4033c552017-09-16 20:54:51 +02006314#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006316#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006318 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006319 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006320 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006321 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006322 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323}
6324
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006326 * Handle the argument for a tabpage related ex command.
6327 * Returns a tabpage number.
6328 * When an error is encountered then eap->errmsg is set.
6329 */
6330 static int
6331get_tabpage_arg(exarg_T *eap)
6332{
6333 int tab_number;
6334 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6335
6336 if (eap->arg && *eap->arg != NUL)
6337 {
6338 char_u *p = eap->arg;
6339 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006340 int relative = 0; // argument +N/-N means: go to N places to the
6341 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006342
6343 if (*p == '-')
6344 {
6345 relative = -1;
6346 p++;
6347 }
6348 else if (*p == '+')
6349 {
6350 relative = 1;
6351 p++;
6352 }
6353
6354 p_save = p;
6355 tab_number = getdigits(&p);
6356
6357 if (relative == 0)
6358 {
6359 if (STRCMP(p, "$") == 0)
6360 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006361 else if (STRCMP(p, "#") == 0)
6362 if (valid_tabpage(lastused_tabpage))
6363 tab_number = tabpage_index(lastused_tabpage);
6364 else
6365 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006366 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6367 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006368 tab_number = 0;
6369 goto theend;
6370 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006371 else if (p == p_save || *p_save == '-' || *p != NUL
6372 || tab_number > LAST_TAB_NR)
6373 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006374 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006375 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006376 goto theend;
6377 }
6378 }
6379 else
6380 {
6381 if (*p_save == NUL)
6382 tab_number = 1;
6383 else if (p == p_save || *p_save == '-' || *p != NUL
6384 || tab_number == 0)
6385 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006386 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006387 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006388 goto theend;
6389 }
6390 tab_number = tab_number * relative + tabpage_index(curtab);
6391 if (!unaccept_arg0 && relative == -1)
6392 --tab_number;
6393 }
6394 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006395 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006396 }
6397 else if (eap->addr_count > 0)
6398 {
6399 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006400 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006401 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006402 tab_number = 0;
6403 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006404 else
6405 {
6406 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006407 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006408 {
zeertzjq076faac2024-03-25 16:41:06 +01006409 char_u *cmdp = eap->cmd;
6410
6411 while (--cmdp > *eap->cmdlinep
6412 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6413 ;
6414 if (*cmdp == '-')
6415 {
6416 --tab_number;
6417 if (tab_number < unaccept_arg0)
6418 eap->errmsg = _(e_invalid_range);
6419 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006420 }
6421 }
6422 }
6423 else
6424 {
6425 switch (eap->cmdidx)
6426 {
6427 case CMD_tabnext:
6428 tab_number = tabpage_index(curtab) + 1;
6429 if (tab_number > LAST_TAB_NR)
6430 tab_number = 1;
6431 break;
6432 case CMD_tabmove:
6433 tab_number = LAST_TAB_NR;
6434 break;
6435 default:
6436 tab_number = tabpage_index(curtab);
6437 }
6438 }
6439
6440theend:
6441 return tab_number;
6442}
6443
6444/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006445 * ":tabclose": close current tab page, unless it is the last one.
6446 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 */
6448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006449ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006451 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006452 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006453
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006454 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006455 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006456 cmdwin_result = K_IGNORE;
6457 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006458 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006459
6460 if (first_tabpage->tp_next == NULL)
6461 {
6462 emsg(_(e_cannot_close_last_tab_page));
6463 return;
6464 }
6465
6466 if (window_layout_locked(CMD_tabclose))
6467 return;
6468
6469 tab_number = get_tabpage_arg(eap);
6470 if (eap->errmsg != NULL)
6471 return;
6472
6473 tp = find_tabpage(tab_number);
6474 if (tp == NULL)
6475 {
6476 beep_flush();
6477 return;
6478 }
6479 if (tp != curtab)
6480 {
6481 tabpage_close_other(tp, eap->forceit);
6482 return;
6483 }
6484 else if (!text_locked() && !curbuf_locked())
6485 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006486}
6487
6488/*
6489 * ":tabonly": close all tab pages except the current one
6490 */
6491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006492ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006493{
6494 tabpage_T *tp;
6495 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006496 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006497
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006498 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006499 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006500 cmdwin_result = K_IGNORE;
6501 return;
6502 }
6503
6504 if (first_tabpage->tp_next == NULL)
6505 {
6506 msg(_("Already only one tab page"));
6507 return;
6508 }
6509
6510 if (window_layout_locked(CMD_tabonly))
6511 return;
6512
6513 tab_number = get_tabpage_arg(eap);
6514 if (eap->errmsg != NULL)
6515 return;
6516
6517 goto_tabpage(tab_number);
6518 // Repeat this up to a 1000 times, because autocommands may
6519 // mess up the lists.
6520 for (done = 0; done < 1000; ++done)
6521 {
6522 FOR_ALL_TABPAGES(tp)
6523 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006524 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006525 tabpage_close_other(tp, eap->forceit);
6526 // if we failed to close it quit
6527 if (valid_tabpage(tp))
6528 done = 1000;
6529 // start over, "tp" is now invalid
6530 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006531 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006532 if (first_tabpage->tp_next == NULL)
6533 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
6537/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006538 * Close the current tab page.
6539 */
6540 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006541tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006542{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006543 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006544 return;
6545
Jim Zhoubcf66e02025-03-16 20:24:57 +01006546 trigger_tabclosedpre(curtab, TRUE);
6547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006548 // First close all the windows but the current one. If that worked then
6549 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006550 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006551 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006552 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006553 ex_win_close(forceit, curwin, NULL);
6554# ifdef FEAT_GUI
6555 need_mouse_correct = TRUE;
6556# endif
6557}
6558
6559/*
6560 * Close tab page "tp", which is not the current tab page.
6561 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006562 * Also takes care of the tab pages line disappearing when closing the
6563 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006564 */
6565 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006566tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006567{
6568 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006569 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006570
Jim Zhoubcf66e02025-03-16 20:24:57 +01006571 trigger_tabclosedpre(tp, TRUE);
6572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006573 // Limit to 1000 windows, autocommands may add a window while we close
6574 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006575 while (++done < 1000)
6576 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006577 wp = tp->tp_firstwin;
6578 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006580 // Autocommands may delete the tab page under our fingers and we may
6581 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006582 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006583 break;
6584 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006585
Bram Moolenaar29323592016-07-24 22:04:11 +02006586 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006587}
6588
6589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 * ":only".
6591 */
6592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006593ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006595 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006596 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597# ifdef FEAT_GUI
6598 need_mouse_correct = TRUE;
6599# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006600 if (eap->addr_count > 0)
6601 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006602 win_T *wp;
6603 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006604 for (wp = firstwin; --wnr > 0; )
6605 {
6606 if (wp->w_next == NULL)
6607 break;
6608 else
6609 wp = wp->w_next;
6610 }
6611 win_goto(wp);
6612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 close_others(TRUE, eap->forceit);
6614}
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006617ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006619 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006620 if (eap->skip)
6621 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006622
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006623 if (window_layout_locked(CMD_hide))
6624 return;
6625#ifdef FEAT_GUI
6626 need_mouse_correct = TRUE;
6627#endif
6628 if (eap->addr_count == 0)
6629 win_close(curwin, FALSE); // don't free buffer
6630 else
6631 {
6632 int winnr = 0;
6633 win_T *win;
6634
6635 FOR_ALL_WINDOWS(win)
6636 {
6637 winnr++;
6638 if (winnr == eap->line2)
6639 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006641 if (win == NULL)
6642 win = lastwin;
6643 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645}
6646
6647/*
6648 * ":stop" and ":suspend": Suspend Vim.
6649 */
dbivolaruab16ad32021-12-29 19:41:47 +00006650 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006651ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
6653 /*
6654 * Disallow suspending for "rvim".
6655 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006656 if (check_restricted())
6657 return;
6658
6659 if (!eap->forceit)
6660 autowrite_all();
6661 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6662 windgoto((int)Rows - 1, 0);
6663 out_char('\n');
6664 out_flush();
6665 stoptermcap();
6666 out_flush(); // needed for SUN to restore xterm buffer
6667 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6668 ui_suspend(); // call machine specific function
6669 maketitle();
6670 resettitle(); // force updating the title
6671 starttermcap();
6672 scroll_start(); // scroll screen before redrawing
6673 redraw_later_clear();
6674 shell_resized(); // may have resized window
6675 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676}
6677
6678/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006679 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 */
6681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006682ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006684#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006685 if (not_in_vim9(eap) == FAIL)
6686 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 if (cmdwin_type != 0)
6689 {
6690 cmdwin_result = Ctrl_C;
6691 return;
6692 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006693 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006694 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006695 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006696 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006697 return;
6698 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006701 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 */
6703 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6704 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006705
6706 // Write the buffer for ":wq" or when it was changed.
6707 // Trigger QuitPre and ExitPre.
6708 // Check if we can exit now, after autocommands have changed things.
6709 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6710 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006712 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
6714 not_exiting();
6715 }
6716 else
6717 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006718 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006720 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721# ifdef FEAT_GUI
6722 need_mouse_correct = TRUE;
6723# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006724 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006725 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 }
6727}
6728
6729/*
6730 * ":print", ":list", ":number".
6731 */
6732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006733ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006735 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006736 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006737 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006739 for ( ;!got_int; ui_breakcheck())
6740 {
6741 print_line(eap->line1,
6742 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6743 || (eap->flags & EXFLAG_NR)),
6744 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6745 if (++eap->line1 > eap->line2)
6746 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006747 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006748 }
6749 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006750 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006751 curwin->w_cursor.lnum = eap->line2;
6752 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 }
6754
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756}
6757
6758#ifdef FEAT_BYTEOFF
6759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006760ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 goto_byte(eap->line2);
6763}
6764#endif
6765
6766/*
6767 * ":shell".
6768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006770ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771{
6772 do_shell(NULL, 0);
6773}
6774
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006775#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006777static int drop_busy = FALSE;
6778static int drop_filec;
6779static char_u **drop_filev = NULL;
6780static int drop_split;
6781static void (*drop_callback)(void *);
6782static void *drop_cookie;
6783
6784 static void
6785handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786{
6787 exarg_T ea;
6788 int save_msg_scroll = msg_scroll;
6789
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006790 // Setting the argument list may cause screen updates and being called
6791 // recursively. Avoid that by setting drop_busy.
6792 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006794 // Check whether the current buffer is changed. If so, we will need
6795 // to split the current window or data could be lost.
6796 // We don't need to check if the 'hidden' option is set, as in this
6797 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006798 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
6800 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006801 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 --emsg_off;
6803 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006804 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (win_split(0, 0) == FAIL)
6807 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006808 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006810 // When splitting the window, create a new alist. Otherwise the
6811 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 alist_unlink(curwin->w_alist);
6813 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 }
6815
6816 /*
6817 * Set up the new argument list.
6818 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006819 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820
6821 /*
6822 * Move to the first file.
6823 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006824 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006825 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 ea.cmd = (char_u *)"next";
6827 do_argfile(&ea, 0);
6828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006829 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6830 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 need_start_insertmode = FALSE;
6832
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006833 // Restore msg_scroll, otherwise a following command may cause scrolling
6834 // unexpectedly. The screen will be redrawn by the caller, thus
6835 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006837
6838 if (drop_callback != NULL)
6839 drop_callback(drop_cookie);
6840
6841 drop_filev = NULL;
6842 drop_busy = FALSE;
6843}
6844
6845/*
6846 * Handle a file drop. The code is here because a drop is *nearly* like an
6847 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006848 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006849 * code is very similar to :args and hence needs access to a lot of the static
6850 * functions in this file.
6851 *
6852 * The "filev" list must have been allocated using alloc(), as should each item
6853 * in the list. This function takes over responsibility for freeing the "filev"
6854 * list.
6855 */
6856 void
6857handle_drop(
6858 int filec, // the number of files dropped
6859 char_u **filev, // the list of files dropped
6860 int split, // force splitting the window
6861 void (*callback)(void *), // to be called after setting the argument
6862 // list
6863 void *cookie) // argument for "callback" (allocated)
6864{
6865 // Cannot handle recursive drops, finish the pending one.
6866 if (drop_busy)
6867 {
6868 FreeWild(filec, filev);
6869 vim_free(cookie);
6870 return;
6871 }
6872
6873 // When calling handle_drop() more than once in a row we only use the last
6874 // one.
6875 if (drop_filev != NULL)
6876 {
6877 FreeWild(drop_filec, drop_filev);
6878 vim_free(drop_cookie);
6879 }
6880
6881 drop_filec = filec;
6882 drop_filev = filev;
6883 drop_split = split;
6884 drop_callback = callback;
6885 drop_cookie = cookie;
6886
6887 // Postpone this when:
6888 // - editing the command line
6889 // - not possible to change the current buffer
6890 // - updating the screen
6891 // As it may change buffers and window structures that are in use and cause
6892 // freed memory to be used.
6893 if (text_locked() || curbuf_locked() || updating_screen)
6894 return;
6895
6896 handle_drop_internal();
6897}
6898
6899/*
6900 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6901 * reset: Handle a postponed drop.
6902 */
6903 void
6904handle_any_postponed_drop(void)
6905{
6906 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006907 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006908 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909}
6910#endif
6911
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 * ":preserve".
6914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006916ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006918 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 ml_preserve(curbuf, TRUE);
6920}
6921
6922/*
6923 * ":recover".
6924 */
6925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006926ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006928 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006930 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6931 | CCGD_MULTWIN
6932 | (eap->forceit ? CCGD_FORCEIT : 0)
6933 | CCGD_EXCMD)
6934
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 && (*eap->arg == NUL
6936 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006937 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 recoverymode = FALSE;
6939}
6940
6941/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006942 * Command modifier used in a wrong way. Also for other commands that can't
6943 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 */
6945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006946ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006948 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949}
6950
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006951#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006952
6953// callback function for 'findfunc'
6954static callback_T ffu_cb;
6955
6956 static callback_T *
6957get_findfunc_callback(void)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006958{
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006959 return *curbuf->b_p_ffu != NUL ? &curbuf->b_ffu_cb : &ffu_cb;
6960}
6961
zeertzjq6eda2692024-11-03 09:23:33 +01006962/*
6963 * Call 'findfunc' to obtain a list of file names.
6964 */
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006965 static list_T *
6966call_findfunc(char_u *pat, int cmdcomplete)
6967{
6968 typval_T args[3];
6969 callback_T *cb;
6970 typval_T rettv;
6971 int retval;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006972 sctx_T saved_sctx = current_sctx;
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006973 sctx_T *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006974
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006975 args[0].v_type = VAR_STRING;
6976 args[0].vval.v_string = pat;
6977 args[1].v_type = VAR_BOOL;
6978 args[1].vval.v_number = cmdcomplete;
6979 args[2].v_type = VAR_UNKNOWN;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006980
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006981 // Lock the text to prevent weird things from happening. Also disallow
6982 // switching to another window, it should not be needed and may end up in
6983 // Insert mode in another buffer.
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006984 ++textlock;
6985
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006986 ctx = get_option_sctx("findfunc");
6987 if (ctx != NULL)
6988 current_sctx = *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006989
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006990 cb = get_findfunc_callback();
6991 retval = call_callback(cb, -1, &rettv, 2, args);
6992
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006993 current_sctx = saved_sctx;
6994
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006995 --textlock;
6996
6997 list_T *retlist = NULL;
6998
6999 if (retval == OK)
7000 {
7001 if (rettv.v_type == VAR_LIST)
7002 retlist = list_copy(rettv.vval.v_list, FALSE, FALSE, get_copyID());
7003 else
7004 emsg(_(e_invalid_return_type_from_findfunc));
7005
7006 clear_tv(&rettv);
7007 }
7008
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007009 return retlist;
7010}
7011
7012/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007013 * Find file names matching "pat" using 'findfunc' and return it in "files".
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007014 * Used for expanding the :find, :sfind and :tabfind command argument.
7015 * Returns OK on success and FAIL otherwise.
7016 */
7017 int
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007018expand_findfunc(char_u *pat, char_u ***files, int *numMatches)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007019{
7020 list_T *l;
7021 int len;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007022
7023 *numMatches = 0;
7024 *files = NULL;
7025
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007026 l = call_findfunc(pat, VVAL_TRUE);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007027
7028 if (l == NULL)
7029 return FAIL;
7030
7031 len = list_len(l);
7032 if (len == 0) // empty List
7033 return FAIL;
7034
7035 *files = ALLOC_MULT(char_u *, len);
7036 if (*files == NULL)
7037 return FAIL;
7038
7039 // Copy all the List items
7040 listitem_T *li;
7041 int idx = 0;
7042 FOR_ALL_LIST_ITEMS(l, li)
7043 {
7044 if (li->li_tv.v_type == VAR_STRING)
7045 {
7046 (*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
7047 idx++;
7048 }
7049 }
7050
7051 *numMatches = idx;
7052 list_free(l);
7053
7054 return OK;
7055}
7056
7057/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007058 * Use 'findfunc' to find file 'findarg'. The 'count' argument is used to find
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007059 * the n'th matching file.
7060 */
7061 static char_u *
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007062findfunc_find_file(char_u *findarg, int findarg_len, int count)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007063{
7064 list_T *fname_list;
7065 char_u *ret_fname = NULL;
7066 char_u cc;
7067 int fname_count;
7068
7069 cc = findarg[findarg_len];
7070 findarg[findarg_len] = NUL;
7071
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007072 fname_list = call_findfunc(findarg, VVAL_FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007073 fname_count = list_len(fname_list);
7074
7075 if (fname_count == 0)
7076 semsg(_(e_cant_find_file_str_in_path), findarg);
7077 else
7078 {
7079 if (count > fname_count)
7080 semsg(_(e_no_more_file_str_found_in_path), findarg);
7081 else
7082 {
7083 listitem_T *li = list_find(fname_list, count - 1);
7084 if (li != NULL && li->li_tv.v_type == VAR_STRING)
7085 ret_fname = vim_strsave(li->li_tv.vval.v_string);
7086 }
7087 }
7088
7089 if (fname_list != NULL)
7090 list_free(fname_list);
7091
7092 findarg[findarg_len] = cc;
7093
7094 return ret_fname;
7095}
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007096
7097/*
7098 * Process the 'findfunc' option value.
7099 * Returns NULL on success and an error message on failure.
7100 */
7101 char *
7102did_set_findfunc(optset_T *args UNUSED)
7103{
7104 int retval;
7105
zeertzjq6eda2692024-11-03 09:23:33 +01007106 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007107 // buffer-local option set
7108 retval = option_set_callback_func(curbuf->b_p_ffu, &curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007109 else
7110 {
7111 // global option set
7112 retval = option_set_callback_func(p_ffu, &ffu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01007113 // when using :set, free the local callback
7114 if (!(args->os_flags & OPT_GLOBAL))
7115 free_callback(&curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007116 }
7117
7118 if (retval == FAIL)
7119 return e_invalid_argument;
7120
7121 // If the option value starts with <SID> or s:, then replace that with
7122 // the script identifier.
7123 char_u **varp = (char_u **)args->os_varp;
7124 char_u *name = get_scriptlocal_funcname(*varp);
7125 if (name != NULL)
7126 {
7127 free_string_option(*varp);
7128 *varp = name;
7129 }
7130
7131 return NULL;
7132}
7133
7134# if defined(EXITFREE) || defined(PROTO)
7135 void
7136free_findfunc_option(void)
7137{
7138 free_callback(&ffu_cb);
7139}
7140# endif
7141
7142/*
7143 * Mark the global 'findfunc' callback with "copyID" so that it is not
7144 * garbage collected.
7145 */
7146 int
7147set_ref_in_findfunc(int copyID UNUSED)
7148{
7149 int abort = FALSE;
7150 abort = set_ref_in_callback(&ffu_cb, copyID);
7151 return abort;
7152}
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007153#endif
7154
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155/*
7156 * :sview [+command] file split window with new file, read-only
7157 * :split [[+command] file] split window with current or new file
7158 * :vsplit [[+command] file] split window vertically with current or new file
7159 * :new [[+command] file] split window with no or new file
7160 * :vnew [[+command] file] split vertically window with no or new file
7161 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007162 *
7163 * :tabedit open new Tab page with empty window
7164 * :tabedit [+command] file open new Tab page and edit "file"
7165 * :tabnew [[+command] file] just like :tabedit
7166 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 */
7168 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007169ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007171 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007173#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007174 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02007175 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007176#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02007177 int use_tab = eap->cmdidx == CMD_tabedit
7178 || eap->cmdidx == CMD_tabfind
7179 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007181 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007182 return;
7183
Bram Moolenaar4033c552017-09-16 20:54:51 +02007184#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaar4033c552017-09-16 20:54:51 +02007188#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007189 // A ":split" in the quickfix window works like ":new". Don't want two
7190 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02007191 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
7193 if (eap->cmdidx == CMD_split)
7194 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 if (eap->cmdidx == CMD_vsplit)
7196 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007200 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007202 char_u *file_to_find = NULL;
7203 char *search_ctx = NULL;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007204
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007205 if (*get_findfunc() != NUL)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007206 {
7207#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007208 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007209 eap->addr_count > 0 ? eap->line2 : 1);
7210#endif
7211 }
7212 else
7213 {
7214 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7215 FNAME_MESS, TRUE, curbuf->b_ffname,
7216 &file_to_find, &search_ctx);
7217 vim_free(file_to_find);
7218 vim_findfile_cleanup(search_ctx);
7219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 if (fname == NULL)
7221 goto theend;
7222 eap->arg = fname;
7223 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007224# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007225 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 && eap->cmdidx != CMD_new)
7228 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007229 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007230# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007231 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007232# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007233 au_has_group((char_u *)"FileExplorer"))
7234 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007235 // No browsing supported but we do have the file explorer:
7236 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007237 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007238 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007239 }
7240 else
7241 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007242 fname = do_browse(0, (char_u *)(use_tab
7243 ? _("Edit File in new tab page")
7244 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007246 if (fname == NULL)
7247 goto theend;
7248 eap->arg = fname;
7249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007251 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007254 /*
7255 * Either open new tab page or split the window.
7256 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007257 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007258 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007259 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007260 : eap->addr_count == 0 ? 0
7261 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007262 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007263 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007265 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007266 if (curwin != old_curwin
7267 && win_valid(old_curwin)
7268 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007269 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007270 old_curwin->w_alt_fnum = curbuf->b_fnum;
7271 }
7272 }
7273 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007276 // Reset 'scrollbind' when editing another file, but keep it when
7277 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007278 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007279 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 else
7281 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 do_exedit(eap, old_curwin);
7283 }
7284
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007285# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007286 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289theend:
7290 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007292
7293/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007294 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007295 */
7296 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007297tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007298{
7299 exarg_T ea;
7300
Bram Moolenaara80faa82020-04-12 19:37:17 +02007301 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007302 ea.cmdidx = CMD_tabnew;
7303 ea.cmd = (char_u *)"tabn";
7304 ea.arg = (char_u *)"";
7305 ex_splitview(&ea);
7306}
7307
7308/*
7309 * :tabnext command
7310 */
7311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007312ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007313{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007314 int tab_number;
7315
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007316 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007317 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007318 switch (eap->cmdidx)
7319 {
7320 case CMD_tabfirst:
7321 case CMD_tabrewind:
7322 goto_tabpage(1);
7323 break;
7324 case CMD_tablast:
7325 goto_tabpage(9999);
7326 break;
7327 case CMD_tabprevious:
7328 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007329 if (eap->arg && *eap->arg != NUL)
7330 {
7331 char_u *p = eap->arg;
7332 char_u *p_save = p;
7333
7334 tab_number = getdigits(&p);
7335 if (p == p_save || *p_save == '-' || *p != NUL
7336 || tab_number == 0)
7337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007338 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007339 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007340 return;
7341 }
7342 }
7343 else
7344 {
7345 if (eap->addr_count == 0)
7346 tab_number = 1;
7347 else
7348 {
7349 tab_number = eap->line2;
7350 if (tab_number < 1)
7351 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007352 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007353 return;
7354 }
7355 }
7356 }
7357 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007358 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007359 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007360 tab_number = get_tabpage_arg(eap);
7361 if (eap->errmsg == NULL)
7362 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007363 break;
7364 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007365}
7366
7367/*
7368 * :tabmove command
7369 */
7370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007371ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007372{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007373 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007374
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007375 tab_number = get_tabpage_arg(eap);
7376 if (eap->errmsg == NULL)
7377 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007378}
7379
7380/*
7381 * :tabs command: List tabs and their contents.
7382 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007384ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007385{
7386 tabpage_T *tp;
7387 win_T *wp;
7388 int tabcount = 1;
7389
7390 msg_start();
7391 msg_scroll = TRUE;
7392 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7393 {
7394 msg_putchar('\n');
7395 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007396 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007397 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007398 ui_breakcheck();
7399
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007400 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007401 wp = firstwin;
7402 else
7403 wp = tp->tp_firstwin;
7404 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7405 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007406 msg_putchar('\n');
7407 msg_putchar(wp == curwin ? '>' : ' ');
7408 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007409 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7410 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007411 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007412 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007413 else
7414 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7415 IObuff, IOSIZE, TRUE);
7416 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007417 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007418 ui_breakcheck();
7419 }
7420 }
7421}
7422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423/*
7424 * ":mode": Set screen mode.
7425 * If no argument given, just get the screen size and redraw.
7426 */
7427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007428ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
7430 if (*eap->arg == NUL)
7431 shell_resized();
7432 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007433 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434}
7435
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436/*
7437 * ":resize".
7438 * set, increment or decrement current window height
7439 */
7440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007441ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
7443 int n;
7444 win_T *wp = curwin;
7445
7446 if (eap->addr_count > 0)
7447 {
7448 n = eap->line2;
7449 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7450 ;
7451 }
7452
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007453# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007457 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 {
7459 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007460 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007461 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007463 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
7465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {
7467 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007468 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007469 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007471 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 }
7473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
7475/*
7476 * ":find [+command] <file>" command.
7477 */
7478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007479ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
Colin Kennedy21570352024-03-03 16:16:47 +01007481 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007482 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007483
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007484 char_u *fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007486 char_u *file_to_find = NULL;
7487 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007489 if (*get_findfunc() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 {
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007491#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007492 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007493 eap->addr_count > 0 ? eap->line2 : 1);
7494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 }
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007496 else
7497 {
7498 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7499 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
7500 if (eap->addr_count > 0)
7501 {
7502 // Repeat finding the file "count" times. This matters when it appears
7503 // several times in the path.
7504 count = eap->line2;
7505 while (fname != NULL && --count > 0)
7506 {
7507 vim_free(fname);
7508 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7509 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
7510 }
7511 }
7512 VIM_CLEAR(file_to_find);
7513 vim_findfile_cleanup(search_ctx);
7514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007516 if (fname == NULL)
7517 return;
7518
7519 eap->arg = fname;
7520 do_exedit(eap, NULL);
7521 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522}
7523
7524/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007525 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007526 */
7527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007528ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007529{
7530 regmatch_T regmatch;
7531 char_u *p;
7532
Bram Moolenaar65088802021-03-13 21:07:21 +01007533#ifdef FEAT_EVAL
7534 if (not_in_vim9(eap) == FAIL)
7535 return;
7536#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007537 curwin->w_cursor.lnum = eap->line2;
7538 beginline(BL_SOL | BL_FIX);
7539 if (*eap->arg == '/')
7540 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007541 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007542 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007543 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007544 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007545 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007546 if (regmatch.regprog != NULL)
7547 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007548 // make a copy of the line, when searching for a mark it might be
7549 // flushed
7550 char_u *line = vim_strsave(ml_get_curline());
7551
Bram Moolenaardf177f62005-02-22 08:39:57 +00007552 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007553 if (vim_regexec(&regmatch, line, (colnr_T)0))
7554 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007555 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007556 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007557 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007558 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007559 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007560 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007561 eap->arg += STRLEN(eap->arg);
7562 }
7563 check_cursor();
7564
7565 eap->cmdidx = CMD_visual;
7566 do_exedit(eap, NULL);
7567}
7568
7569/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007570 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 */
7572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007573ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007575 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7576
Colin Kennedy21570352024-03-03 16:16:47 +01007577 // Exclude commands which keep the window's current buffer
7578 if (
7579 eap->cmdidx != CMD_badd
7580 && eap->cmdidx != CMD_balt
7581 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007582 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7583 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007584 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 do_exedit(eap, NULL);
7587}
7588
7589/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007590 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007593do_exedit(
7594 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007595 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007599 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007601 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7602 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007603 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 /*
7605 * ":vi" command ends Ex mode.
7606 */
7607 if (exmode_active && (eap->cmdidx == CMD_visual
7608 || eap->cmdidx == CMD_view))
7609 {
7610 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007611 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007613 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007614 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007615 if (global_busy)
7616 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007617 if (eap->nextcmd != NULL)
7618 {
7619 stuffReadbuff(eap->nextcmd);
7620 eap->nextcmd = NULL;
7621 }
7622
7623 if (exmode_was != EXMODE_VIM)
7624 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007625 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007626 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007627 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007628 no_wait_return = 0;
7629 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007630 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007631 msg_scroll = 0;
7632#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007633 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007634 hold_gui_events = 0;
7635#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007636 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007637 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007638
7639 main_loop(FALSE, TRUE);
7640
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007641 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007642 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007643 no_wait_return = save_nwr;
7644 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007645#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007646 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007647#endif
7648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 }
7652
7653 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007654 || eap->cmdidx == CMD_tabnew
7655 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007656 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007658 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 setpcmark();
7660 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007661 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7662 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007664 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 || *eap->arg != NUL
7666#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007667 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668#endif
7669 )
7670 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007671 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7672 // Only ":edit" or ":script" can bring us here, others are stopped
7673 // earlier.
7674 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007675 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007676
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 n = readonlymode;
7678 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7679 readonlymode = TRUE;
7680 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007681 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7682 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007683 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7684 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7686 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007687 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7689 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7690 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007691 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007693 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007694 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007695 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7696 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007697 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007699 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 if (old_curwin != NULL)
7701 {
7702 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007703 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007705#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007706 cleanup_T cs;
7707
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007708 // Reset the error/interrupt/exception state here so that
7709 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007710 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007711#endif
7712#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007714#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007715 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007716
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007717#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007718 // Restore the error/interrupt/exception state if not
7719 // discarded by a new aborting error, interrupt, or
7720 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007721 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 }
7726 else if (readonlymode && curbuf->b_nwindows == 1)
7727 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007728 // When editing an already visited buffer, 'readonly' won't be set
7729 // but the previous value is kept. With ":view" and ":sview" we
7730 // want the file to be readonly, except when another window is
7731 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 curbuf->b_p_ro = TRUE;
7733 }
7734 readonlymode = n;
7735 }
7736 else
7737 {
7738 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007739 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 if (n != curwin->w_arg_idx_invalid)
7743 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 }
7745
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 /*
7747 * if ":split file" worked, set alternate file name in old window to new
7748 * file
7749 */
7750 if (old_curwin != NULL
7751 && *eap->arg != NUL
7752 && curwin != old_curwin
7753 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007754 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007755 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757
7758 ex_no_reprint = TRUE;
7759}
7760
7761#ifndef FEAT_GUI
7762/*
7763 * ":gui" and ":gvim" when there is no GUI.
7764 */
7765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007766ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007768 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769}
7770#endif
7771
Bram Moolenaar4f974752019-02-17 17:44:42 +01007772#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007774ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 gui_make_tearoff(eap->arg);
7777}
7778#endif
7779
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007780#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7781 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007783ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007785# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7786 if (gui.in_use)
7787 gui_make_popup(eap->arg, eap->forceit);
7788# ifdef FEAT_TERM_POPUP_MENU
7789 else
7790# endif
7791# endif
7792# ifdef FEAT_TERM_POPUP_MENU
7793 pum_make_popup(eap->arg, eap->forceit);
7794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795}
7796#endif
7797
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007799ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007802 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007804 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805}
7806
7807/*
7808 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7809 * offset.
7810 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007813ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007816 win_T *save_curwin = curwin;
7817 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 long topline;
7819 long y;
7820 linenr_T old_linenr = curwin->w_cursor.lnum;
7821
7822 setpcmark();
7823
7824 /*
7825 * determine max topline
7826 */
7827 if (curwin->w_p_scb)
7828 {
7829 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007830 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {
7832 if (wp->w_p_scb && wp->w_buffer)
7833 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007834 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 if (topline > y)
7836 topline = y;
7837 }
7838 }
7839 if (topline < 1)
7840 topline = 1;
7841 }
7842 else
7843 {
7844 topline = 1;
7845 }
7846
7847
7848 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007849 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007851 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {
7853 if (curwin->w_p_scb)
7854 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007855 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 y = topline - curwin->w_topline;
7857 if (y > 0)
7858 scrollup(y, TRUE);
7859 else
7860 scrolldown(-y, TRUE);
7861 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007862 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 }
7866 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007867 curwin = save_curwin;
7868 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (curwin->w_p_scb)
7870 {
7871 did_syncbind = TRUE;
7872 checkpcmark();
7873 if (old_linenr != curwin->w_cursor.lnum)
7874 {
7875 char_u ctrl_o[2];
7876
7877 ctrl_o[0] = Ctrl_O;
7878 ctrl_o[1] = 0;
7879 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7880 }
7881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882}
7883
7884
7885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007886ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007888 int i;
7889 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7890 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007892 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007894 do_bang(1, eap, FALSE, FALSE, TRUE);
7895 return;
7896 }
7897
7898 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900
7901#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007902 if (cmdmod.cmod_flags & CMOD_BROWSE)
7903 {
7904 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007906 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7907 NULL, NULL, NULL, curbuf);
7908 if (browseFile != NULL)
7909 {
7910 i = readfile(browseFile, NULL,
7911 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7912 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 }
7914 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007915 i = OK;
7916 }
7917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007919 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007921 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 return;
7923 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007924 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 }
7926 else
7927 {
7928 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7929 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7930 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007931 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
7933 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007934 if (i != OK)
7935 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007936#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007937 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007939 semsg(_(e_cant_open_file_str), eap->arg);
7940 }
7941 else
7942 {
7943 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007944 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007945 // Delete the empty line that remains. Historically ex does
7946 // this but vi doesn't.
7947 if (eap->line2 == 0)
7948 lnum = curbuf->b_ml.ml_line_count;
7949 else
7950 lnum = 1;
7951 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007952 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007953 ml_delete(lnum);
7954 if (curwin->w_cursor.lnum > 1
7955 && curwin->w_cursor.lnum >= lnum)
7956 --curwin->w_cursor.lnum;
7957 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007958 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007959 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007960 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 }
7962}
7963
Bram Moolenaarea408852005-06-25 22:49:46 +00007964static char_u *prev_dir = NULL;
7965
7966#if defined(EXITFREE) || defined(PROTO)
7967 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007969{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007970 VIM_CLEAR(prev_dir);
7971 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007972}
7973#endif
7974
Bram Moolenaarf4258302013-06-02 18:20:17 +02007975/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007976 * Get the previous directory for the given chdir scope.
7977 */
7978 static char_u *
7979get_prevdir(cdscope_T scope)
7980{
7981 if (scope == CDSCOPE_WINDOW)
7982 return curwin->w_prevdir;
7983 else if (scope == CDSCOPE_TABPAGE)
7984 return curtab->tp_prevdir;
7985 return prev_dir;
7986}
7987
7988/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007989 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007990 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7991 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007992 */
7993 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007994post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007995{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007996 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007997 // Clear tab local directory for both :cd and :tcd
7998 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007999 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008000 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008001 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008002 char_u *pdir = get_prevdir(scope);
8003
8004 // If still in the global directory, need to remember current
8005 // directory as the global directory.
8006 if (globaldir == NULL && pdir != NULL)
8007 globaldir = vim_strsave(pdir);
8008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008009 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02008010 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008011 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008012 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008013 curtab->tp_localdir = vim_strsave(NameBuff);
8014 else
8015 curwin->w_localdir = vim_strsave(NameBuff);
8016 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02008017 }
8018 else
8019 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008020 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01008021 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008022 }
8023
zeertzjq64be6aa2021-11-19 11:59:08 +00008024 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008025 shorten_fnames(TRUE);
8026}
8027
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008028/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008029 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
8030 */
8031 void
8032trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
8033{
8034#ifdef FEAT_EVAL
8035 dict_T *v_event;
8036 save_v_event_T save_v_event;
8037
8038 v_event = get_v_event(&save_v_event);
8039 (void)dict_add_string(v_event, "directory", new_dir);
8040 dict_set_items_ro(v_event);
8041#endif
8042 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
8043#ifdef FEAT_EVAL
8044 restore_v_event(v_event, &save_v_event);
8045#endif
8046}
8047
8048/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008049 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02008050 * chdir() function.
8051 * scope == CDSCOPE_WINDOW: changes the window-local directory
8052 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
8053 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008054 * Returns TRUE if the directory is successfully changed.
8055 */
8056 int
8057changedir_func(
8058 char_u *new_dir,
8059 int forceit,
8060 cdscope_T scope)
8061{
Bram Moolenaar002bc792020-06-05 22:33:42 +02008062 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008063 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008064 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00008065 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008066 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008067
Bram Moolenaar7cc96922020-01-31 22:41:38 +01008068 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008069 return FALSE;
8070
8071 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
8072 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00008073 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008074 return FALSE;
8075 }
8076
8077 // ":cd -": Change to previous directory
8078 if (STRCMP(new_dir, "-") == 0)
8079 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008080 pdir = get_prevdir(scope);
8081 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008082 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008083 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008084 return FALSE;
8085 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02008086 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008087 }
8088
8089 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008090 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02008091 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008092 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02008093 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008094
Bakudankun29f3a452021-12-11 12:28:08 +00008095 // For UNIX ":cd" means: go to home directory.
8096 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008097#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008098 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00008099#else
8100 if (*new_dir == NUL && p_cdh)
8101#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008102 {
8103 // use NameBuff for home directory name
8104# ifdef VMS
8105 char_u *p;
8106
8107 p = mch_getenv((char_u *)"SYS$LOGIN");
8108 if (p == NULL || *p == NUL) // empty is the same as not set
8109 NameBuff[0] = NUL;
8110 else
8111 vim_strncpy(NameBuff, p, MAXPATHL - 1);
8112# else
8113 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8114# endif
8115 new_dir = NameBuff;
8116 }
zeertzjqf38aad82021-12-30 13:45:57 +00008117 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008118 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
8119 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00008120 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008121 if (scope == CDSCOPE_WINDOW)
8122 acmd_fname = (char_u *)"window";
8123 else if (scope == CDSCOPE_TABPAGE)
8124 acmd_fname = (char_u *)"tabpage";
8125 else
8126 acmd_fname = (char_u *)"global";
8127 trigger_DirChangedPre(acmd_fname, new_dir);
8128
8129 if (vim_chdir(new_dir))
8130 {
8131 emsg(_(e_command_failed));
8132 vim_free(pdir);
8133 return FALSE;
8134 }
Richard Doty3d0abad2021-12-29 14:39:08 +00008135 }
zeertzjq73257142022-02-02 13:16:37 +00008136
8137 if (scope == CDSCOPE_WINDOW)
8138 pp = &curwin->w_prevdir;
8139 else if (scope == CDSCOPE_TABPAGE)
8140 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008141 else
zeertzjq73257142022-02-02 13:16:37 +00008142 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008143 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00008144 *pp = pdir;
8145
8146 post_chdir(scope);
8147
8148 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008149 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008150 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00008151 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008152}
Bram Moolenaarea408852005-06-25 22:49:46 +00008153
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008155 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008157 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008158ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01008160 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161
8162 new_dir = eap->arg;
8163#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00008164 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
8165 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008167 ex_pwd(NULL);
8168 return;
8169 }
8170#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008171
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008172 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008173
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008174 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8175 scope = CDSCOPE_WINDOW;
8176 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
8177 scope = CDSCOPE_TABPAGE;
8178
8179 if (changedir_func(new_dir, eap->forceit, scope))
8180 {
8181 // Echo the new current directory if the command was typed.
8182 if (KeyTyped || p_verbose >= 5)
8183 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185}
8186
8187/*
8188 * ":pwd".
8189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008191ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8194 {
8195#ifdef BACKSLASH_IN_FILENAME
8196 slash_adjust(NameBuff);
8197#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02008198 if (p_verbose > 0)
8199 {
8200 char *context = "global";
8201
Bram Moolenaar05268152021-11-18 18:53:45 +00008202 if (last_chdir_reason != NULL)
8203 context = last_chdir_reason;
8204 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02008205 context = "window";
8206 else if (curtab->tp_localdir != NULL)
8207 context = "tabpage";
8208 smsg("[%s] %s", context, (char *)NameBuff);
8209 }
8210 else
8211 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 }
8213 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00008214 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215}
8216
8217/*
8218 * ":=".
8219 */
8220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008221ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008223 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008224 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225}
8226
8227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008228ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008230 int n;
8231 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232
8233 if (cursor_valid())
8234 {
8235 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8236 if (n >= 0)
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02008237 windgoto(n, curwin->w_wincol + curwin->w_wcol + TPL_LCOL(curwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008239
8240 len = eap->line2;
8241 switch (*eap->arg)
8242 {
8243 case 'm': break;
8244 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008245 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008246 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008247
8248 // Hide the cursor if invoked with !
8249 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250}
8251
8252/*
8253 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008254 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 */
8256 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008257do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
Bram Moolenaar52953192019-08-16 10:27:13 +02008259 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008260 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008261# ifdef ELAPSED_FUNC
8262 elapsed_T start_tv;
8263
8264 // Remember at what time we started, so that we know how much longer we
8265 // should wait after waiting for a bit.
8266 ELAPSED_INIT(start_tv);
8267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008269 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008270 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008271 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008272 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008273
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008274 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008275 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008277 wait_now = msec - done > 1000L ? 1000L : msec - done;
8278#ifdef FEAT_TIMERS
8279 {
8280 long due_time = check_due_timer();
8281
8282 if (due_time > 0 && due_time < wait_now)
8283 wait_now = due_time;
8284 }
8285#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008286#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008287 if (has_any_channel() && wait_now > 20L)
8288 wait_now = 20L;
8289#endif
8290#ifdef FEAT_SOUND
8291 if (has_any_sound_callback() && wait_now > 20L)
8292 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008293#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008294 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008295
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008296#ifdef FEAT_JOB_CHANNEL
8297 if (has_any_channel())
8298 ui_breakcheck_force(TRUE);
8299 else
8300#endif
8301 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008302#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008303 // Process the netbeans and clientserver messages that may have been
8304 // received in the call to ui_breakcheck() when the GUI is in use. This
8305 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008306 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008307#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008308
8309# ifdef ELAPSED_FUNC
8310 // actual time passed
8311 done = ELAPSED_FUNC(start_tv);
8312# else
8313 // guestimate time passed (will actually be more)
8314 done += wait_now;
8315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008317
8318 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8319 // input buffer, otherwise a following call to input() fails.
8320 if (got_int)
8321 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008322
8323 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008324 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325}
8326
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327/*
8328 * ":winsize" command (obsolete).
8329 */
8330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008331ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
8333 int w, h;
8334 char_u *arg = eap->arg;
8335 char_u *p;
8336
Keith Thompson184f71c2024-01-04 21:19:04 +01008337 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008338 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008339 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008340 return;
8341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 w = getdigits(&arg);
8343 arg = skipwhite(arg);
8344 p = arg;
8345 h = getdigits(&arg);
8346 if (*p != NUL && *arg == NUL)
8347 set_shellsize(w, h, TRUE);
8348 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008349 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350}
8351
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008353ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354{
8355 int xchar = NUL;
8356 char_u *p;
8357
8358 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008360 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 if (eap->arg[1] == NUL)
8362 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008363 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 return;
8365 }
8366 xchar = eap->arg[1];
8367 p = eap->arg + 2;
8368 }
8369 else
8370 p = eap->arg + 1;
8371
Bram Moolenaar63b91732021-08-05 20:40:03 +02008372 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008374 if (*p != NUL && *p != (
8375#ifdef FEAT_EVAL
8376 in_vim9script() ? '#' :
8377#endif
8378 '"')
8379 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008380 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008381 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008383 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008384 postponed_split_flags = cmdmod.cmod_split;
8385 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8387 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008388 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 }
8390}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
Bram Moolenaar843ee412004-06-30 16:16:41 +00008392#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393/*
8394 * ":winpos".
8395 */
8396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008397ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
8399 int x, y;
8400 char_u *arg = eap->arg;
8401 char_u *p;
8402
8403 if (*arg == NUL)
8404 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008405# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008406# ifdef VIMDLL
8407 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8408 mch_get_winpos(&x, &y) != FAIL)
8409# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008411# else
8412 if (mch_get_winpos(&x, &y) != FAIL)
8413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
8415 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008416 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 }
8418 else
8419# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008420 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 }
8422 else
8423 {
8424 x = getdigits(&arg);
8425 arg = skipwhite(arg);
8426 p = arg;
8427 y = getdigits(&arg);
8428 if (*p == NUL || *arg != NUL)
8429 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008430 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 return;
8432 }
8433# ifdef FEAT_GUI
8434 if (gui.in_use)
8435 gui_mch_set_winpos(x, y);
8436 else if (gui.starting)
8437 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008438 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 gui_win_x = x;
8440 gui_win_y = y;
8441 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008442# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 else
8444# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008445# endif
8446# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008447 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448# endif
8449# ifdef HAVE_TGETENT
8450 if (*T_CWP)
8451 term_set_winpos(x, y);
8452# endif
8453 }
8454}
8455#endif
8456
8457/*
8458 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8459 */
8460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008461ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
8463 oparg_T oa;
8464
8465 clear_oparg(&oa);
8466 oa.regname = eap->regname;
8467 oa.start.lnum = eap->line1;
8468 oa.end.lnum = eap->line2;
8469 oa.line_count = eap->line2 - eap->line1 + 1;
8470 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008472 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
8474 setpcmark();
8475 curwin->w_cursor.lnum = eap->line1;
8476 beginline(BL_SOL | BL_FIX);
8477 }
8478
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008479 if (VIsual_active)
8480 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 switch (eap->cmdidx)
8483 {
8484 case CMD_delete:
8485 oa.op_type = OP_DELETE;
8486 op_delete(&oa);
8487 break;
8488
8489 case CMD_yank:
8490 oa.op_type = OP_YANK;
8491 (void)op_yank(&oa, FALSE, TRUE);
8492 break;
8493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008494 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008495 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008497 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8498#else
8499 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008501 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 oa.op_type = OP_RSHIFT;
8503 else
8504 oa.op_type = OP_LSHIFT;
8505 op_shift(&oa, FALSE, eap->amount);
8506 break;
8507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008509 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510}
8511
8512/*
8513 * ":put".
8514 */
8515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008516ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008518 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 if (eap->line2 == 0)
8520 {
8521 eap->line2 = 1;
8522 eap->forceit = TRUE;
8523 }
8524 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008525 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008526 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008527 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528}
8529
8530/*
64-bitmane08f10a2025-03-18 22:14:34 +01008531 * ":iput".
8532 */
8533 static void
8534ex_iput(exarg_T *eap)
8535{
8536 // ":0iput" works like ":1iput!".
8537 if (eap->line2 == 0)
8538 {
8539 eap->line2 = 1;
8540 eap->forceit = TRUE;
8541 }
8542 curwin->w_cursor.lnum = eap->line2;
8543 check_cursor_col();
8544 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
8545 PUT_LINE|PUT_CURSLINE
8546 |PUT_FIXINDENT);
8547}
8548
8549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 * Handle ":copy" and ":move".
8551 */
8552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008553ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
8555 long n;
8556
Bram Moolenaar491799b2020-08-01 19:23:43 +02008557#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008558 if (not_in_vim9(eap) == FAIL)
8559 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008560#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008561 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008562 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 {
8564 eap->nextcmd = NULL;
8565 return;
8566 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008567 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568
8569 /*
8570 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8571 */
8572 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8573 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008574 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 return;
8576 }
8577
8578 if (eap->cmdidx == CMD_move)
8579 {
8580 if (do_move(eap->line1, eap->line2, n) == FAIL)
8581 return;
8582 }
8583 else
8584 ex_copy(eap->line1, eap->line2, n);
8585 u_clearline();
8586 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008587 ex_may_print(eap);
8588}
8589
8590/*
8591 * Print the current line if flags were given to the Ex command.
8592 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008593 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008594ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008595{
8596 if (eap->flags != 0)
8597 {
8598 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8599 (eap->flags & EXFLAG_LIST));
8600 ex_no_reprint = TRUE;
8601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602}
8603
8604/*
8605 * ":smagic" and ":snomagic".
8606 */
8607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008608ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008610 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008612 magic_overruled = eap->cmdidx == CMD_smagic
8613 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008614 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008615 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616}
8617
8618/*
8619 * ":join".
8620 */
8621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008622ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623{
8624 curwin->w_cursor.lnum = eap->line1;
8625 if (eap->line1 == eap->line2)
8626 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008627 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 return;
8629 if (eap->line2 == curbuf->b_ml.ml_line_count)
8630 {
8631 beep_flush();
8632 return;
8633 }
8634 ++eap->line2;
8635 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008636 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008638 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639}
8640
8641/*
8642 * ":[addr]@r" or ":[addr]*r": execute register
8643 */
8644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008645ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
8647 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008648 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649
8650 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008651 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652
8653#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008654 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655#endif
8656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008657 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 c = *eap->arg;
8659 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8660 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008661 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008662 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8663 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008666 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008669 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008671 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008673 /*
8674 * Execute from the typeahead buffer.
8675 * Continue until the stuff buffer is empty and all added characters
8676 * have been consumed.
8677 */
8678 while (!stuff_empty() || typebuf.tb_len > prev_len)
8679 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8680
8681 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682}
8683
8684/*
8685 * ":!".
8686 */
8687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008688ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689{
8690 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8691}
8692
8693/*
8694 * ":undo".
8695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008697ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008699 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008700 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008701 else
8702 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703}
8704
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008705#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008707ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008708{
8709 char_u hash[UNDO_HASH_SIZE];
8710
8711 u_compute_hash(hash);
8712 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8713}
8714
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008716ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008717{
8718 char_u hash[UNDO_HASH_SIZE];
8719
8720 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008721 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008722}
8723#endif
8724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725/*
8726 * ":redo".
8727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008729ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 u_redo(1);
8732}
8733
8734/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008735 * ":earlier" and ":later".
8736 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008738ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008739{
8740 long count = 0;
8741 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008742 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008743 char_u *p = eap->arg;
8744
8745 if (*p == NUL)
8746 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008747 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008748 {
8749 count = getdigits(&p);
8750 switch (*p)
8751 {
8752 case 's': ++p; sec = TRUE; break;
8753 case 'm': ++p; sec = TRUE; count *= 60; break;
8754 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008755 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8756 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008757 }
8758 }
8759
8760 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008761 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008762 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008763 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8764 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008765}
8766
8767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 * ":redir": start/stop redirection.
8769 */
8770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008771ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
8773 char *mode;
8774 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008775 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776
Bram Moolenaarba768492016-07-08 15:32:54 +02008777#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008778 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008779 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008780 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008781 return;
8782 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008783#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 if (STRICMP(eap->arg, "END") == 0)
8786 close_redir();
8787 else
8788 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008789 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008791 ++arg;
8792 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008794 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 mode = "a";
8796 }
8797 else
8798 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008799 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800
8801 close_redir();
8802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008803 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008804 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 if (fname == NULL)
8806 return;
8807#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008808 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 {
8810 char_u *browseFile;
8811
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008812 browseFile = do_browse(BROWSE_SAVE,
8813 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008814 fname, NULL, NULL,
8815 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008817 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 vim_free(fname);
8819 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008820 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 }
8822#endif
8823
8824 redir_fd = open_exfile(fname, eap->forceit, mode);
8825 vim_free(fname);
8826 }
8827#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008828 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008830 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008832 ++arg;
8833 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008835 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008836 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008838 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008840 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008841 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008842 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008843 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008845 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008846 if (*arg == '>')
8847 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008848 // Make register empty when not using @A-@Z and the
8849 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008850 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008851 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008853 }
8854 if (*arg != NUL)
8855 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008856 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008857 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008858 }
8859 }
8860 else if (*arg == '=' && arg[1] == '>')
8861 {
8862 int append;
8863
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008864 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008865 close_redir();
8866 arg += 2;
8867
8868 if (*arg == '>')
8869 {
8870 ++arg;
8871 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 }
8873 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008874 append = FALSE;
8875
8876 if (var_redir_start(skipwhite(arg), append) == OK)
8877 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
8879#endif
8880
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008881 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008884 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008887 // Make sure redirection is not off. Can happen for cmdline completion
8888 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008889 if (redir_fd != NULL
8890#ifdef FEAT_EVAL
8891 || redir_reg || redir_vname
8892#endif
8893 )
8894 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895}
8896
8897/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008898 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008900 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008901ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008903 redraw_cmd(eap->forceit);
8904}
8905
8906/*
8907 * ":redraw": force redraw, with clear if "clear" is TRUE.
8908 */
8909 void
8910redraw_cmd(int clear)
8911{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008912 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008914
8915 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008917
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008918 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008920 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 if (need_maketitle)
8922 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008923#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8924# ifdef VIMDLL
8925 if (!gui.in_use)
8926# endif
8927 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008928#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008929 RedrawingDisabled = save_RedrawingDisabled;
8930 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931
Bram Moolenaar5017c662022-04-07 18:06:08 +01008932 // After drawing the statusline screen_attr may still be set.
8933 screen_stop_highlight();
8934
Bram Moolenaara2a89732022-08-31 14:46:18 +01008935 // Reset msg_didout, so that a message that's there is overwritten.
8936 msg_didout = FALSE;
8937 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008939 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008940 need_wait_return = FALSE;
8941
Bram Moolenaara653e532022-04-19 11:38:24 +01008942 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008943 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008944 redrawcmdline();
8945
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 out_flush();
8947}
8948
8949/*
8950 * ":redrawstatus": force redraw of status line(s)
8951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008953ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 if (eap->forceit)
8956 status_redraw_all();
8957 else
8958 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008959 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008960 return; // redraw later
8961
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008962 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008963 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008964
8965 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008966 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008967
zeertzjq320d9102022-09-20 17:12:13 +01008968 if (State & MODE_CMDLINE)
8969 redraw_statuslines();
8970 else
8971 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008972 RedrawingDisabled = save_RedrawingDisabled;
8973 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975}
8976
Bram Moolenaare12bab32019-01-08 22:02:56 +01008977/*
8978 * ":redrawtabline": force redraw of the tabline
8979 */
8980 static void
8981ex_redrawtabline(exarg_T *eap UNUSED)
8982{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008983 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008984 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008985
8986 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008987 p_lz = FALSE;
8988
8989 draw_tabline();
8990
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008991 RedrawingDisabled = save_RedrawingDisabled;
8992 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008993 out_flush();
8994}
8995
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02008996/*
8997 * ":redrawtabpanel": force redraw of the tabpanel
8998 */
8999 static void
9000ex_redrawtabpanel(exarg_T *eap UNUSED)
9001{
9002 int save_RedrawingDisabled = RedrawingDisabled;
9003 RedrawingDisabled = 0;
9004
9005 int save_p_lz = p_lz;
9006 p_lz = FALSE;
9007
9008#if defined(FEAT_TABPANEL)
9009 draw_tabpanel();
9010#else
9011 emsg(_(e_cannot_not_support_redrawtabpanel));
9012#endif
9013
9014 RedrawingDisabled = save_RedrawingDisabled;
9015 p_lz = save_p_lz;
9016 out_flush();
9017}
9018
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009020close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
9022 if (redir_fd != NULL)
9023 {
9024 fclose(redir_fd);
9025 redir_fd = NULL;
9026 }
9027#ifdef FEAT_EVAL
9028 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009029 if (redir_vname)
9030 {
9031 var_redir_stop();
9032 redir_vname = 0;
9033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034#endif
9035}
9036
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02009037#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009038 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02009039vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009040{
9041 if (vim_mkdir(name, prot) != 0)
9042 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00009043 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009044 return FAIL;
9045 }
9046 return OK;
9047}
9048#endif
9049
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050/*
9051 * Open a file for writing for an Ex command, with some checks.
9052 * Return file descriptor, or NULL on failure.
9053 */
9054 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009055open_exfile(
9056 char_u *fname,
9057 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009058 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 FILE *fd;
9061
9062#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009063 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 if (mch_isdir(fname))
9065 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01009066 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 return NULL;
9068 }
9069#endif
9070 if (!forceit && *mode != 'a' && vim_fexists(fname))
9071 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00009072 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 return NULL;
9074 }
9075
9076 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00009077 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078
9079 return fd;
9080}
9081
9082/*
9083 * ":mark" and ":k".
9084 */
9085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009086ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087{
9088 pos_T pos;
9089
Bram Moolenaar10b94212021-02-19 21:42:57 +01009090#ifdef FEAT_EVAL
9091 if (not_in_vim9(eap) == FAIL)
9092 return;
9093#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009094 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009096 emsg(_(e_argument_required));
9097 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009099
9100 if (eap->arg[1] != NUL) // more than one character?
9101 {
9102 semsg(_(e_trailing_characters_str), eap->arg);
9103 return;
9104 }
9105
9106 pos = curwin->w_cursor; // save curwin->w_cursor
9107 curwin->w_cursor.lnum = eap->line2;
9108 beginline(BL_WHITE | BL_FIX);
9109 if (setmark(*eap->arg) == FAIL) // set mark
9110 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
9111 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112}
9113
9114/*
9115 * Update w_topline, w_leftcol and the cursor position.
9116 */
9117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009118update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009120 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 update_topline();
9122 if (!curwin->w_p_wrap)
9123 validate_cursor();
9124 update_curswant();
9125}
9126
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009128 * Save the current State and go to Normal mode.
9129 * Return TRUE if the typeahead could be saved.
9130 */
9131 int
9132save_current_state(save_state_T *sst)
9133{
9134 sst->save_msg_scroll = msg_scroll;
9135 sst->save_restart_edit = restart_edit;
9136 sst->save_msg_didout = msg_didout;
9137 sst->save_State = State;
9138 sst->save_insertmode = p_im;
9139 sst->save_finish_op = finish_op;
9140 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009141 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009142 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009143
Bram Moolenaar4324d872020-12-01 20:12:24 +01009144 msg_scroll = FALSE; // no msg scrolling in Normal mode
9145 restart_edit = 0; // don't go to Insert mode
9146 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01009147
Bram Moolenaara452b802020-12-01 21:47:59 +01009148 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01009149 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009150
9151 /*
9152 * Save the current typeahead. This is required to allow using ":normal"
9153 * from an event handler and makes sure we don't hang when the argument
9154 * ends with half a command.
9155 */
9156 save_typeahead(&sst->tabuf);
9157 return sst->tabuf.typebuf_valid;
9158}
9159
9160 void
9161restore_current_state(save_state_T *sst)
9162{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009163 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02009164 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009165
9166 msg_scroll = sst->save_msg_scroll;
9167 restart_edit = sst->save_restart_edit;
9168 p_im = sst->save_insertmode;
9169 finish_op = sst->save_finish_op;
9170 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009171 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009172 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009173 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01009174 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009175
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009176 // Restore the state (needed when called from a function executed for
9177 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009178 State = sst->save_State;
9179#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009180 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009181#endif
9182}
9183
9184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 * ":normal[!] {commands}": Execute normal mode commands.
9186 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009187 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009188ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009190 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 char_u *arg = NULL;
9192 int l;
9193 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009195 if (ex_normal_lock > 0)
9196 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00009197 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009198 return;
9199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 if (ex_normal_busy >= p_mmd)
9201 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009202 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 return;
9204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 /*
9207 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9208 * this for the K_SPECIAL leading byte, otherwise special keys will not
9209 * work.
9210 */
9211 if (has_mbyte)
9212 {
9213 int len = 0;
9214
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009215 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 for (p = eap->arg; *p != NUL; ++p)
9217 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009218#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009219 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01009221#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009222 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009223 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009224#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 )
9228 len += 2;
9229 }
9230 if (len > 0)
9231 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009232 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 if (arg != NULL)
9234 {
9235 len = 0;
9236 for (p = eap->arg; *p != NUL; ++p)
9237 {
9238 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01009239#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 if (*p == CSI)
9241 {
9242 arg[len++] = KS_EXTRA;
9243 arg[len++] = (int)KE_CSI;
9244 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009245#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009246 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 {
9248 arg[len++] = *++p;
9249 if (*p == K_SPECIAL)
9250 {
9251 arg[len++] = KS_SPECIAL;
9252 arg[len++] = KE_FILLER;
9253 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009254#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 else if (*p == CSI)
9256 {
9257 arg[len++] = KS_EXTRA;
9258 arg[len++] = (int)KE_CSI;
9259 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 }
9262 arg[len] = NUL;
9263 }
9264 }
9265 }
9266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009268 ++ex_normal_busy;
9269 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 {
9271 /*
9272 * Repeat the :normal command for each line in the range. When no
9273 * range given, execute it just once, without positioning the cursor
9274 * first.
9275 */
9276 do
9277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 if (eap->addr_count != 0)
9279 {
9280 curwin->w_cursor.lnum = eap->line1++;
9281 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009282 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 }
9284
Bram Moolenaar13505972019-01-24 15:04:48 +01009285 exec_normal_cmd(arg != NULL
9286 ? arg
9287 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 }
9289 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9290 }
9291
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009292 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 update_topline_cursor();
9294
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009295 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009297 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009298#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009299 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009300#endif
9301
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303}
9304
9305/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009306 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 */
9308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009309ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009311 if (eap->forceit)
9312 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009313 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009314 if (!curwin->w_cursor.lnum)
9315 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009316 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009317 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009318#ifdef FEAT_TERMINAL
9319 // Ignore this when running in an active terminal.
9320 if (term_job_running(curbuf->b_term))
9321 return;
9322#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009323
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009324 // Ignore the command when already in Insert mode. Inserting an
9325 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009326 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009327 return;
9328
Bram Moolenaar64969662005-12-14 21:59:55 +00009329 if (eap->cmdidx == CMD_startinsert)
9330 restart_edit = 'a';
9331 else if (eap->cmdidx == CMD_startreplace)
9332 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009334 restart_edit = 'V';
9335
9336 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009338 if (eap->cmdidx == CMD_startinsert)
9339 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009340 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009342
9343 if (VIsual_active)
9344 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345}
9346
9347/*
9348 * ":stopinsert"
9349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009351ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
9353 restart_edit = 0;
9354 stop_insert_mode = TRUE;
Christian Brabandtcf665cc2025-04-12 18:09:28 +02009355#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
9356 // when called from remote_expr in insert mode, make sure insert mode is
9357 // ended by adding K_NOP to the typeahead buffer
9358 if (vgetc_busy)
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02009359 ins_char_typebuf(K_NOP, 0);
Christian Brabandtcf665cc2025-04-12 18:09:28 +02009360#endif
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009361 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009364/*
9365 * Execute normal mode command "cmd".
9366 * "remap" can be REMAP_NONE or REMAP_YES.
9367 */
9368 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009369exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009370{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009371 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009372 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009373 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009374}
Bram Moolenaar25281632016-01-21 23:32:32 +01009375
Bram Moolenaar25281632016-01-21 23:32:32 +01009376/*
9377 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009378 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009379 */
9380 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009381exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009382{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009383 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009384 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009385
Bram Moolenaar905dd902019-04-07 14:21:47 +02009386 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9387 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009388 clear_oparg(&oa);
9389 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009390 while ((!stuff_empty()
9391 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009392 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009393 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009394 {
9395 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009396#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009397 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009398 && oa.op_type == OP_NOP && oa.regname == NUL
9399 && !VIsual_active)
9400 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009401 // If terminal_loop() returns OK we got a key that is handled
9402 // in Normal model. With FAIL we first need to position the
9403 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009404 if (terminal_loop(TRUE) == OK)
9405 normal_cmd(&oa, TRUE);
9406 }
9407 else
9408#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009409 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009410 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009411 }
9412}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009413
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414#ifdef FEAT_FIND_ID
9415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009416ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417{
9418 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9419 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009420 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421}
9422
Bram Moolenaar4033c552017-09-16 20:54:51 +02009423#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424/*
9425 * ":psearch"
9426 */
9427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009428ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429{
9430 g_do_tagpreview = p_pvh;
9431 ex_findpat(eap);
9432 g_do_tagpreview = 0;
9433}
9434#endif
9435
9436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009437ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 int whole = TRUE;
9440 long n;
9441 char_u *p;
9442 int action;
9443
9444 switch (cmdnames[eap->cmdidx].cmd_name[2])
9445 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009446 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9448 action = ACTION_GOTO;
9449 else
9450 action = ACTION_SHOW;
9451 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009452 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 action = ACTION_SHOW_ALL;
9454 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009455 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 action = ACTION_GOTO;
9457 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009458 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 action = ACTION_SPLIT;
9460 break;
9461 }
9462
9463 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009464 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 {
9466 n = getdigits(&eap->arg);
9467 eap->arg = skipwhite(eap->arg);
9468 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009469 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 {
9471 whole = FALSE;
9472 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009473 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 if (*p)
9475 {
9476 *p++ = NUL;
9477 p = skipwhite(p);
9478
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009479 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009480 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009481 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009483 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 }
9485 }
9486 if (!eap->skip)
9487 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9488 whole, !eap->forceit,
9489 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009490 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491}
9492#endif
9493
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494
Bram Moolenaar4033c552017-09-16 20:54:51 +02009495#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496/*
9497 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9498 */
9499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009500ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009502 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9504}
9505
9506/*
9507 * ":pedit"
9508 */
9509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009510ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
9512 win_T *curwin_save = curwin;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009513 prepare_preview_window();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009515 // Edit the file.
9516 do_exedit(eap, NULL);
9517
9518 back_to_current_window(curwin_save);
9519}
9520
9521/*
9522 * ":pbuffer"
9523 */
9524 static void
9525ex_pbuffer(exarg_T *eap)
9526{
9527 win_T *curwin_save = curwin;
9528 prepare_preview_window();
9529
9530 // Go to the buffer.
9531 do_exbuffer(eap);
9532
9533 back_to_current_window(curwin_save);
9534}
9535
9536 static void
9537prepare_preview_window(void)
9538{
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009539 if (ERROR_IF_ANY_POPUP_WINDOW)
9540 return;
9541
Bram Moolenaar026587b2019-08-17 15:08:00 +02009542 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009544 prepare_tagpreview(TRUE, TRUE, FALSE);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009545}
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009546
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009547 static void
9548back_to_current_window(win_T *curwin_save)
9549{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 if (curwin != curwin_save && win_valid(curwin_save))
9551 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009552 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009554 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 win_enter(curwin_save, TRUE);
9556 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009557# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009558 else if (WIN_IS_POPUP(curwin))
9559 {
9560 // can't keep focus in popup window
9561 win_enter(firstwin, TRUE);
9562 }
9563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 g_do_tagpreview = 0;
9565}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567
9568/*
9569 * ":stag", ":stselect" and ":stjump".
9570 */
9571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009572ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
9574 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009575 postponed_split_flags = cmdmod.cmod_split;
9576 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9578 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009579 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581
9582/*
9583 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9584 */
9585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009586ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9589}
9590
9591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009592ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594 int cmd;
9595
9596 switch (name[1])
9597 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009598 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009600 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009602 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009604 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009606 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009608 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009610 case 'f': // ":tfirst"
9611 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009613 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009615 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009617 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009619 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 return;
9621 }
9622#endif
9623 cmd = DT_TAG;
9624 break;
9625 }
9626
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009627 if (name[0] == 'l')
9628 {
9629#ifndef FEAT_QUICKFIX
9630 ex_ni(eap);
9631 return;
9632#else
9633 cmd = DT_LTAG;
9634#endif
9635 }
9636
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9638 eap->forceit, TRUE);
9639}
9640
John Marriotted908f72024-04-18 22:46:56 +02009641enum {
9642 SPEC_PERC = 0,
9643 SPEC_HASH,
9644 SPEC_CWORD, // cursor word
9645 SPEC_CCWORD, // cursor WORD
9646 SPEC_CEXPR, // expr under cursor
9647 SPEC_CFILE, // cursor path name
9648 SPEC_SFILE, // ":so" file name
9649 SPEC_SLNUM, // ":so" file line number
9650 SPEC_STACK, // call stack
9651 SPEC_SCRIPT, // script file name
9652 SPEC_AFILE, // autocommand file name
9653 SPEC_ABUF, // autocommand buffer number
9654 SPEC_AMATCH, // autocommand match name
9655 SPEC_SFLNUM, // script file line number
9656 SPEC_SID // script ID: <SNR>123_
9657#ifdef FEAT_CLIENTSERVER
9658 , SPEC_CLIENT
9659#endif
9660};
9661
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009663 * Check "str" for starting with a special cmdline variable.
9664 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9665 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9666 */
9667 int
Mike Williams51024bb2024-05-30 07:46:30 +02009668find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009669{
John Marriotted908f72024-04-18 22:46:56 +02009670 // must be sorted by the 'value' field because it is used by bsearch()!
9671 static keyvalue_T spec_str_tab[] = {
9672 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9673 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9674 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9675 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9676 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9677 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9678 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009679#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009680 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009681#endif
John Marriotted908f72024-04-18 22:46:56 +02009682 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9683 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9684 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9685 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9686 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9687 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009688 };
John Marriotted908f72024-04-18 22:46:56 +02009689 keyvalue_T target;
9690 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009691
John Marriotted908f72024-04-18 22:46:56 +02009692 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009693 {
John Marriotted908f72024-04-18 22:46:56 +02009694 case '%':
9695 *usedlen = 1;
9696 return SPEC_PERC;
9697
9698 case '#':
9699 *usedlen = 1;
9700 return SPEC_HASH;
9701
9702 case '<':
9703 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01009704 target.value.string = src + 1; // skip over '<'
9705 target.value.length = 0; // not used, see cmp_keyvalue_value_n()
John Marriotted908f72024-04-18 22:46:56 +02009706
John Marriott8d4477e2024-11-02 15:59:01 +01009707 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab,
9708 ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]),
9709 cmp_keyvalue_value_n);
John Marriotted908f72024-04-18 22:46:56 +02009710 if (entry == NULL)
9711 return -1;
9712
John Marriott8d4477e2024-11-02 15:59:01 +01009713 *usedlen = entry->value.length + 1;
John Marriotted908f72024-04-18 22:46:56 +02009714 return entry->key;
9715
9716 default:
9717 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009718 }
John Marriotted908f72024-04-18 22:46:56 +02009719
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009720 return -1;
9721}
9722
9723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 * Evaluate cmdline variables.
9725 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009726 * change "%" to curbuf->b_ffname
9727 * "#" to curwin->w_alt_fnum
9728 * "%%" to curwin->w_alt_fnum in Vim9 script
9729 * "<cword>" to word under the cursor
9730 * "<cWORD>" to WORD under the cursor
9731 * "<cexpr>" to C-expression under the cursor
9732 * "<cfile>" to path name under the cursor
9733 * "<sfile>" to sourced file name
9734 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009735 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009736 * "<slnum>" to sourced file line number
9737 * "<afile>" to file name for autocommand
9738 * "<abuf>" to buffer number for autocommand
9739 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 *
9741 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9742 * "" for error without a message) and NULL is returned.
9743 * Returns an allocated string if a valid match was found.
9744 * Returns NULL if no match was found. "usedlen" then still contains the
9745 * number of characters to skip.
9746 */
9747 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009748eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009749 char_u *src, // pointer into commandline
9750 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009751 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009752 linenr_T *lnump, // line number for :e command, or NULL
9753 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009754 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009755 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009756 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757{
9758 int i;
9759 char_u *s;
9760 char_u *result;
9761 char_u *resultbuf = NULL;
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +01009762 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009764 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009766 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769
9770 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009771 if (escaped != NULL)
9772 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773
9774 /*
9775 * Check if there is something to do.
9776 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009777 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009778 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 {
9780 *usedlen = 1;
9781 return NULL;
9782 }
9783
9784 /*
9785 * Skip when preceded with a backslash "\%" and "\#".
9786 * Note: In "\\%" the % is also not recognized!
9787 */
9788 if (src > srcstart && src[-1] == '\\')
9789 {
9790 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009791 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 return NULL;
9793 }
9794
9795 /*
9796 * word or WORD under cursor
9797 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009798 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9799 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009801 resultlen = find_ident_under_cursor(&result,
9802 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9803 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9804 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 if (resultlen == 0)
9806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009807 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 return NULL;
9809 }
9810 }
9811
9812 /*
9813 * '#': Alternate file name
9814 * '%': Current file name
9815 * File name under the cursor
9816 * File name for autocommand
9817 * and following modifiers
9818 */
9819 else
9820 {
Mike Williams51024bb2024-05-30 07:46:30 +02009821 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009822
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 switch (spec_idx)
9824 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009825 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009826#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009827 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009830 // '%': current file
9831 if (curbuf->b_fname == NULL)
9832 {
9833 result = (char_u *)"";
9834 valid = 0; // Must have ":p:h" to be valid
9835 }
9836 else
9837 {
9838 result = curbuf->b_fname;
9839 tilde_file = STRCMP(result, "~") == 0;
9840 }
9841 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009843#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009844 // "%%" alternate file
9845 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009846#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009847 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009848 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009849 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009851 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 result = arg_all();
9853 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009854 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009855 if (escaped != NULL)
9856 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 break;
9859 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009860 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009861 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009862 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009864 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009865 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009866 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009867 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009869 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009871 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009873 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009874 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009875 return NULL;
9876 }
9877#ifdef FEAT_EVAL
9878 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9879 (long)i);
9880 if (result == NULL)
9881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009882 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009883 return NULL;
9884 }
9885#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009886 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 }
9890 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009891 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009892 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9893 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009894 buf = buflist_findnr(i);
9895 if (buf == NULL)
9896 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009897 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009898 return NULL;
9899 }
9900 if (lnump != NULL)
9901 *lnump = ECMD_LAST;
9902 if (buf->b_fname == NULL)
9903 {
9904 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009905 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009906 }
9907 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009908 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009909 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009910 tilde_file = STRCMP(result, "~") == 0;
9911 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 break;
9914
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009915 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009916 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 if (result == NULL)
9918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009919 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 return NULL;
9921 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009922 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009925 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009927 if (result != NULL && !autocmd_fname_full)
9928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009929 // Still need to turn the fname into a full path. It is
9930 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009931 autocmd_fname_full = TRUE;
9932 result = FullName_save(autocmd_fname, FALSE);
9933 vim_free(autocmd_fname);
9934 autocmd_fname = result;
9935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 if (result == NULL)
9937 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009938 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 return NULL;
9940 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009941 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 break;
9943
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009944 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 if (autocmd_bufnr <= 0)
9946 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009947 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 return NULL;
9949 }
9950 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9951 result = strbuf;
9952 break;
9953
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009954 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 result = autocmd_match;
9956 if (result == NULL)
9957 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009958 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 return NULL;
9960 }
9961 break;
9962
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009963 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009964 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 if (result == NULL)
9966 {
LemonBoy6013d002022-04-09 21:42:10 +01009967 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9968 return NULL;
9969 }
9970 resultbuf = result; // remember allocated string
9971 break;
9972 case SPEC_STACK: // call stack
9973 result = estack_sfile(ESTACK_STACK);
9974 if (result == NULL)
9975 {
9976 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9977 return NULL;
9978 }
9979 resultbuf = result; // remember allocated string
9980 break;
9981 case SPEC_SCRIPT: // script file name
9982 result = estack_sfile(ESTACK_SCRIPT);
9983 if (result == NULL)
9984 {
9985 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 return NULL;
9987 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009988 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009990
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009991 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009992 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009993 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009994 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009995 return NULL;
9996 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009997 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009998 result = strbuf;
9999 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010000
10001#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010002 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010010003 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010004 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +000010005 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010006 return NULL;
10007 }
10008 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010010009 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010010 result = strbuf;
10011 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010012
Bram Moolenaar90944302020-08-01 20:45:11 +020010013 case SPEC_SID:
10014 if (current_sctx.sc_sid <= 0)
10015 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +000010016 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +020010017 return NULL;
10018 }
10019 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
10020 result = strbuf;
10021 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +020010022#endif
Bram Moolenaar90944302020-08-01 20:45:11 +020010023
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010024#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010025 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010026 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10027 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 result = strbuf;
10029 break;
10030#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010031
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010032 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010033 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010034 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 }
10036
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010037 resultlen = STRLEN(result); // length of new string
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010038 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 {
10040 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010042 resultlen = s - result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 else if (!skip_mod)
10045 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010046 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 &resultlen);
10048 if (result == NULL)
10049 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010050 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 return NULL;
10052 }
10053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 }
10055
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010056 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010058 if (empty_is_error)
10059 {
10060 if (valid != VALID_HEAD + VALID_PATH)
10061 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
10062 else
10063 *errormsg = _(e_evaluates_to_an_empty_string);
10064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 result = NULL;
10066 }
10067 else
10068 result = vim_strnsave(result, resultlen);
10069 vim_free(resultbuf);
10070 return result;
10071}
10072
10073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 * Expand the <sfile> string in "arg".
10075 *
10076 * Returns an allocated string, or NULL for any error.
10077 */
10078 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010079expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010081 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +020010083 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +020010085 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +020010087 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +020010088 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 char_u *p;
10090
John Marriotted908f72024-04-18 22:46:56 +020010091 resultlen = STRLEN(arg);
10092 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 if (result == NULL)
10094 return NULL;
10095
10096 for (p = result; *p; )
10097 {
10098 if (STRNCMP(p, "<sfile>", 7) != 0)
10099 ++p;
10100 else
10101 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010102 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +010010103 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 if (errormsg != NULL)
10105 {
10106 if (*errormsg)
10107 emsg(errormsg);
10108 vim_free(result);
10109 return NULL;
10110 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010111 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 {
10113 p += srclen;
10114 continue;
10115 }
John Marriotted908f72024-04-18 22:46:56 +020010116 repllen = STRLEN(repl);
10117 resultlen += repllen - srclen;
10118 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 if (newres == NULL)
10120 {
10121 vim_free(repl);
10122 vim_free(result);
10123 return NULL;
10124 }
John Marriotted908f72024-04-18 22:46:56 +020010125 len = p - result;
10126 mch_memmove(newres, result, len);
10127 STRCPY(newres + len, repl);
10128 len += repllen;
10129 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 vim_free(repl);
10131 vim_free(result);
10132 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010133 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 }
10135 }
10136
10137 return result;
10138}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010141/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010142 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010143 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010146dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 if (fname == NULL)
10149 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010150 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151}
10152#endif
10153
10154/*
10155 * ":behave {mswin,xterm}"
10156 */
10157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010158ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159{
10160 if (STRCMP(eap->arg, "mswin") == 0)
10161 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010162 set_option_value_give_err((char_u *)"selection",
10163 0L, (char_u *)"exclusive", 0);
10164 set_option_value_give_err((char_u *)"selectmode",
10165 0L, (char_u *)"mouse,key", 0);
10166 set_option_value_give_err((char_u *)"mousemodel",
10167 0L, (char_u *)"popup", 0);
10168 set_option_value_give_err((char_u *)"keymodel",
10169 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 }
10171 else if (STRCMP(eap->arg, "xterm") == 0)
10172 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010173 set_option_value_give_err((char_u *)"selection",
10174 0L, (char_u *)"inclusive", 0);
10175 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
10176 set_option_value_give_err((char_u *)"mousemodel",
10177 0L, (char_u *)"extend", 0);
10178 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 }
10180 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010181 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182}
10183
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184static int filetype_detect = FALSE;
10185static int filetype_plugin = FALSE;
10186static int filetype_indent = FALSE;
10187
10188/*
10189 * ":filetype [plugin] [indent] {on,off,detect}"
10190 * on: Load the filetype.vim file to install autocommands for file types.
10191 * off: Load the ftoff.vim file to remove all autocommands for file types.
10192 * plugin on: load filetype.vim and ftplugin.vim
10193 * plugin off: load ftplugof.vim
10194 * indent on: load filetype.vim and indent.vim
10195 * indent off: load indoff.vim
10196 */
10197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010198ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199{
10200 char_u *arg = eap->arg;
10201 int plugin = FALSE;
10202 int indent = FALSE;
10203
10204 if (*eap->arg == NUL)
10205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010206 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010207 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 filetype_detect ? "ON" : "OFF",
10209 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10210 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10211 return;
10212 }
10213
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010214 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 for (;;)
10216 {
10217 if (STRNCMP(arg, "plugin", 6) == 0)
10218 {
10219 plugin = TRUE;
10220 arg = skipwhite(arg + 6);
10221 continue;
10222 }
10223 if (STRNCMP(arg, "indent", 6) == 0)
10224 {
10225 indent = TRUE;
10226 arg = skipwhite(arg + 6);
10227 continue;
10228 }
10229 break;
10230 }
10231 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10232 {
10233 if (*arg == 'o' || !filetype_detect)
10234 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010235 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 filetype_detect = TRUE;
10237 if (plugin)
10238 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010239 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 filetype_plugin = TRUE;
10241 }
10242 if (indent)
10243 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010244 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 filetype_indent = TRUE;
10246 }
10247 }
10248 if (*arg == 'd')
10249 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010250 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010251 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 }
10253 }
10254 else if (STRCMP(arg, "off") == 0)
10255 {
10256 if (plugin || indent)
10257 {
10258 if (plugin)
10259 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010260 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 filetype_plugin = FALSE;
10262 }
10263 if (indent)
10264 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010265 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 filetype_indent = FALSE;
10267 }
10268 }
10269 else
10270 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010271 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 filetype_detect = FALSE;
10273 }
10274 }
10275 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010276 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277}
10278
10279/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010280 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 */
10282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010283ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284{
zeertzjq5bf6c212024-03-31 18:41:27 +020010285 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010286 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010287
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010288 char_u *arg = eap->arg;
10289 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10290 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010291
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010292 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10293 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +020010294 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010298ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299{
10300#ifdef FEAT_DIGRAPHS
10301 if (*eap->arg != NUL)
10302 putdigraph(eap->arg);
10303 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010304 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305#else
mityu61065042021-07-19 20:07:21 +020010306 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307#endif
10308}
10309
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010310#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10311 void
10312set_no_hlsearch(int flag)
10313{
10314 no_hlsearch = flag;
10315# ifdef FEAT_EVAL
10316 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10317# endif
10318}
10319
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320/*
10321 * ":nohlsearch"
10322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010324ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010326 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010327 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329#endif
10330
10331#ifdef FEAT_CRYPT
10332/*
10333 * ":X": Get crypt key
10334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010336ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010338 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010339 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340}
10341#endif
10342
10343#ifdef FEAT_FOLDING
10344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010345ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346{
10347 if (foldManualAllowed(TRUE))
10348 foldCreate(eap->line1, eap->line2);
10349}
10350
10351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010352ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
10354 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10355 eap->forceit, FALSE);
10356}
10357
10358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010359ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
10361 linenr_T lnum;
10362
Bram Moolenaar4facea32019-10-12 20:17:40 +020010363# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010364 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010365# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010366
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010367 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10369 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10370 ml_setmarked(lnum);
10371
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010372 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010374 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010375# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010376 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378}
10379#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010381#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010382/*
10383 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10384 * instead of a quickfix command.
10385 */
10386 int
10387is_loclist_cmd(int cmdidx)
10388{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010389 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010390 return FALSE;
10391 return cmdnames[cmdidx].cmd_name[0] == 'l';
10392}
10393#endif
10394
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010395 int
10396get_pressedreturn(void)
10397{
10398 return ex_pressedreturn;
10399}
10400
10401 void
10402set_pressedreturn(int val)
10403{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +020010404 ex_pressedreturn = val;
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010405}