blob: b985e2ba2bb8a5d856822b9579feab000f22d445 [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
Foxe Chenb90c2392025-06-27 21:10:35 +0200376#if !defined(FEAT_WAYLAND)
377# define ex_wlrestore ex_ni
378#endif
379#if !defined(FEAT_CLIPBOARD)
380# define ex_clipreset ex_ni
381#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100382#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200383# define ex_popupclear ex_ni
384#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386/*
387 * Declare cmdnames[].
388 */
389#define DO_DECLARE_EXCMD
390#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200391#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100392
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static char_u dollar_command[2] = {'$', 0};
394
395
396#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100397// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398typedef struct
399{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100400 char_u *line; // command line
401 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402} wcmd_T;
403
404/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000405 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000407 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000409struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100411 garray_T *lines_gap; // growarray with line info
412 int current_line; // last read line from growarray
413 int repeating; // TRUE when looping a second time
414 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100415 char_u *(*lc_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 void *cookie;
417};
418
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200419static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100420static int store_loop_line(garray_T *gap, char_u *line);
421static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000422
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100423// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000424struct dbg_stuff
425{
426 int trylevel;
427 int force_abort;
428 except_T *caught_stack;
429 char_u *vv_exception;
430 char_u *vv_throwpoint;
431 int did_emsg;
432 int got_int;
433 int did_throw;
434 int need_rethrow;
435 int check_cstack;
436 except_T *current_exception;
437};
438
Bram Moolenaared203462004-06-16 11:19:22 +0000439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100440save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000441{
442 dsp->trylevel = trylevel; trylevel = 0;
443 dsp->force_abort = force_abort; force_abort = FALSE;
444 dsp->caught_stack = caught_stack; caught_stack = NULL;
445 dsp->vv_exception = v_exception(NULL);
446 dsp->vv_throwpoint = v_throwpoint(NULL);
447
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100448 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000449 dsp->did_emsg = did_emsg; did_emsg = FALSE;
450 dsp->got_int = got_int; got_int = FALSE;
451 dsp->did_throw = did_throw; did_throw = FALSE;
452 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
453 dsp->check_cstack = check_cstack; check_cstack = FALSE;
454 dsp->current_exception = current_exception; current_exception = NULL;
455}
456
457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100458restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000459{
460 suppress_errthrow = FALSE;
461 trylevel = dsp->trylevel;
462 force_abort = dsp->force_abort;
463 caught_stack = dsp->caught_stack;
464 (void)v_exception(dsp->vv_exception);
465 (void)v_throwpoint(dsp->vv_throwpoint);
466 did_emsg = dsp->did_emsg;
467 got_int = dsp->got_int;
468 did_throw = dsp->did_throw;
469 need_rethrow = dsp->need_rethrow;
470 check_cstack = dsp->check_cstack;
471 current_exception = dsp->current_exception;
472}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#endif
474
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475/*
Colin Kennedy65e580b2024-03-26 18:29:30 +0100476 * Check if ffname differs from fnum.
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200477 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid
478 * buffer ID.
Colin Kennedy65e580b2024-03-26 18:29:30 +0100479 * ffname is a full path to where a buffer lives on-disk or would live on-disk.
480 *
481 */
482 static int
483is_other_file(int fnum, char_u *ffname)
484{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200485 if (fnum != 0)
486 {
487 if (fnum == curbuf->b_fnum)
488 return FALSE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100489
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200490 return TRUE;
491 }
Colin Kennedy65e580b2024-03-26 18:29:30 +0100492
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200493 if (ffname == NULL)
494 return TRUE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100495
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200496 if (*ffname == NUL)
497 return FALSE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100498
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200499 // TODO: Need a reliable way to know whether a buffer is meant to live
500 // on-disk !curbuf->b_dev_valid is not always available (example: missing
501 // on Windows)
502 if (curbuf->b_sfname != NULL
503 && *curbuf->b_sfname != NUL)
504 // This occurs with unsaved buffers. In which case `ffname` actually
505 // corresponds to curbuf->b_sfname
506 return fnamecmp(ffname, curbuf->b_sfname) != 0;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100507
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200508 return otherfile(ffname);
Colin Kennedy65e580b2024-03-26 18:29:30 +0100509}
510
511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
513 * command is given.
514 */
515 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100516do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100517 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
519 int save_msg_scroll;
520 int prev_msg_row;
521 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100522 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523
524 if (improved)
525 exmode_active = EXMODE_VIM;
526 else
527 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100528 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100529 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 // When using ":global /pat/ visual" and then "Q" we return to continue
532 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 if (global_busy)
534 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
536 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100537 ++RedrawingDisabled; // don't redisplay the window
538 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 ++hold_gui_events;
542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
Bram Moolenaar32526b32019-01-19 17:43:09 +0100544 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 while (exmode_active)
546 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000548 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
549 {
550 exmode_active = FALSE;
551 break;
552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 msg_scroll = TRUE;
554 need_wait_return = FALSE;
555 ex_pressedreturn = FALSE;
556 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100557 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 prev_msg_row = msg_row;
559 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (improved)
561 {
562 cmdline_row = msg_row;
563 do_cmdline(NULL, getexline, NULL, 0);
564 }
565 else
566 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
567 lines_left = Rows - 1;
568
Bram Moolenaardf177f62005-02-22 08:39:57 +0000569 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100570 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000572 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000573 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000574 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000576 if (ex_pressedreturn)
577 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100578 // go up one line, to overwrite the ":<CR>" line, so the
579 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000580 msg_row = prev_msg_row;
581 if (prev_msg_row == Rows - 1)
582 msg_row--;
583 }
584 msg_col = 0;
585 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
586 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100589 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000590 {
591 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000592 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000593 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000594 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597
598#ifdef FEAT_GUI
599 --hold_gui_events;
600#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100601 if (RedrawingDisabled > 0)
602 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100604 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 need_wait_return = FALSE;
606 msg_scroll = save_msg_scroll;
607}
608
609/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200610 * Print the executed command for when 'verbose' is set.
611 * When "lnum" is 0 only print the command.
612 */
613 static void
614msg_verbose_cmd(linenr_T lnum, char_u *cmd)
615{
616 ++no_wait_return;
617 verbose_enter_scroll();
618
619 if (lnum == 0)
620 smsg(_("Executing: %s"), cmd);
621 else
622 smsg(_("line %ld: %s"), (long)lnum, cmd);
623 if (msg_silent == 0)
624 msg_puts("\n"); // don't overwrite this
625
626 verbose_leave_scroll();
627 --no_wait_return;
628}
629
630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 * Execute a simple command line. Used for translated commands like "*".
632 */
633 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100634do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 return do_cmdline(cmd, NULL, NULL,
637 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
638}
639
640/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100641 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
642 * This allows for using a range without ":" in Vim9 script.
643 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200644 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100645do_cmd_argument(char_u *cmd)
646{
647 return do_cmdline(cmd, NULL, NULL,
648 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
649}
650
651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 * do_cmdline(): execute one Ex command line
653 *
654 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100655 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 * 2. Split up in parts separated with '|'.
657 *
658 * This function can be called recursively!
659 *
660 * flags:
661 * DOCMD_VERBOSE - The command will be included in the error message.
662 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100663 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 * DOCMD_KEYTYPED - Don't reset KeyTyped.
665 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
666 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
667 *
668 * return FAIL if cmdline could not be executed, OK otherwise
669 */
670 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100671do_cmdline(
672 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200673 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100674 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100675 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100677 char_u *next_cmdline; // next cmd to execute
678 char_u *cmdline_copy = NULL; // copy of cmd line
679 int used_getline = FALSE; // used "fgetline" to obtain command
680 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100682 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100683 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 int retval = OK;
685#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100686 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100687 garray_T lines_ga; // keep lines for ":while"/":for"
688 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200689 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100690 char_u *fname = NULL; // function or script name
691 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
692 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
693 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200695 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200696 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100698 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200699 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000701 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100705# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706# define cmd_cookie cookie
707#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100708 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200709#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
711 // location for storing error messages to be converted to an exception.
712 // This ensures that the do_errthrow() call in do_one_cmd() does not
713 // combine the messages stored by an earlier invocation of do_one_cmd()
714 // with the command name of the later one. This would happen when
715 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 saved_msg_list = msg_list;
717 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#endif
719
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100720 // It's possible to create an endless loop with ":execute", catch that
721 // here. The value of 200 allows nested function calls, ":source", etc.
722 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100723 if (call_depth >= 200
724#ifdef FEAT_EVAL
725 && call_depth >= p_mfd
726#endif
727 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000729 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100731 // When converting to an exception, we do not include the command name
732 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100733 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 msg_list = saved_msg_list;
735#endif
736 return FAIL;
737 }
738 ++call_depth;
739
740#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100741 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000743 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100745 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100747 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100748 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000749 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 ++ex_nesting_level;
751
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100752 // Get the function or script name and the address where the next breakpoint
753 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000754 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 fname = func_name(real_cookie);
757 breakpoint = func_breakpoint(real_cookie);
758 dbg_tick = func_dbg_tick(real_cookie);
759 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100760 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100762 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 breakpoint = source_breakpoint(real_cookie);
764 dbg_tick = source_dbg_tick(real_cookie);
765 }
766
767 /*
768 * Initialize "force_abort" and "suppress_errthrow" at the top level.
769 */
770 if (!recursive)
771 {
772 force_abort = FALSE;
773 suppress_errthrow = FALSE;
774 }
775
776 /*
777 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000778 * exception handling (used when debugging). Otherwise clear it to avoid
779 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000781 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000782 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000783 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200784 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786 initial_trylevel = trylevel;
787
788 /*
789 * "did_throw" will be set to TRUE when an exception is being thrown.
790 */
791 did_throw = FALSE;
792#endif
793 /*
794 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000795 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 * If force_abort is set, we cancel everything.
797 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100798#ifdef FEAT_EVAL
799 did_emsg_cumul += did_emsg;
800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 did_emsg = FALSE;
802
803 /*
804 * KeyTyped is only set when calling vgetc(). Reset it here when not
805 * calling vgetc() (sourced command lines).
806 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100807 if (!(flags & DOCMD_KEYTYPED)
808 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 KeyTyped = FALSE;
810
811 /*
812 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000813 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 * - for multiple commands on one line, separated with '|'
815 * - when repeating until there are no more lines (for ":source")
816 */
817 next_cmdline = cmdline;
818 do
819 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
823
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100824 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (next_cmdline == NULL
826#ifdef FEAT_EVAL
827 && !force_abort
828 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#endif
831 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100832 {
833#ifdef FEAT_EVAL
834 did_emsg_cumul += did_emsg;
835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
839 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000840 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100841 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 * 3. If a line is given: Make a copy, so we can mess with it.
843 */
844
845#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100846 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000847 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100849 // Each '|' separated command is stored separately in lines_ga, to
850 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100851 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100853 // Check if a function has returned or, unless it has an unclosed
854 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000855 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000857# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000858 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000859 func_line_end(real_cookie);
860# endif
861 if (func_has_ended(real_cookie))
862 {
863 retval = FAIL;
864 break;
865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000867#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000868 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000870 script_line_end();
871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100874 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 {
876 retval = FAIL;
877 break;
878 }
879
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100880 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (breakpoint != NULL && dbg_tick != NULL
882 && *dbg_tick != debug_tick)
883 {
884 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100886 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 *dbg_tick = debug_tick;
888 }
889
890 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100891 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100895 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100897 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100898 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100900 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100901 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 *dbg_tick = debug_tick;
903 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000905 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906 {
907 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100908 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100909 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000910 script_line_start();
911 }
912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#endif
915
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100916 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (next_cmdline == NULL)
918 {
919 /*
920 * Need to set msg_didout for the first line after an ":if",
921 * otherwise the ":if" will be overwritten.
922 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100923 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100925 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926#ifdef FEAT_EVAL
927 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
928#else
929 0
930#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100931 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
932 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100934 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100935 // returned for the end of a sourced file or executed function
936 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 if (KeyTyped && !(flags & DOCMD_REPEAT))
938 need_wait_return = FALSE;
939 retval = FAIL;
940 break;
941 }
942 used_getline = TRUE;
943
944 /*
945 * Keep the first typed line. Clear it when more lines are typed.
946 */
947 if (flags & DOCMD_KEEPLINE)
948 {
949 vim_free(repeat_cmdline);
950 if (count == 0)
951 repeat_cmdline = vim_strsave(next_cmdline);
952 else
953 repeat_cmdline = NULL;
954 }
955 }
956
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100957 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 else if (cmdline_copy == NULL)
959 {
960 next_cmdline = vim_strsave(next_cmdline);
961 if (next_cmdline == NULL)
962 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200963 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 retval = FAIL;
965 break;
966 }
967 }
968 cmdline_copy = next_cmdline;
969
970#ifdef FEAT_EVAL
971 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200972 * Inside a while/for loop, and when the command looks like a ":while"
973 * or ":for", the line is stored, because we may need it later when
974 * looping.
975 *
976 * When there is a '|' and another command, it is stored separately,
977 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000978 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200979 *
980 * Pass a different "fgetline" function to do_one_cmd() below,
981 * that it stores lines in or reads them from "lines_ga". Makes it
982 * possible to define a function inside a while/for loop and handles
983 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200985 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200987 cmd_getline = get_loop_line;
988 cmd_cookie = (void *)&cmd_loop_cookie;
989 cmd_loop_cookie.lines_gap = &lines_ga;
990 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100991 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200992 cmd_loop_cookie.cookie = cookie;
993 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
994
995 // Save the current line when encountering it the first time.
996 if (current_line == lines_ga.ga_len
997 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
999 retval = FAIL;
1000 break;
1001 }
Bram Moolenaard5053d02020-06-28 15:51:16 +02001002 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
Bram Moolenaard5053d02020-06-28 15:51:16 +02001004 else
1005 {
1006 cmd_getline = fgetline;
1007 cmd_cookie = cookie;
1008 }
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 did_endif = FALSE;
1011#endif
1012
1013 if (count++ == 0)
1014 {
1015 /*
1016 * All output from the commands is put below each other, without
1017 * waiting for a return. Don't do this when executing commands
1018 * from a script or when being called recursive (e.g. for ":e
1019 * +command file").
1020 */
1021 if (!(flags & DOCMD_NOWAIT) && !recursive)
1022 {
1023 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001024 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001026 msg_scroll = TRUE; // put messages below each other
1027 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001029 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031 }
1032
Bram Moolenaar823654b2020-05-29 23:03:09 +02001033 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001034 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036 /*
1037 * 2. Execute one '|' separated command.
1038 * do_one_cmd() will return NULL if there is no trailing '|'.
1039 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1040 */
1041 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001042 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043#ifdef FEAT_EVAL
1044 &cstack,
1045#endif
1046 cmd_getline, cmd_cookie);
1047 --recursive;
1048
1049#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001051 // Use "current_line" from "cmd_loop_cookie", it may have been
1052 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054#endif
1055
1056 if (next_cmdline == NULL)
1057 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001058 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 /*
1061 * If the command was typed, remember it for the ':' register.
1062 * Do this AFTER executing the command to make :@: work.
1063 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001064 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 && new_last_cmdline != NULL)
1066 {
1067 vim_free(last_cmdline);
1068 last_cmdline = new_last_cmdline;
1069 new_last_cmdline = NULL;
1070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
1072 else
1073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // need to copy the command after the '|' to cmdline_copy, for the
1075 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001076 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 next_cmdline = cmdline_copy;
1078 }
1079
1080
1081#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001082 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001084 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001086 {
1087 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
1093 ++current_line;
1094
1095 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 * An ":endwhile", ":endfor" and ":continue" is handled here.
1097 * If we were executing commands, jump back to the ":while" or
1098 * ":for".
1099 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001105 // Jump back to the matching ":while" or ":for". Be careful
1106 // not to use a cs_line[] from an entry that isn't a ":while"
1107 // or ":for": It would make "current_line" invalid and can
1108 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (!did_emsg && !got_int && !did_throw
1110 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 && (cstack.cs_flags[cstack.cs_idx]
1112 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 && cstack.cs_line[cstack.cs_idx] >= 0
1114 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1115 {
1116 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001117 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001119 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001121 // Check for the next breakpoint at or after the ":while"
1122 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001123 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
1125 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001126 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 fname,
1128 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1129 *dbg_tick = debug_tick;
1130 }
1131 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001134 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001136 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1137 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 }
1140
1141 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001142 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001144 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001146 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001147 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 }
1150
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001151 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001152 if (breakpoint != NULL && has_watchexpr())
1153 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001154 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001155 *dbg_tick = debug_tick;
1156 }
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 /*
1159 * When not inside any ":while" loop, clear remembered lines.
1160 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001161 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {
1163 if (lines_ga.ga_len > 0)
1164 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001165 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1167 free_cmdlines(&lines_ga);
1168 }
1169 current_line = 0;
1170 }
1171
1172 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1174 * being restored at the ":endtry". Reset them here and set the
1175 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1176 * This includes the case where a missing ":endif", ":endwhile" or
1177 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001179 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001181 cstack.cs_lflags &= ~CSL_HAD_FINA;
1182 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1183 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 did_throw ? (void *)current_exception : NULL);
1185 did_emsg = got_int = did_throw = FALSE;
1186 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1187 }
1188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001189 // Update global "trylevel" for recursive calls to do_cmdline() from
1190 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 trylevel = initial_trylevel + cstack.cs_trylevel;
1192
1193 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001194 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 * files) is aborted because of an error, an interrupt, or an uncaught
1196 * exception, cancel everything. If it is left normally, reset
1197 * force_abort to get the non-EH compatible abortion behavior for
1198 * the rest of the script.
1199 */
1200 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1201 force_abort = FALSE;
1202
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001203 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001205#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
1207 }
1208 /*
1209 * Continue executing command lines when:
1210 * - no CTRL-C typed, no aborting error, no exception thrown or try
1211 * conditionals need to be checked for executing finally clauses or
1212 * catching an interrupt exception
1213 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 * looping for ":source" command or function call.
1216 */
1217 while (!((got_int
1218#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001219 || (did_emsg && (force_abort || in_vim9script()))
1220 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#endif
1222 )
1223#ifdef FEAT_EVAL
1224 && cstack.cs_trylevel == 0
1225#endif
1226 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001227 && !(did_emsg
1228#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001229 // Keep going when inside try/catch, so that the error can be
1230 // deal with, except when it is a syntax error, it may cause
1231 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001232 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1233#endif
1234 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001235 && (getline_equal(fgetline, cookie, getexmodeline)
1236 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 && (next_cmdline != NULL
1238#ifdef FEAT_EVAL
1239 || cstack.cs_idx >= 0
1240#endif
1241 || (flags & DOCMD_REPEAT)));
1242
1243 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001244 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245#ifdef FEAT_EVAL
1246 free_cmdlines(&lines_ga);
1247 ga_clear(&lines_ga);
1248
1249 if (cstack.cs_idx >= 0)
1250 {
1251 /*
1252 * If a sourced file or executed function ran to its end, report the
1253 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001254 * In Vim9 script do not give a second error, executing aborts after
1255 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001257 if (!got_int && !did_throw && !aborting()
1258 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001259 && ((getline_equal(fgetline, cookie, getsourceline)
1260 && !source_finished(fgetline, cookie))
1261 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 && !func_has_ended(real_cookie))))
1263 {
1264 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001265 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001267 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001268 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001269 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001271 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273
1274 /*
1275 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1276 * ":endtry" in a sourced file or executed function. If the try
1277 * conditional is in its finally clause, ignore anything pending.
1278 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001279 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 */
1281 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001282 {
1283 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1284
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001285 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001286 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001287 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1288 &cstack.cs_looplevel);
1289 }
1290 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 trylevel = initial_trylevel;
1292 }
1293
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001294 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1295 // lack was reported above and the error message is to be converted to an
1296 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001297 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 ? (char_u *)"endfunction" : (char_u *)NULL);
1299
1300 if (trylevel == 0)
1301 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001302 // Just in case did_throw got set but current_exception wasn't.
1303 if (current_exception == NULL)
1304 did_throw = FALSE;
1305
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 /*
1307 * When an exception is being thrown out of the outermost try
1308 * conditional, discard the uncaught exception, disable the conversion
1309 * of interrupts or errors to exceptions, and ensure that no more
1310 * commands are executed.
1311 */
1312 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001313 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315 /*
1316 * On an interrupt or an aborting error not converted to an exception,
1317 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001318 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 * when force_abort is set and did_emsg unset in case of an interrupt
1320 * from a finally clause after an error.
1321 */
1322 else if (got_int || (did_emsg && force_abort))
1323 suppress_errthrow = TRUE;
1324 }
1325
1326 /*
1327 * The current cstack will be freed when do_cmdline() returns. An uncaught
1328 * exception will have to be rethrown in the previous cstack. If a function
1329 * has just returned or a script file was just finished and the previous
1330 * cstack belongs to the same function or, respectively, script file, it
1331 * will have to be checked for finally clauses to be executed due to the
1332 * ":return" or ":finish". This is done in do_one_cmd().
1333 */
1334 if (did_throw)
1335 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001336 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ex_nesting_level > func_level(real_cookie) + 1))
1340 {
1341 if (!did_throw)
1342 check_cstack = TRUE;
1343 }
1344 else
1345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001346 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001347 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 --ex_nesting_level;
1349 /*
1350 * Go to debug mode when returning from a function in which we are
1351 * single-stepping.
1352 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001353 if ((getline_equal(fgetline, cookie, getsourceline)
1354 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001356 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 ? (char_u *)_("End of sourced file")
1358 : (char_u *)_("End of function"));
1359 }
1360
1361 /*
1362 * Restore the exception environment (done after returning from the
1363 * debugger).
1364 */
1365 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001366 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
1368 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001369
1370 // Cleanup if "cs_emsg_silent_list" remains.
1371 if (cstack.cs_emsg_silent_list != NULL)
1372 {
1373 eslist_T *elem, *temp;
1374
1375 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1376 {
1377 temp = elem->next;
1378 vim_free(elem);
1379 }
1380 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001381#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
1383 /*
1384 * If there was too much output to fit on the command line, ask the user to
1385 * hit return before redrawing the screen. With the ":global" command we do
1386 * this only once after the command is finished.
1387 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001388 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001390 if (RedrawingDisabled > 0)
1391 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 --no_wait_return;
1393 msg_scroll = FALSE;
1394
1395 /*
1396 * When just finished an ":if"-":else" which was typed, no need to
1397 * wait for hit-return. Also for an error situation.
1398 */
1399 if (retval == FAIL
1400#ifdef FEAT_EVAL
1401 || (did_endif && KeyTyped && !did_emsg)
1402#endif
1403 )
1404 {
1405 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001406 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408 else if (need_wait_return)
1409 {
1410 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001411 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 * here should not overwrite the command that may be shown before
1413 * doing that.
1414 */
1415 msg_didout |= msg_didout_before_start;
1416 wait_return(FALSE);
1417 }
1418 }
1419
Bram Moolenaar2a942252012-11-28 23:03:07 +01001420#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001421 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001422#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 /*
1424 * Reset if_level, in case a sourced script file contains more ":if" than
1425 * ":endif" (could be ":if x | foo | endif").
1426 */
1427 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001428#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 --call_depth;
1431 return retval;
1432}
1433
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001434#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001435/*
1436 * Handle when "did_throw" is set after executing commands.
1437 */
1438 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001439handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001440{
1441 char *p = NULL;
1442 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001443 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001444
1445 /*
1446 * If the uncaught exception is a user exception, report it as an
1447 * error. If it is an error exception, display the saved error
1448 * message now. For an interrupt exception, do nothing; the
1449 * interrupt message is given elsewhere.
1450 */
1451 switch (current_exception->type)
1452 {
1453 case ET_USER:
1454 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001455 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001456 current_exception->value);
1457 p = (char *)vim_strsave(IObuff);
1458 break;
1459 case ET_ERROR:
1460 messages = current_exception->messages;
1461 current_exception->messages = NULL;
1462 break;
1463 case ET_INTERRUPT:
1464 break;
1465 }
1466
1467 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1468 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001469 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001470 current_exception->throw_name = NULL;
1471
1472 discard_current_exception(); // uses IObuff if 'verbose'
LemonBoy749ba0f2024-07-04 19:23:16 +02001473
1474 // If "silent!" is active the uncaught exception is not fatal.
1475 if (emsg_silent == 0)
1476 {
1477 suppress_errthrow = TRUE;
1478 force_abort = TRUE;
1479 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001480
1481 if (messages != NULL)
1482 {
1483 do
1484 {
1485 msglist_T *next = messages->next;
1486 int save_compiling = estack_compiling;
1487
1488 estack_compiling = messages->msg_compiling;
1489 emsg(messages->msg);
1490 vim_free(messages->msg);
1491 vim_free(messages->sfile);
1492 vim_free(messages);
1493 messages = next;
1494 estack_compiling = save_compiling;
1495 }
1496 while (messages != NULL);
1497 }
1498 else if (p != NULL)
1499 {
1500 emsg(p);
1501 vim_free(p);
1502 }
1503 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001504 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001505 estack_pop();
1506}
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001512get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001514 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 wcmd_T *wp;
1516 char_u *line;
1517
1518 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1519 {
1520 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001521 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001523 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001524 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001525 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001527 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001528 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 ++cp->current_line;
1530
1531 return line;
1532 }
1533
1534 KeyTyped = FALSE;
1535 ++cp->current_line;
1536 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001537 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 return vim_strsave(wp->line);
1539}
1540
1541/*
1542 * Store a line in "gap" so that a ":while" loop can execute it again.
1543 */
1544 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001545store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 if (ga_grow(gap, 1) == FAIL)
1548 return FAIL;
1549 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001550 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 return OK;
1553}
1554
1555/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001556 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
1558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001559free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 while (gap->ga_len > 0)
1562 {
1563 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1564 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566}
1567#endif
1568
1569/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001570 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1571 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001574getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001575 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001576 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001577 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001580 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001581 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001583 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1584 // function that's originally used to obtain the lines. This may be
1585 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001586 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001587 cp = (struct loop_cookie *)cookie;
1588 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001590 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 cp = cp->cookie;
1592 }
1593 return gp == func;
1594#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001595 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596#endif
1597}
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001600 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 * getline function. Otherwise return "cookie".
1602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001604getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001605 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
Bram Moolenaar13505972019-01-24 15:04:48 +01001608#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001609 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001610 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001612 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1613 // cookie that's originally used to obtain the lines. This may be nested
1614 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001615 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001616 cp = (struct loop_cookie *)cookie;
1617 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001619 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 cp = cp->cookie;
1621 }
1622 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001623#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001626}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001628#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001629/*
1630 * Get the next line source line without advancing.
1631 */
1632 char_u *
1633getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001634 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001635 void *cookie) // argument for fgetline()
1636{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001637 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001638 struct loop_cookie *cp;
1639 wcmd_T *wp;
1640
1641 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1642 // cookie that's originally used to obtain the lines. This may be nested
1643 // several levels.
1644 gp = fgetline;
1645 cp = (struct loop_cookie *)cookie;
1646 while (gp == get_loop_line)
1647 {
1648 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1649 {
1650 // executing lines a second time, use the stored copy
1651 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1652 return wp->line;
1653 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001654 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001655 cp = cp->cookie;
1656 }
1657 if (gp == getsourceline)
1658 return source_nextline(cp);
1659 return NULL;
1660}
1661#endif
1662
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001663
1664/*
1665 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1666 * ":bwipeout", etc.
1667 * Returns the buffer number.
1668 */
1669 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001670compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001671{
1672 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001673 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001674 int count = offset;
1675
1676 buf = firstbuf;
1677 while (buf->b_next != NULL && buf->b_fnum < lnum)
1678 buf = buf->b_next;
1679 while (count != 0)
1680 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001681 count += (offset < 0) ? 1 : -1;
1682 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1683 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001684 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001685 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001687 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001688 while (buf->b_ml.ml_mfp == NULL)
1689 {
1690 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1691 if (nextbuf == NULL)
1692 break;
1693 buf = nextbuf;
1694 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001695 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001696 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001697 if (addr_type == ADDR_LOADED_BUFFERS)
1698 while (buf->b_ml.ml_mfp == NULL)
1699 {
1700 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1701 if (nextbuf == NULL)
1702 break;
1703 buf = nextbuf;
1704 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001705 return buf->b_fnum;
1706}
1707
Bram Moolenaarb7316892019-05-01 18:08:42 +02001708/*
1709 * Return the window number of "win".
1710 * When "win" is NULL return the number of windows.
1711 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001712 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001713current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001714{
1715 win_T *wp;
1716 int nr = 0;
1717
Bram Moolenaar29323592016-07-24 22:04:11 +02001718 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001719 {
1720 ++nr;
1721 if (wp == win)
1722 break;
1723 }
1724 return nr;
1725}
1726
1727 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001728current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001729{
1730 tabpage_T *tp;
1731 int nr = 0;
1732
Bram Moolenaar29323592016-07-24 22:04:11 +02001733 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001734 {
1735 ++nr;
1736 if (tp == tab)
1737 break;
1738 }
1739 return nr;
1740}
1741
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001742 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001743comment_start(char_u *p, int starts_with_colon UNUSED)
1744{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001745 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001746 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001747 return *p == '"';
1748}
1749
Bram Moolenaarf240e182014-11-27 18:33:02 +01001750# define CURRENT_WIN_NR current_win_nr(curwin)
1751# define LAST_WIN_NR current_win_nr(NULL)
1752# define CURRENT_TAB_NR current_tab_nr(curtab)
1753# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755/*
1756 * Execute one Ex command.
1757 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001758 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1759 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 *
1761 * 1. skip comment lines and leading space
1762 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001763 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001764 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001765 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001766 * 6. parse arguments
1767 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001769 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 *
1771 * This function may be called recursively!
1772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001774do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001775 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001776 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001778 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001780 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001781 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001783 char_u *p;
1784 linenr_T lnum;
1785 long n;
1786 char *errormsg = NULL; // error message
1787 char_u *after_modifier = NULL;
1788 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001789 cmdmod_T save_cmdmod;
1790 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001791 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001792 int ni; // set when Not Implemented
1793 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001794 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001795 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001796#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001797 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001798#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001799 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001800 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaara80faa82020-04-12 19:37:17 +02001802 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 ea.line1 = 1;
1804 ea.line2 = 1;
1805#ifdef FEAT_EVAL
1806 ++ex_nesting_level;
1807#endif
1808
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001809 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (quitmore
1811#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001812 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001813 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001814#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001815 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001816 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 --quitmore;
1818
1819 /*
1820 * Reset browse, confirm, etc.. They are restored when returning, for
1821 * recursive calls.
1822 */
1823 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001825 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001826 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1827 goto doend;
1828
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001829/*
1830 * 1. Skip comment lines and leading white space and colons.
1831 * 2. Handle command modifiers.
1832 */
1833 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001835 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001836 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001837 ea.cookie = cookie;
1838#ifdef FEAT_EVAL
1839 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001840 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001842 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001843 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001844 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001845 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847#ifdef FEAT_EVAL
1848 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1849 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1850#else
1851 ea.skip = (if_level > 0);
1852#endif
1853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001855 * 3. Skip over the range to find the command. Let "p" point to after it.
1856 *
1857 * We need the command to know what kind of range it uses.
1858 */
1859 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001860
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001861 // In Vim9 script a colon is required before the range. This may also be
1862 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001863 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001864 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001865 {
1866 may_have_range = FALSE;
1867 for (p = ea.cmd; p >= *cmdlinep; --p)
1868 {
1869 if (*p == ':')
1870 may_have_range = TRUE;
1871 if (p < ea.cmd && !VIM_ISWHITE(*p))
1872 break;
1873 }
1874 }
1875 else
1876 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001877 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001878 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001880#ifdef FEAT_EVAL
1881 // Handle ":export" - it functions almost like a command modifier.
1882 // ":export var Name: type"
1883 // ":export def Name(..."
1884 // etc.
1885 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1886 is_export = TRUE;
1887#endif
1888
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001889 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001890 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001891 if (ea.cmd == cmd + 1 && *cmd == '$')
1892 // should be "$VAR = val"
1893 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001894#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001895 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001896#else
1897 p = find_ex_command(&ea, NULL, NULL, NULL);
1898#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001899 if (ea.cmdidx == CMD_SIZE)
1900 {
1901 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1902
1903 // If a ':' before the range is missing, give a clearer error
1904 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001905 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001906 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001907 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001908 goto doend;
1909 }
1910 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001914
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001915#ifdef FEAT_EVAL
1916# ifdef FEAT_PROFILE
1917 // Count this line for profiling if skip is TRUE.
1918 if (do_profiling == PROF_YES
1919 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1920 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1921 {
1922 int skip = did_emsg || got_int || did_throw;
1923
1924 if (ea.cmdidx == CMD_catch)
1925 skip = !skip && !(cstack->cs_idx >= 0
1926 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1927 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1928 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1929 skip = skip || !(cstack->cs_idx >= 0
1930 && !(cstack->cs_flags[cstack->cs_idx]
1931 & (CSF_ACTIVE | CSF_TRUE)));
1932 else if (ea.cmdidx == CMD_finally)
1933 skip = FALSE;
1934 else if (ea.cmdidx != CMD_endif
1935 && ea.cmdidx != CMD_endfor
1936 && ea.cmdidx != CMD_endtry
1937 && ea.cmdidx != CMD_endwhile)
1938 skip = ea.skip;
1939
1940 if (!skip)
1941 {
1942 if (getline_equal(fgetline, cookie, get_func_line))
1943 func_line_exec(getline_cookie(fgetline, cookie));
1944 else if (getline_equal(fgetline, cookie, getsourceline))
1945 script_line_exec();
1946 }
1947 }
1948# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001949#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001950
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001951 ea.cmd = cmd;
1952
1953#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001954 // May go to debug mode. If this happens and the ">quit" debug command is
1955 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001956 dbg_check_breakpoint(&ea);
1957 if (!ea.skip && got_int)
1958 {
1959 ea.skip = TRUE;
1960 (void)do_intthrow(cstack);
1961 }
1962#endif
1963
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001964/*
1965 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 *
1967 * where 'addr' is:
1968 *
1969 * % (entire file)
1970 * $ [+-NUM]
1971 * 'x [+-NUM] (where x denotes a currently defined mark)
1972 * . [+-NUM]
1973 * [+-NUM]..
1974 * NUM
1975 *
1976 * The ea.cmd pointer is updated to point to the first character following the
1977 * range spec. If an initial address is found, but no second, the upper bound
1978 * is equal to the lower.
1979 */
1980
Bram Moolenaar25190db2019-05-04 15:05:28 +02001981 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001982 if (!IS_USER_CMDIDX(ea.cmdidx))
1983 {
1984 if (ea.cmdidx != CMD_SIZE)
1985 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1986 else
1987 ea.addr_type = ADDR_LINES;
1988
Bram Moolenaar25190db2019-05-04 15:05:28 +02001989 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001990 if (ea.cmdidx == CMD_wincmd && p != NULL)
1991 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001992#ifdef FEAT_QUICKFIX
1993 // :.cc in quickfix window uses line number
1994 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1995 ea.addr_type = ADDR_OTHER;
1996#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001997 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001998
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001999 if (!may_have_range)
2000 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01002001 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
2002 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002005 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 */
2007
2008 /*
2009 * Skip ':' and any white space
2010 */
2011 ea.cmd = skipwhite(ea.cmd);
2012 while (*ea.cmd == ':')
2013 ea.cmd = skipwhite(ea.cmd + 1);
2014
2015 /*
2016 * If we got a line, but no command, then go to the line.
2017 * If we find a '|' or '\n' we set ea.nextcmd.
2018 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002019 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2020 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 /*
2023 * strange vi behaviour:
2024 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002025 * ":3|..." prints line 3 (not in Vim9 script)
2026 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002028 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002030 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 goto doend;
2032 }
2033
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // If this looks like an undefined user command and there are CmdUndefined
2035 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002036 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2037 && ASCII_ISUPPER(*ea.cmd)
2038 && has_cmdundefined())
2039 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002040 int ret;
2041
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002042 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002043 while (ASCII_ISALNUM(*p))
2044 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002045 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002046 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2047 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002048 // If the autocommands did something and didn't cause an error, try
2049 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002050 p = (ret
2051#ifdef FEAT_EVAL
2052 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002053#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002055 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (p == NULL)
2058 {
2059 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002060 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 goto doend;
2062 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002063 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002064 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2065 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
2067 errormsg = uc_fun_cmd();
2068 goto doend;
2069 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (ea.cmdidx == CMD_SIZE)
2072 {
2073 if (!ea.skip)
2074 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002075 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 // If the modifier was parsed OK the error must be in the
2079 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002080 if (after_modifier != NULL)
2081 append_command(after_modifier);
2082 else
2083 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002084 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002085 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002086 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002087 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089 goto doend;
2090 }
2091
Bram Moolenaar958636c2014-10-21 20:01:58 +02002092 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2093 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002094#ifdef HAVE_EX_SCRIPT_NI
2095 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2096#endif
2097 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099#ifndef FEAT_EVAL
2100 /*
2101 * When the expression evaluation is disabled, recognize the ":if" and
2102 * ":endif" commands and ignore everything in between it.
2103 */
2104 if (ea.cmdidx == CMD_if)
2105 ++if_level;
2106 if (if_level)
2107 {
2108 if (ea.cmdidx == CMD_endif)
2109 --if_level;
2110 goto doend;
2111 }
2112
2113#endif
2114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002115 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002116 if (*p == '!' && ea.cmdidx != CMD_substitute
2117 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
2119 ++p;
2120 ea.forceit = TRUE;
2121 }
2122 else
2123 ea.forceit = FALSE;
2124
2125/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002126 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002128 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002129 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
2131 if (!ea.skip)
2132 {
2133#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002134 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002136 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002137 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 goto doend;
2139 }
2140#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002141 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002142 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002143 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002144 goto doend;
2145 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002146 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002148 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002149 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 goto doend;
2151 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002152
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002153 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002155 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2156 {
2157 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002158 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002159 goto doend;
2160 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002161 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2162 {
2163 // Command not allowed when text is locked
2164 errormsg = _(get_text_locked_msg());
2165 goto doend;
2166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 // Disallow editing another buffer when "curbuf_lock" is set.
2170 // Do allow ":checktime" (it is postponed).
2171 // Do allow ":edit" (check for an argument later).
2172 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002173 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002174 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002175 && ea.cmdidx != CMD_edit
2176 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002177 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002178 && curbuf_locked())
2179 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002181 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002183 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 goto doend;
2185 }
2186 }
2187
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002188 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002190 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 goto doend;
2192 }
2193
2194 /*
2195 * Don't complain about the range if it is not used
2196 * (could happen if line_count is accidentally set to 0).
2197 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002198 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
2200 /*
2201 * If the range is backwards, ask for confirmation and, if given, swap
2202 * ea.line1 & ea.line2 so it's forwards again.
2203 * When global command is busy, don't ask, will fail below.
2204 */
2205 if (!global_busy && ea.line1 > ea.line2)
2206 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002207 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002209 if (sourcing || exmode_active)
2210 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002211 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002212 goto doend;
2213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (ask_yesno((char_u *)
2215 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002216 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
2218 lnum = ea.line1;
2219 ea.line1 = ea.line2;
2220 ea.line2 = lnum;
2221 }
2222 if ((errormsg = invalid_range(&ea)) != NULL)
2223 goto doend;
2224 }
2225
Bram Moolenaarb7316892019-05-01 18:08:42 +02002226 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2227 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 ea.line2 = 1;
2229
2230 correct_range(&ea);
2231
2232#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002233 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002234 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002236 // Put the first line at the start of a closed fold, put the last line
2237 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 (void)hasFolding(ea.line1, &ea.line1, NULL);
2239 (void)hasFolding(ea.line2, NULL, &ea.line2);
2240 }
2241#endif
2242
2243#ifdef FEAT_QUICKFIX
2244 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002245 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 * option here, so things like % get expanded.
2247 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002248 p = replace_makeprg(&ea, p, cmdlinep);
2249 if (p == NULL)
2250 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#endif
2252
2253 /*
2254 * Skip to start of argument.
2255 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2256 */
2257 if (ea.cmdidx == CMD_bang)
2258 ea.arg = p;
2259 else
2260 ea.arg = skipwhite(p);
2261
Bram Moolenaar379fb762018-08-30 15:58:28 +02002262 // ":file" cannot be run with an argument when "curbuf_lock" is set
2263 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2264 goto doend;
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 /*
2267 * Check for "++opt=val" argument.
2268 * Must be first, allow ":w ++enc=utf8 !cmd"
2269 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002270 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2272 if (getargopt(&ea) == FAIL && !ni)
2273 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002274 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 goto doend;
2276 }
2277
2278 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2279 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002280 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002282 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002284 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 goto doend;
2286 }
2287 ea.arg = skipwhite(ea.arg + 1);
2288 ea.append = TRUE;
2289 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002290 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 ++ea.arg;
2293 ea.usefilter = TRUE;
2294 }
2295 }
2296
2297 if (ea.cmdidx == CMD_read)
2298 {
2299 if (ea.forceit)
2300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002301 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 ea.forceit = FALSE;
2303 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002304 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 ++ea.arg;
2307 ea.usefilter = TRUE;
2308 }
2309 }
2310
2311 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2312 {
2313 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002314 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 ++ea.arg;
2317 ++ea.amount;
2318 }
2319 ea.arg = skipwhite(ea.arg);
2320 }
2321
2322 /*
2323 * Check for "+command" argument, before checking for next command.
2324 * Don't do this for ":read !cmd" and ":write !cmd".
2325 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002326 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2328
2329 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002330 * For commands that do not use '|' inside their argument: Check for '|' to
2331 * separate commands and '"' or '#' to start comments.
2332 *
2333 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002334 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002335 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002336 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002338 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2339 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002340 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002341 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002342 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002343 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002344 || ea.cmdidx == CMD_global
2345 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002346 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002347#ifdef FEAT_EVAL
2348 || inside_block(&ea)
2349#endif
2350 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 for (p = ea.arg; *p; ++p)
2353 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002354 // Remove one backslash before a newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002356 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002357 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
2359 ea.nextcmd = p + 1;
2360 *p = NUL;
2361 break;
2362 }
2363 }
2364 }
2365
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002366 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002367 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002369 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002370 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002372 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002373 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002374 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002376#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002377 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002378 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002380 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002381 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 }
2383#endif
64-bitmane08f10a2025-03-18 22:14:34 +01002384 if (valid_yank_reg(*ea.arg, (!IS_USER_CMDIDX(ea.cmdidx)
2385 && ea.cmdidx != CMD_put && ea.cmdidx != CMD_iput)))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002386 {
2387 ea.regname = *ea.arg++;
2388#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002389 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002390 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2391 {
kuuote08d7b1c2021-10-04 22:17:36 +01002392 if (!ea.skip)
2393 {
2394 set_expr_line(vim_strsave(ea.arg), &ea);
2395 did_set_expr_line = TRUE;
2396 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002397 ea.arg += STRLEN(ea.arg);
2398 }
2399#endif
2400 ea.arg = skipwhite(ea.arg);
2401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403
2404 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002405 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 * count, it's a buffer name.
2407 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002408 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002409 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002410 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002412 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002414 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002416 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 goto doend;
2418 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002419 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
2421 ea.line2 = n;
2422 if (ea.addr_count == 0)
2423 ea.addr_count = 1;
2424 }
2425 else
2426 {
2427 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002428 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002429 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002430 else
2431 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 ++ea.addr_count;
2433 /*
2434 * Be vi compatible: no error message for out of range.
2435 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002436 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 ea.line2 = curbuf->b_ml.ml_line_count;
2438 }
2439 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002440
2441 /*
2442 * Check for flags: 'l', 'p' and '#'.
2443 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002444 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002445 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002446 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2447 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002449 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002450 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 goto doend;
2452 }
2453
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002454 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002456 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 goto doend;
2458 }
2459
2460#ifdef FEAT_EVAL
2461 /*
2462 * Skip the command when it's not going to be executed.
2463 * The commands like :if, :endif, etc. always need to be executed.
2464 * Also make an exception for commands that handle a trailing command
2465 * themselves.
2466 */
2467 if (ea.skip)
2468 {
2469 switch (ea.cmdidx)
2470 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002471 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 case CMD_while:
2473 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002474 case CMD_for:
2475 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 case CMD_if:
2477 case CMD_elseif:
2478 case CMD_else:
2479 case CMD_endif:
2480 case CMD_try:
2481 case CMD_catch:
2482 case CMD_finally:
2483 case CMD_endtry:
2484 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002485 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 break;
2487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002488 // Commands that handle '|' themselves. Check: A command should
2489 // either have the EX_TRLBAR flag, appear in this list or appear in
2490 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_aboveleft:
2492 case CMD_and:
2493 case CMD_belowright:
2494 case CMD_botright:
2495 case CMD_browse:
2496 case CMD_call:
2497 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002498 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 case CMD_delfunction:
2500 case CMD_djump:
2501 case CMD_dlist:
2502 case CMD_dsearch:
2503 case CMD_dsplit:
2504 case CMD_echo:
2505 case CMD_echoerr:
2506 case CMD_echomsg:
2507 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002508 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002510 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002511 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 case CMD_help:
2513 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002514 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 case CMD_ijump:
2516 case CMD_ilist:
2517 case CMD_isearch:
2518 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002519 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 case CMD_keepjumps:
2521 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002522 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 case CMD_leftabove:
2524 case CMD_let:
2525 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002526 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002527 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002529 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002530 case CMD_noautocmd:
2531 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 case CMD_perl:
2533 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002534 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002535 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002536 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 case CMD_return:
2538 case CMD_rightbelow:
2539 case CMD_ruby:
2540 case CMD_silent:
2541 case CMD_smagic:
2542 case CMD_snomagic:
2543 case CMD_substitute:
2544 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002545 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 case CMD_tcl:
2547 case CMD_throw:
2548 case CMD_tilde:
2549 case CMD_topleft:
2550 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002551 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002552 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 case CMD_verbose:
2554 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002555 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 break;
2557
2558 default: goto doend;
2559 }
2560 }
2561#endif
2562
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002563 if ((ea.argt & EX_XFILE)
2564 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2565 goto doend;
2566
2567#ifdef FEAT_EVAL
2568 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002570 emsg(_(e_invalid_command_after_export));
2571 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 /*
2576 * Accept buffer name. Cannot be used at the same time with a buffer
2577 * number. Don't do this for a user command.
2578 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002579 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002580 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
2582 /*
2583 * :bdelete, :bwipeout and :bunload take several arguments, separated
2584 * by spaces: find next space (skipping over escaped characters).
2585 * The others take one argument: ignore trailing spaces.
2586 */
2587 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2588 || ea.cmdidx == CMD_bunload)
2589 p = skiptowhite_esc(ea.arg);
2590 else
2591 {
2592 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002593 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 --p;
2595 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002596 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002597 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002598 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 goto doend;
2600 ea.addr_count = 1;
2601 ea.arg = skipwhite(p);
2602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002604 // The :try command saves the emsg_silent flag, reset it here when
2605 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002606 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002607 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002608 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002609 if (emsg_silent < 0)
2610 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002611 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002612 }
2613
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002615 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
Bram Moolenaar958636c2014-10-21 20:01:58 +02002618 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 /*
2621 * Execute a user-defined command.
2622 */
2623 do_ucmd(&ea);
2624 }
2625 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
2627 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002628 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2631 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002632 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634
2635#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002636 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002637 // set something went wrong or the command was never executed.
2638 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002639 {
2640 if (errormsg == NULL)
2641 errormsg = _(e_export_with_invalid_argument);
2642 is_export = FALSE;
2643 }
2644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002646 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002647 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002648 && current_sctx.sc_sid > 0
2649 && ea.cmdidx != CMD_endif
2650 && (cstack->cs_idx < 0
2651 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002652 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 /*
2655 * If the command just executed called do_cmdline(), any throw or ":return"
2656 * or ":finish" encountered there must also check the cstack of the still
2657 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2658 * exception, or reanimate a returned function or finished script file and
2659 * return or finish it again.
2660 */
2661 if (need_rethrow)
2662 do_throw(cstack);
2663 else if (check_cstack)
2664 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002665 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002667 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 && current_func_returned())
2669 do_return(&ea, TRUE, FALSE, NULL);
2670 }
2671 need_rethrow = check_cstack = FALSE;
2672#endif
2673
2674doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002675 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002678 curwin->w_cursor.col = 0;
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2682 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002683 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002685 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
2687 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002688 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002690 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692 emsg(errormsg);
2693 }
2694#ifdef FEAT_EVAL
2695 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002696 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2697 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002698
2699 if (did_set_expr_line)
2700 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002701 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#endif
2703
Bram Moolenaare1004402020-10-24 20:49:43 +02002704 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002706 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002707 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002709 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 ea.nextcmd = NULL;
2711
2712#ifdef FEAT_EVAL
2713 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002714 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#endif
2716
2717 return ea.nextcmd;
2718}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002720static char ex_error_buf[MSG_BUF_LEN];
2721
2722/*
2723 * Return an error message with argument included.
2724 * Uses a static buffer, only the last error will be kept.
2725 * "msg" will be translated, caller should use N_().
2726 */
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02002727 char *
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002728ex_errmsg(char *msg, char_u *arg)
2729{
2730 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2731 return ex_error_buf;
2732}
2733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002735 * The "+" string used in place of an empty command in Ex mode.
2736 * This string is used in pointer comparison.
2737 */
2738static char exmode_plus[] = "+";
2739
2740/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002741 * Handle a range without a command.
2742 * Returns an error message on failure.
2743 */
2744 char *
2745ex_range_without_command(exarg_T *eap)
2746{
2747 char *errormsg = NULL;
2748
zeertzjq7d664bf2024-07-14 10:22:54 +02002749 if ((*eap->cmd == '|' ||
2750 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002751#ifdef FEAT_EVAL
2752 && !in_vim9script()
2753#endif
2754 )
2755 {
2756 eap->cmdidx = CMD_print;
2757 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2758 if ((errormsg = invalid_range(eap)) == NULL)
2759 {
2760 correct_range(eap);
2761 ex_print(eap);
2762 }
2763 }
2764 else if (eap->addr_count != 0)
2765 {
2766 if (eap->line2 > curbuf->b_ml.ml_line_count)
2767 {
2768 // With '-' in 'cpoptions' a line number past the file is an
2769 // error, otherwise put it at the end of the file.
2770 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2771 eap->line2 = -1;
2772 else
2773 eap->line2 = curbuf->b_ml.ml_line_count;
2774 }
2775
2776 if (eap->line2 < 0)
2777 errormsg = _(e_invalid_range);
2778 else
2779 {
2780 if (eap->line2 == 0)
2781 curwin->w_cursor.lnum = 1;
2782 else
2783 curwin->w_cursor.lnum = eap->line2;
2784 beginline(BL_SOL | BL_FIX);
2785 }
2786 }
2787 return errormsg;
2788}
2789
2790/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002791 * Check for an Ex command with optional tail.
2792 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002793 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002794 */
2795 static int
2796checkforcmd_opt(
2797 char_u **pp, // start of command
2798 char *cmd, // name of command
2799 int len, // required length
2800 int noparen)
2801{
2802 int i;
2803
2804 for (i = 0; cmd[i] != NUL; ++i)
2805 if (((char_u *)cmd)[i] != (*pp)[i])
2806 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002807 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002808 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002809 {
2810 *pp = skipwhite(*pp + i);
2811 return TRUE;
2812 }
2813 return FALSE;
2814}
2815
2816/*
2817 * Check for an Ex command with optional tail.
2818 * If there is a match advance "pp" to the argument and return TRUE.
2819 */
2820 int
2821checkforcmd(
2822 char_u **pp, // start of command
2823 char *cmd, // name of command
2824 int len) // required length
2825{
2826 return checkforcmd_opt(pp, cmd, len, FALSE);
2827}
2828
2829/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002830 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002831 * If there is a match advance "pp" to the argument and return TRUE.
2832 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002833 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002834checkforcmd_noparen(
2835 char_u **pp, // start of command
2836 char *cmd, // name of command
2837 int len) // required length
2838{
2839 return checkforcmd_opt(pp, cmd, len, TRUE);
2840}
2841
2842/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002843 * Parse and skip over command modifiers:
2844 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002845 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002847 * When "skip_only" is TRUE the global variables are not changed, except for
2848 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002849 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2850 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002851 * Call apply_cmdmod() to get the side effects of the modifiers:
2852 * - Increment "sandbox" for ":sandbox"
2853 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002854 * - set msg_silent for ":silent"
2855 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002856 * Return FAIL when the command is not to be executed.
2857 * May set "errormsg" to an error message.
2858 */
2859 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002860parse_command_modifiers(
2861 exarg_T *eap,
2862 char **errormsg,
2863 cmdmod_T *cmod,
2864 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002865{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002866 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002867 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002868 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002869 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002870 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002871 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002872
Bram Moolenaare1004402020-10-24 20:49:43 +02002873 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002874 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002875
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002876 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2877 {
2878 // The automatically inserted Visual area range is skipped, so that
2879 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002880 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2881 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002882 eap->cmd += 5;
2883 cmd_start = eap->cmd;
2884 has_visual_range = TRUE;
2885 }
2886
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002887 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002888 for (;;)
2889 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002890 char_u *p;
2891
Bram Moolenaareffed932018-08-14 13:38:17 +02002892 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002893 {
2894 if (*eap->cmd == ':')
2895 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002897 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002898
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002899 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002901 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2902 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2904 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002905 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002906 if (!skip_only)
2907 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002908 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 }
2910
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002911 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002912 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002913 {
2914 // a comment ends at a NL
zeertzjqf7b86092024-09-05 17:26:30 +02002915 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2916 if (eap->nextcmd != NULL)
2917 ++eap->nextcmd;
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002918 if (vim9script)
2919 {
2920 if (has_cmdmod(cmod, FALSE))
2921 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002922#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002923 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2924 && eap->cmd[2] != '{')
2925 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002926#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002927 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002928 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002929 }
zeertzjqf7b86092024-09-05 17:26:30 +02002930 if (*eap->cmd == '\n')
zeertzjq2432b4a2024-09-03 22:58:30 +02002931 {
2932 eap->nextcmd = eap->cmd + 1;
2933 return FAIL;
2934 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002935 if (*eap->cmd == NUL)
2936 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002937 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002938 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002939 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002940 if (vim9script && has_cmdmod(cmod, FALSE))
2941 *errormsg = _(e_command_modifier_without_command);
2942 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002943 return FAIL;
2944 }
2945
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002946 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002947
2948 // In Vim9 script a variable can shadow a command modifier:
2949 // verbose = 123
2950 // verbose += 123
2951 // silent! verbose = func()
2952 // verbose.member = 2
2953 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002954 // But not:
2955 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002956 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002957 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002958 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002959
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002960 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002961 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002962 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002963 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2964 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002965 break;
2966 }
2967
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 switch (*p)
2969 {
John Marriotted908f72024-04-18 22:46:56 +02002970 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002971 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002974 continue;
2975
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002976 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002977 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002978 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 continue;
2980 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002981 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 {
2983#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002984 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002985#endif
2986 continue;
2987 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002988 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002990 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002991 continue;
2992
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002993 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 break;
2995#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002996 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002997#endif
2998 continue;
2999
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003000 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003002 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003003 continue;
3004 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003005 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003007 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003008 continue;
3009 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003010 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003012 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003013 continue;
3014 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003015 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003017 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003018 continue;
3019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003020 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003022 char_u *reg_pat;
3023 char_u *nulp = NULL;
3024 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003025
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003026 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003027 || *p == NUL
3028 || (ends_excmd(*p)
3029#ifdef FEAT_EVAL
3030 // in ":filter #pat# cmd" # does not
3031 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003032 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003033#endif
3034 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003035 break;
3036 if (*p == '!')
3037 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003038 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003039 p = skipwhite(p + 1);
3040 if (*p == NUL || ends_excmd(*p))
3041 break;
3042 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003043#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003044 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003045 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003046 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003047#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003048 if (skip_only)
3049 p = skip_vimgrep_pat(p, NULL, NULL);
3050 else
3051 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003052 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3053 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003054 if (p == NULL || *p == NUL)
3055 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003056 if (!skip_only)
3057 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003058 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003059 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003060 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003061 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003062 // restore the character overwritten by NUL
3063 if (nulp != NULL)
3064 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003065 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003066 eap->cmd = p;
3067 continue;
3068 }
3069
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003070 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3071 {
3072 cmod->cmod_split |= WSP_HOR;
3073 continue;
3074 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003075 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003076 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003077 || *p == NUL || ends_excmd(*p))
3078 break;
3079 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003080 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003081 continue;
3082
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003083 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003084 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003085 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003086 continue;
3087 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003088 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3089 {
3090 if (ends_excmd2(p, eap->cmd))
3091 {
3092 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003093 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003094 return FAIL;
3095 }
3096 cmod->cmod_flags |= CMOD_LEGACY;
3097 continue;
3098 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003099
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003100 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003101 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003102 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003103 continue;
3104
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003105 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003106 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003107 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003108 continue;
3109 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003110 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003111 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003112 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003113 continue;
3114
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003115 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003116 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003117 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003118 continue;
3119
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003120 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003121 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003122 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003123 continue;
3124 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003125 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003126 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003127 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003128 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3129 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003130 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003131 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003132 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003133 }
3134 continue;
3135
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003136 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003137 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003138 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003139 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003140 long tabnr = get_address(eap, &eap->cmd,
3141 ADDR_TABS, eap->skip,
3142 skip_only, FALSE, 1);
3143 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003144 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003145 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003146 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003147 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3148 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003149 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003150 return FAIL;
3151 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003152 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003153 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003154 }
3155 eap->cmd = p;
3156 continue;
3157 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003158 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003159 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003160 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003161 continue;
3162
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003163 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003164 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003165 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003166 continue;
3167
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003168 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003169 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003170 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003171 continue;
3172 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003173 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003174 {
3175 if (ends_excmd2(p, eap->cmd))
3176 {
3177 *errormsg =
3178 _(e_vim9cmd_must_be_followed_by_command);
3179 return FAIL;
3180 }
3181 cmod->cmod_flags |= CMOD_VIM9CMD;
3182 continue;
3183 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003184 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003185 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003186 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003187 {
zeertzjqcd749632022-06-14 13:30:35 +01003188 // zero means not set, one is verbose == 0, etc.
3189 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003190 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003191 else
zeertzjqcd749632022-06-14 13:30:35 +01003192 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003193 eap->cmd = p;
3194 continue;
3195 }
3196 break;
3197 }
3198
Bram Moolenaar1501b632022-03-27 16:56:21 +01003199 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003200 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003201 if (eap->cmd > cmd_start)
3202 {
3203 // Move the '<,'> range to after the modifiers and insert a colon.
3204 // Since the modifiers have been parsed put the colon on top of the
3205 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3206 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003207 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003208 {
3209 size_t len = STRLEN(cmd_start);
3210
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003211 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003212 // "'<,'>mods" -> "mods *+
3213 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003214 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003215 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003216 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003217 }
3218 else
3219 {
3220 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3221 eap->cmd -= 5;
3222 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3223 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003224 }
3225 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003226 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003227 // Special case: change empty command to "+".
3228 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003229 eap->cmd = (char_u *)"'<,'>+";
3230 else
3231 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003232 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003233 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003234 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003235
Bram Moolenaareffed932018-08-14 13:38:17 +02003236 return OK;
3237}
3238
3239/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003240 * Return TRUE if "cmod" has anything set.
3241 */
3242 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003243has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003244{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003245 return (cmod->cmod_flags != 0 && (!ignore_silent
3246 || (cmod->cmod_flags
3247 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003248 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003249 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003250 || cmod->cmod_tab != 0
3251 || cmod->cmod_filter_regmatch.regprog != NULL;
3252}
3253
Bram Moolenaar8991be22022-02-14 21:51:46 +00003254#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003255/*
3256 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3257 */
3258 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003259cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003260{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003261 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003262 {
3263 emsg(_(e_misplaced_command_modifier));
3264 return TRUE;
3265 }
3266 return FALSE;
3267}
Dominique Pelle748b3082022-01-08 12:41:16 +00003268#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003269
3270/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003271 * Apply the command modifiers. Saves current state in "cmdmod", call
3272 * undo_cmdmod() later.
3273 */
3274 void
3275apply_cmdmod(cmdmod_T *cmod)
3276{
3277#ifdef HAVE_SANDBOX
3278 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3279 {
3280 ++sandbox;
3281 cmod->cmod_did_sandbox = TRUE;
3282 }
3283#endif
zeertzjqcd749632022-06-14 13:30:35 +01003284 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003285 {
3286 if (cmod->cmod_verbose_save == 0)
3287 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003288 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003289 }
3290
3291 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3292 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003293 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003294 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003295 cmod->cmod_save_msg_scroll = msg_scroll;
3296 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003297 if (cmod->cmod_flags & CMOD_SILENT)
3298 ++msg_silent;
3299 if (cmod->cmod_flags & CMOD_UNSILENT)
3300 msg_silent = 0;
3301
3302 if (cmod->cmod_flags & CMOD_ERRSILENT)
3303 {
3304 ++emsg_silent;
3305 ++cmod->cmod_did_esilent;
3306 }
3307
Bram Moolenaare1004402020-10-24 20:49:43 +02003308 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003309 {
3310 // Set 'eventignore' to "all".
3311 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003312 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003313 set_string_option_direct((char_u *)"ei", -1,
3314 (char_u *)"all", OPT_FREE, SID_NONE);
3315 }
3316}
3317
3318/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003319 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003320 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003321 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003322undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003323{
Bram Moolenaare1004402020-10-24 20:49:43 +02003324 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003325 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003326 p_verbose = cmod->cmod_verbose_save - 1;
3327 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003328 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003329
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003330#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003331 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003332 {
3333 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003334 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003335 }
3336#endif
3337
Bram Moolenaare1004402020-10-24 20:49:43 +02003338 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003340 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003341 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3342 OPT_FREE, SID_NONE);
3343 free_string_option(cmod->cmod_save_ei);
3344 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003345 }
3346
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003347 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003348
Bram Moolenaare1004402020-10-24 20:49:43 +02003349 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003350 {
3351 // messages could be enabled for a serious error, need to check if the
3352 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003353 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3354 msg_silent = cmod->cmod_save_msg_silent - 1;
3355 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003356 if (emsg_silent < 0)
3357 emsg_silent = 0;
3358 // Restore msg_scroll, it's set by file I/O commands, even when no
3359 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003360 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003361
3362 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3363 // somewhere in the line. Put it back in the first column.
3364 if (redirecting())
3365 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003366
Bram Moolenaare1004402020-10-24 20:49:43 +02003367 cmod->cmod_save_msg_silent = 0;
3368 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003369 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003370}
3371
3372/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003373 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003374 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003375 * Return FAIL and set "errormsg" or return OK.
3376 */
3377 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003378parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003379{
3380 int address_count = 1;
3381 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003382 int need_check_cursor = FALSE;
3383 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003384
3385 // Repeat for all ',' or ';' separated addresses.
3386 for (;;)
3387 {
3388 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003389 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003390 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003391 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003392 eap->addr_count == 0, address_count++);
3393 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003394 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003395 if (lnum == MAXLNUM)
3396 {
3397 if (*eap->cmd == '%') // '%' - all lines
3398 {
3399 ++eap->cmd;
3400 switch (eap->addr_type)
3401 {
3402 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003403 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003404 eap->line1 = 1;
3405 eap->line2 = curbuf->b_ml.ml_line_count;
3406 break;
3407 case ADDR_LOADED_BUFFERS:
3408 {
3409 buf_T *buf = firstbuf;
3410
3411 while (buf->b_next != NULL
3412 && buf->b_ml.ml_mfp == NULL)
3413 buf = buf->b_next;
3414 eap->line1 = buf->b_fnum;
3415 buf = lastbuf;
3416 while (buf->b_prev != NULL
3417 && buf->b_ml.ml_mfp == NULL)
3418 buf = buf->b_prev;
3419 eap->line2 = buf->b_fnum;
3420 break;
3421 }
3422 case ADDR_BUFFERS:
3423 eap->line1 = firstbuf->b_fnum;
3424 eap->line2 = lastbuf->b_fnum;
3425 break;
3426 case ADDR_WINDOWS:
3427 case ADDR_TABS:
3428 if (IS_USER_CMDIDX(eap->cmdidx))
3429 {
3430 eap->line1 = 1;
3431 eap->line2 = eap->addr_type == ADDR_WINDOWS
3432 ? LAST_WIN_NR : LAST_TAB_NR;
3433 }
3434 else
3435 {
3436 // there is no Vim command which uses '%' and
3437 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003438 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003439 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003440 }
3441 break;
3442 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003443 case ADDR_UNSIGNED:
3444 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003445 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003446 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003447 case ADDR_ARGUMENTS:
3448 if (ARGCOUNT == 0)
3449 eap->line1 = eap->line2 = 0;
3450 else
3451 {
3452 eap->line1 = 1;
3453 eap->line2 = ARGCOUNT;
3454 }
3455 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003456 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003457#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003458 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003459 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003460 if (eap->line2 == 0)
3461 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003462#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003463 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003464 case ADDR_NONE:
3465 // Will give an error later if a range is found.
3466 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003467 }
3468 ++eap->addr_count;
3469 }
3470 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3471 {
3472 pos_T *fp;
3473
3474 // '*' - visual area
3475 if (eap->addr_type != ADDR_LINES)
3476 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003477 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003478 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003479 }
3480
3481 ++eap->cmd;
3482 if (!eap->skip)
3483 {
3484 fp = getmark('<', FALSE);
3485 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003486 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003487 eap->line1 = fp->lnum;
3488 fp = getmark('>', FALSE);
3489 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003490 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003491 eap->line2 = fp->lnum;
3492 ++eap->addr_count;
3493 }
3494 }
3495 }
3496 else
3497 eap->line2 = lnum;
3498 eap->addr_count++;
3499
3500 if (*eap->cmd == ';')
3501 {
3502 if (!eap->skip)
3503 {
3504 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003505
Bram Moolenaar0e717042020-04-27 19:29:01 +02003506 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003507 // accept zero as address, so 0;/PATTERN/ works correctly
3508 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003509 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003510 if (eap->line2 > 0)
3511 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003512 else
3513 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003514 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003515 }
3516 }
3517 else if (*eap->cmd != ',')
3518 break;
3519 ++eap->cmd;
3520 }
3521
3522 // One address given: set start and end lines.
3523 if (eap->addr_count == 1)
3524 {
3525 eap->line1 = eap->line2;
3526 // ... but only implicit: really no address given
3527 if (lnum == MAXLNUM)
3528 eap->addr_count = 0;
3529 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003530 ret = OK;
3531
3532theend:
3533 if (need_check_cursor)
3534 check_cursor();
3535 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003536}
3537
3538/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003539 * Append "cmd" to the error message in IObuff.
3540 * Takes care of limiting the length and handling 0xa0, which would be
3541 * invisible otherwise.
3542 */
3543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003544append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003545{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003546 size_t len = STRLEN(IObuff);
3547 char_u *s = cmd;
3548 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003549
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003550 if (len > IOSIZE - 100)
3551 {
3552 // Not enough space, truncate and put in "...".
3553 d = IObuff + IOSIZE - 100;
3554 d -= mb_head_off(IObuff, d);
3555 STRCPY(d, "...");
3556 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003557 STRCAT(IObuff, ": ");
3558 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003559 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003560 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003561 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003562 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003563 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003564 STRCPY(d, "<a0>");
3565 d += 4;
3566 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003567 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3568 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003569 else
3570 MB_COPY_CHAR(s, d);
3571 }
3572 *d = NUL;
3573}
3574
Dominique Pelle748b3082022-01-08 12:41:16 +00003575#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003576/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003577 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3578 * the name. Otherwise just return "start".
3579 */
3580 char_u *
3581skip_option_env_lead(char_u *start)
3582{
3583 char_u *name = start;
3584
3585 if (*start == '&')
3586 {
3587 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3588 name += 3;
3589 else
3590 name += 1;
3591 }
3592 else if (*start == '$')
3593 name += 1;
3594 return name;
3595}
Dominique Pelle748b3082022-01-08 12:41:16 +00003596#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003597
3598/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003599 * Return TRUE and set "*idx" if "p" points to a one letter command.
3600 * If not in Vim9 script:
Doug Kearnsea842022024-09-29 17:17:41 +02003601 * - The 'k' command can directly be followed by any character
3602 * but :keepa[lt] is another command, as are :keepj[umps],
3603 * :kee[pmarks] and :keepp[atterns].
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003604 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3605 * but :sre[wind] is another command, as are :scr[iptnames],
3606 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3607 */
3608 static int
3609one_letter_cmd(char_u *p, cmdidx_T *idx)
3610{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003611 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003612 return FALSE;
Doug Kearnsea842022024-09-29 17:17:41 +02003613 if (p[0] == 'k'
3614 && (p[1] != 'e' || (p[1] == 'e' && p[2] != 'e')))
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003615 {
3616 *idx = CMD_k;
3617 return TRUE;
3618 }
3619 if (p[0] == 's'
3620 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3621 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3622 || p[1] == 'g'
3623 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3624 || p[1] == 'I'
3625 || (p[1] == 'r' && p[2] != 'e')))
3626 {
3627 *idx = CMD_substitute;
3628 return TRUE;
3629 }
3630 return FALSE;
3631}
3632
Dominique Pellee764d1b2023-03-12 21:20:59 +00003633#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003634/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003635 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3636 * call.
3637 */
3638 int
3639number_method(char_u *cmd)
3640{
3641 char_u *p = skipdigits(cmd);
3642
3643 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3644}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003645#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003646
3647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003649 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003650 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003651 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 *
3653 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3654 * assignment without "let". Sets eap->cmdidx to the command while returning
3655 * "eap->cmd".
3656 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 * Returns NULL for an ambiguous user command.
3658 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 char_u *
3660find_ex_command(
3661 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003662 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003663 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003664 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
3666 int len;
3667 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003668 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003669#ifndef FEAT_EVAL
3670 int vim9 = FALSE;
3671#else
3672 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 /*
3675 * Recognize a Vim9 script function/method call and assignment:
3676 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3677 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003678 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003679 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003681 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003683 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003684 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3685 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003687 int oplen;
3688 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003689 char_u *swp;
3690
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003691 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003692 || (eap->cmd[0] == '$'
3693 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003694 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003695 && (valid_yank_reg(eap->cmd[1], FALSE)
3696 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003697 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003698 if (*eap->cmd == '&')
3699 {
3700 p = eap->cmd + 1;
3701 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3702 p += 2;
3703 p = to_name_end(p, FALSE);
3704 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003705 else if (*eap->cmd == '$')
3706 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003707 else
3708 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003709 if (ends_excmd(*skipwhite(p)))
3710 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003711 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3712 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003713 eap->cmdidx = CMD_eval;
3714 return eap->cmd;
3715 }
3716 // "&option" can be followed by "->" or "=", check below
3717 }
3718
3719 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003721 if (
3722 // "(..." is an expression.
3723 // "funcname(" is always a function call.
3724 *p == '('
3725 || (p == eap->cmd
3726 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003727 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003728 *eap->cmd == '{'
3729 // "'string'->func()" is an expression.
3730 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003731 // '"string"->func()' is an expression.
3732 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003733 // '$"string"->func()' is an expression.
3734 // "$'string'->func()" is an expression.
3735 || (eap->cmd[0] == '$'
3736 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3737 // '0z1234->func()' is an expression.
3738 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003739 // "g:varname" is an expression.
3740 || eap->cmd[1] == ':'
3741 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003742 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003743 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003744 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003745 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3746 {
3747 // "{" by itself is the start of a block.
3748 eap->cmdidx = CMD_block;
3749 return eap->cmd + 1;
3750 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003751 eap->cmdidx = CMD_eval;
3752 return eap->cmd;
3753 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003754
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003755 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003756 // "varname[]" is an expression.
3757 *p == '['
3758 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003759 || (*p == '.'
3760 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3761 // g:[key] is an expression
3762 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003763 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003764 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003765
3766 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003767 // Skip over the whole thing, it can be:
3768 // name.member = val
3769 // name[a : b] = val
3770 // name[idx] = val
3771 // name[idx].member = val
3772 // etc.
3773 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003774 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003775 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003776 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003777 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003778 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003779 || (after[0] == '.' && after[1] == '.'
3780 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003781 eap->cmdidx = CMD_var;
3782 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003783 --emsg_silent;
3784 return eap->cmd;
3785 }
3786
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003787 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3788 // an assignment.
3789 // If there is no line break inside the "[...]" then "p" is
3790 // advanced to after the "]" by to_name_const_end(): check if a "="
3791 // follows.
3792 // If "[...]" has a line break "p" still points at the "[" and it
3793 // can't be an assignment.
3794 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003795 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003796 char_u *eq;
3797
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003798 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003799 if (p == eap->cmd && *p == '[')
3800 {
3801 int count = 0;
3802 int semicolon = FALSE;
3803
3804 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3805 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003806 eq = p;
3807 if (eq != NULL)
3808 {
3809 eq = skipwhite(eq);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003810 if (vim_strchr((char_u *)"+-*/%.", *eq) != NULL)
3811 {
3812 if (eq[0] == '.' && eq[1] == '.')
3813 ++eq;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003814 ++eq;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003815 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003816 }
3817 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003818 {
3819 eap->cmdidx = CMD_eval;
3820 return eap->cmd;
3821 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003822 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003823 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003824 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003825 return eap->cmd;
3826 }
3827 }
3828
3829 // Recognize an assignment if we recognize the variable name:
3830 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003831 // "@r = expr"
3832 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003833 // "var = expr" where "var" is a variable name or we are skipping
3834 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003835 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003836 oplen = assignment_len(skipwhite(p), &heredoc);
3837 if (oplen > 0)
3838 {
3839 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3840 || *eap->cmd == '&'
3841 || *eap->cmd == '$'
3842 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003843 || (eap->skip && IS_WHITE_OR_NUL(
3844 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003845 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003846 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003847 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003848 return eap->cmd;
3849 }
3850 }
3851
Bram Moolenaar9510d222022-09-11 15:14:05 +01003852 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003853 // "w:varname: number = 123".
3854 if (eap->cmd[1] == ':' && *p == ':')
3855 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003856 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003857 return eap->cmd;
3858 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003859 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003860
Bram Moolenaar31d99482022-05-26 22:24:43 +01003861 // 1234->func() is a method call
3862 if (number_method(eap->cmd))
3863 {
3864 eap->cmdidx = CMD_eval;
3865 return eap->cmd;
3866 }
3867
Bram Moolenaar7b829262021-10-13 15:04:34 +01003868 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3869 // an expression. A global/substitute/list command needs to use a
3870 // longer name.
3871 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3872 {
3873 eap->cmdidx = CMD_eval;
3874 return eap->cmd;
3875 }
3876
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003877 // If it is an ID it might be a variable with an operator on the next
3878 // line, if the variable exists it can't be an Ex command.
3879 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003880 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003881 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3882 {
3883 eap->cmdidx = CMD_eval;
3884 return eap->cmd;
3885 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003886
3887 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003888 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3889 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003890 {
3891 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3892 return eap->cmd + 2;
3893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 }
3895#endif
3896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 /*
3898 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 */
3900 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003901 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 ++p;
Doug Kearnsea842022024-09-29 17:17:41 +02003904 if (full != NULL)
3905 *full = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907 else
3908 {
3909 while (ASCII_ISALPHA(*p))
3910 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003911 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003912 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003914 while (ASCII_ISALNUM(*p))
3915 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 }
3917 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3918 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003919 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 ++p;
3921 while (ASCII_ISALPHA(*p))
3922 ++p;
3923 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003924
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003925 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003926 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 ++p;
3928 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003929 // The "d" command can directly be followed by 'l' or 'p' flag, when
3930 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003931 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003932 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003933 // Check for ":dl", ":dell", etc. to ":deletel": that's
3934 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003935 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003936 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003937 break;
3938 if (i == len - 1)
3939 {
3940 --len;
3941 if (p[-1] == 'l')
3942 eap->flags |= EXFLAG_LIST;
3943 else
3944 eap->flags |= EXFLAG_PRINT;
3945 }
3946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003948 if (ASCII_ISLOWER(eap->cmd[0]))
3949 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003950 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003951 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003952
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003953 if (command_count != (int)CMD_SIZE)
3954 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003955 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003956 getout(1);
3957 }
3958
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003959 // Use a precomputed index for fast look-up in cmdnames[]
3960 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003961 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3962 if (ASCII_ISLOWER(c2))
3963 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3964 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003965 else if (ASCII_ISUPPER(eap->cmd[0]))
3966 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003968 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3971 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3972 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3973 (size_t)len) == 0)
3974 {
3975#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003976 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 *full = TRUE;
3978#endif
3979 break;
3980 }
3981
Bram Moolenaar204852a2022-03-05 12:56:44 +00003982 // :Print and :mode are not supported in Vim9 script.
3983 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003984 if (vim9 && eap->cmdidx != CMD_SIZE)
3985 {
3986 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3987 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003988 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003989 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003990 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003991 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003992 eap->cmdidx = CMD_SIZE;
3993 }
3994 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003995
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003996 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003997 // 'cpoptions'.
3998 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3999 p = eap->cmd;
4000
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004001 // Look for a user defined command as a last resort. Let ":Print" be
4002 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01004003 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
4004 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004006 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 while (ASCII_ISALNUM(*p))
4008 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004009 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01004011 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 eap->cmdidx = CMD_SIZE;
4013 }
4014
Bram Moolenaar204852a2022-03-05 12:56:44 +00004015 // ":fina" means ":finally" in legacy script, for backwards compatibility.
4016 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02004017 eap->cmdidx = CMD_finally;
4018
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004019#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004020 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004021 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004022 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004023 && (eap->cmdidx < 0 ||
4024 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004025 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004026 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4027
4028 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004029 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004030 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004031 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004032#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return p;
4035}
4036
4037#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004038typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004039{
4040 char *name;
4041 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004042 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004043} cmdmod_info_T;
4044
4045static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004046 {"aboveleft", 3, FALSE},
4047 {"belowright", 3, FALSE},
4048 {"botright", 2, FALSE},
4049 {"browse", 3, FALSE},
4050 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004051 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004052 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004053 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004054 {"keepalt", 5, FALSE},
4055 {"keepjumps", 5, FALSE},
4056 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004057 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004058 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004059 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004060 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004061 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004062 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004063 {"rightbelow", 6, FALSE},
4064 {"sandbox", 3, FALSE},
4065 {"silent", 3, FALSE},
4066 {"tab", 3, TRUE},
4067 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004068 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004069 {"verbose", 4, TRUE},
4070 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004071 {"vim9cmd", 4, FALSE}
4072}; // cmdmod_info_tab
4073
Bram Moolenaared53fb92007-11-24 20:50:24 +00004074/*
4075 * Return length of a command modifier (including optional count).
4076 * Return zero when it's not a modifier.
4077 */
4078 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004079modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004080{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004081 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004082 char_u *p = cmd;
4083
4084 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004085 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004086 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004087 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004088 for (j = 0; p[j] != NUL; ++j)
4089 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004090 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004091 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4092 && (p == cmd || cmdmod_info_tab[i].has_count))
4093 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004094 }
4095 return 0;
4096}
4097
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098/*
4099 * Return > 0 if an Ex command "name" exists.
4100 * Return 2 if there is an exact match.
4101 * Return 3 if there is an ambiguous match.
4102 */
4103 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004104cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
4106 exarg_T ea;
4107 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004108 int i;
4109 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004110 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
Christian Brabandt70a11a62024-07-26 19:13:55 +02004112 // Check command modifiers.
4113 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004115 for (j = 0; name[j] != NUL; ++j)
4116 if (name[j] != cmdmod_info_tab[i].name[j])
4117 break;
4118 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4119 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004122 // Check built-in commands and user defined commands.
4123 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004124 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004126 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004128 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004130 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4131 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004132 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004133 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4135}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004136
4137/*
4138 * "fullcommand" function
4139 */
4140 void
4141f_fullcommand(typval_T *argvars, typval_T *rettv)
4142{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004143 exarg_T ea;
4144 char_u *name;
4145 char_u *p;
4146 int vim9script = in_vim9script();
4147 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004148
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004149 rettv->v_type = VAR_STRING;
4150 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004151
Bram Moolenaaraa534142022-09-15 21:46:02 +01004152 if (in_vim9script()
4153 && (check_for_string_arg(argvars, 0) == FAIL
4154 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004155 return;
4156
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004157 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004158 if (name == NULL)
4159 return;
4160
Bram Moolenaaraa534142022-09-15 21:46:02 +01004161 if (argvars[1].v_type != VAR_UNKNOWN)
4162 {
4163 vim9script = tv_get_bool(&argvars[1]);
4164 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4165 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4166 }
4167
zeertzjqc024ed92022-01-04 16:22:52 +00004168 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004169 name++;
4170 name = skip_range(name, TRUE, NULL);
4171
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004172 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4173 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004174 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004175 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004176 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004177 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004178 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004179 goto theend;
4180
4181 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004182 {
4183 int res;
4184
4185 ++emsg_silent;
4186 res = not_in_vim9(&ea);
4187 --emsg_silent;
4188
4189 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004190 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004191 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004192
4193 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004194 ? get_user_command_name(ea.useridx, ea.cmdidx)
4195 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004196theend:
4197 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004198}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199#endif
4200
Bram Moolenaard0190392019-08-23 21:17:35 +02004201 cmdidx_T
4202excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
Bram Moolenaard0190392019-08-23 21:17:35 +02004204 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004206 if (!one_letter_cmd(cmd, &idx))
4207 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4208 idx = (cmdidx_T)((int)idx + 1))
4209 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4210 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
Bram Moolenaard0190392019-08-23 21:17:35 +02004212 return idx;
4213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
Bram Moolenaard0190392019-08-23 21:17:35 +02004215 long
4216excmd_get_argt(cmdidx_T idx)
4217{
4218 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219}
4220
4221/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004222 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 *
4224 * Backslashed delimiters after / or ? will be skipped, and commands will
4225 * not be expanded between /'s and ?'s or after "'".
4226 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004227 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 * Returns the "cmd" pointer advanced to beyond the range.
4229 */
4230 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004231skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004232 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004233 int skip_star, // skip "*" used for Visual range
4234 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004236 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004237 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004239 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004241 if (*cmd == '\\')
4242 {
4243 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4244 ++cmd;
4245 else
4246 break;
4247 }
4248 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004250 char_u *p = cmd;
4251
4252 // a quote is only valid at the start or after a separator
4253 while (p > cmd_start)
4254 {
4255 --p;
4256 if (!VIM_ISWHITE(*p))
4257 break;
4258 }
4259 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (*++cmd == NUL && ctx != NULL)
4262 *ctx = EXPAND_NOTHING;
4263 }
4264 else if (*cmd == '/' || *cmd == '?')
4265 {
4266 delim = *cmd++;
4267 while (*cmd != NUL && *cmd != delim)
4268 if (*cmd++ == '\\' && *cmd != NUL)
4269 ++cmd;
4270 if (*cmd == NUL && ctx != NULL)
4271 *ctx = EXPAND_NOTHING;
4272 }
4273 if (*cmd != NUL)
4274 ++cmd;
4275 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004276
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004277 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004278 while (*cmd == ':')
4279 cmd = skipwhite(cmd + 1);
4280
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004281 // Skip "*" used for Visual range.
4282 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4283 cmd = skipwhite(cmd + 1);
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return cmd;
4286}
4287
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004288 static void
4289addr_error(cmd_addr_T addr_type)
4290{
4291 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004292 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004293 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004294 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004295}
4296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004298 * Return the default address for an address type.
4299 */
4300 static linenr_T
4301default_address(exarg_T *eap)
4302{
4303 linenr_T lnum = 0;
4304
4305 switch (eap->addr_type)
4306 {
4307 case ADDR_LINES:
4308 case ADDR_OTHER:
4309 // Default is the cursor line number. Avoid using an invalid
4310 // line number though.
4311 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4312 lnum = curbuf->b_ml.ml_line_count;
4313 else
4314 lnum = curwin->w_cursor.lnum;
4315 break;
4316 case ADDR_WINDOWS:
4317 lnum = CURRENT_WIN_NR;
4318 break;
4319 case ADDR_ARGUMENTS:
4320 lnum = curwin->w_arg_idx + 1;
4321 if (lnum > ARGCOUNT)
4322 lnum = ARGCOUNT;
4323 break;
4324 case ADDR_LOADED_BUFFERS:
4325 case ADDR_BUFFERS:
4326 lnum = curbuf->b_fnum;
4327 break;
4328 case ADDR_TABS:
4329 lnum = CURRENT_TAB_NR;
4330 break;
4331 case ADDR_TABS_RELATIVE:
4332 case ADDR_UNSIGNED:
4333 lnum = 1;
4334 break;
4335 case ADDR_QUICKFIX:
4336#ifdef FEAT_QUICKFIX
4337 lnum = qf_get_cur_idx(eap);
4338#endif
4339 break;
4340 case ADDR_QUICKFIX_VALID:
4341#ifdef FEAT_QUICKFIX
4342 lnum = qf_get_cur_valid_idx(eap);
4343#endif
4344 break;
4345 case ADDR_NONE:
4346 // Will give an error later if a range is found.
4347 break;
4348 }
4349 return lnum;
4350}
4351
4352/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004353 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 *
4355 * Set ptr to the next character after the part that was interpreted.
4356 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004357 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 *
4359 * Return MAXLNUM when no Ex address was found.
4360 */
4361 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004362get_address(
4363 exarg_T *eap UNUSED,
4364 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004365 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004366 int skip, // only skip the address, don't use it
4367 int silent, // no errors or side effects
4368 int to_other_file, // flag: may jump to other file
4369 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
4371 int c;
4372 int i;
4373 long n;
4374 char_u *cmd;
4375 pos_T pos;
4376 pos_T *fp;
4377 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004378 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 cmd = skipwhite(*ptr);
4381 lnum = MAXLNUM;
4382 do
4383 {
4384 switch (*cmd)
4385 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004386 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 ++cmd;
4388 switch (addr_type)
4389 {
4390 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004391 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 lnum = curwin->w_cursor.lnum;
4393 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004395 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 break;
4397 case ADDR_ARGUMENTS:
4398 lnum = curwin->w_arg_idx + 1;
4399 break;
4400 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004401 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 lnum = curbuf->b_fnum;
4403 break;
4404 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004405 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004407 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004408 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004409 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004410 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004411 cmd = NULL;
4412 goto error;
4413 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004414 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004415#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004416 lnum = qf_get_cur_idx(eap);
4417#endif
4418 break;
4419 case ADDR_QUICKFIX_VALID:
4420#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004421 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004422#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004423 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004424 }
4425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 ++cmd;
4429 switch (addr_type)
4430 {
4431 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004432 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 lnum = curbuf->b_ml.ml_line_count;
4434 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004436 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004437 break;
4438 case ADDR_ARGUMENTS:
4439 lnum = ARGCOUNT;
4440 break;
4441 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004442 buf = lastbuf;
4443 while (buf->b_ml.ml_mfp == NULL)
4444 {
4445 if (buf->b_prev == NULL)
4446 break;
4447 buf = buf->b_prev;
4448 }
4449 lnum = buf->b_fnum;
4450 break;
4451 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004452 lnum = lastbuf->b_fnum;
4453 break;
4454 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004455 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004457 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004458 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004459 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004460 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004461 cmd = NULL;
4462 goto error;
4463 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004464 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004465#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004466 lnum = qf_get_size(eap);
4467 if (lnum == 0)
4468 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004469#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004470 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004471 case ADDR_QUICKFIX_VALID:
4472#ifdef FEAT_QUICKFIX
4473 lnum = qf_get_valid_size(eap);
4474 if (lnum == 0)
4475 lnum = 1;
4476#endif
4477 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 }
4479 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004481 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 if (*++cmd == NUL)
4483 {
4484 cmd = NULL;
4485 goto error;
4486 }
4487 if (addr_type != ADDR_LINES)
4488 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004489 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004490 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 goto error;
4492 }
4493 if (skip)
4494 ++cmd;
4495 else
4496 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004497 // Only accept a mark in another file when it is
4498 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4500 ++cmd;
4501 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004502 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 lnum = curwin->w_cursor.lnum;
4504 else
4505 {
4506 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
4508 cmd = NULL;
4509 goto error;
4510 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 lnum = fp->lnum;
4512 }
4513 }
4514 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
4516 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004517 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004518 c = *cmd++;
4519 if (addr_type != ADDR_LINES)
4520 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004521 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004522 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 goto error;
4524 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004525 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004527 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004528 if (*cmd == c)
4529 ++cmd;
4530 }
4531 else
4532 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004533 int flags;
4534
4535 pos = curwin->w_cursor; // save curwin->w_cursor
4536
4537 // When '/' or '?' follows another address, start from
4538 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004539 if (lnum > 0 && lnum != MAXLNUM)
4540 curwin->w_cursor.lnum =
4541 lnum > curbuf->b_ml.ml_line_count
4542 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004543
4544 // Start a forward search at the end of the line (unless
4545 // before the first line).
4546 // Start a backward search at the start of the line.
4547 // This makes sure we never match in the current
4548 // line, and can match anywhere in the
4549 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004550 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004551 curwin->w_cursor.col = MAXCOL;
4552 else
4553 curwin->w_cursor.col = 0;
4554 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004555 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004556 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004557 {
4558 curwin->w_cursor = pos;
4559 cmd = NULL;
4560 goto error;
4561 }
4562 lnum = curwin->w_cursor.lnum;
4563 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004564 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004565 cmd += searchcmdlen;
4566 }
4567 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004569 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004570 ++cmd;
4571 if (addr_type != ADDR_LINES)
4572 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004573 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004574 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004575 goto error;
4576 }
4577 if (*cmd == '&')
4578 i = RE_SUBST;
4579 else if (*cmd == '?' || *cmd == '/')
4580 i = RE_SEARCH;
4581 else
4582 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004583 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004584 cmd = NULL;
4585 goto error;
4586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004588 if (!skip)
4589 {
4590 /*
4591 * When search follows another address, start from
4592 * there.
4593 */
4594 if (lnum != MAXLNUM)
4595 pos.lnum = lnum;
4596 else
4597 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004599 /*
4600 * Start the search just like for the above
4601 * do_search().
4602 */
4603 if (*cmd != '?')
4604 pos.col = MAXCOL;
4605 else
4606 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004607 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004608 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004609 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004610 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004611 lnum = pos.lnum;
4612 else
4613 {
4614 cmd = NULL;
4615 goto error;
4616 }
4617 }
4618 ++cmd;
4619 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004622 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004623 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625
4626 for (;;)
4627 {
4628 cmd = skipwhite(cmd);
4629 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4630 break;
4631
4632 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004633 {
4634 switch (addr_type)
4635 {
4636 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004637 case ADDR_OTHER:
4638 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004639 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004640 break;
4641 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004642 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 break;
4644 case ADDR_ARGUMENTS:
4645 lnum = curwin->w_arg_idx + 1;
4646 break;
4647 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004648 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004649 lnum = curbuf->b_fnum;
4650 break;
4651 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004652 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004653 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004654 case ADDR_TABS_RELATIVE:
4655 lnum = 1;
4656 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004657 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004658#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004659 lnum = qf_get_cur_idx(eap);
4660#endif
4661 break;
4662 case ADDR_QUICKFIX_VALID:
4663#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004664 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004665#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004666 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004667 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004668 case ADDR_UNSIGNED:
4669 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004670 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004671 }
4672 }
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004675 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 else
4677 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004678 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 n = 1;
4680 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004681 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004682 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004684 if (n == MAXLNUM)
4685 {
4686 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004687 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004688 goto error;
4689 }
4690 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004691
4692 if (addr_type == ADDR_TABS_RELATIVE)
4693 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004694 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004695 cmd = NULL;
4696 goto error;
4697 }
4698 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004699 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004700 lnum = compute_buffer_local_count(
4701 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004703 {
4704#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004705 // Relative line addressing: need to adjust for lines in a
4706 // closed fold after the first address.
4707 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4708 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004709 (void)hasFolding(lnum, NULL, &lnum);
4710#endif
4711 if (i == '-')
4712 lnum -= n;
4713 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004714 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004715 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004716 {
4717 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004718 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004719 goto error;
4720 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004721 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004722 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
4725 } while (*cmd == '/' || *cmd == '?');
4726
4727error:
4728 *ptr = cmd;
4729 return lnum;
4730}
4731
4732/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004733 * Set eap->line1 and eap->line2 to the whole range.
4734 * Used for commands with the EX_DFLALL flag and no range given.
4735 */
4736 static void
4737address_default_all(exarg_T *eap)
4738{
4739 eap->line1 = 1;
4740 switch (eap->addr_type)
4741 {
4742 case ADDR_LINES:
4743 case ADDR_OTHER:
4744 eap->line2 = curbuf->b_ml.ml_line_count;
4745 break;
4746 case ADDR_LOADED_BUFFERS:
4747 {
4748 buf_T *buf = firstbuf;
4749
4750 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4751 buf = buf->b_next;
4752 eap->line1 = buf->b_fnum;
4753 buf = lastbuf;
4754 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4755 buf = buf->b_prev;
4756 eap->line2 = buf->b_fnum;
4757 }
4758 break;
4759 case ADDR_BUFFERS:
4760 eap->line1 = firstbuf->b_fnum;
4761 eap->line2 = lastbuf->b_fnum;
4762 break;
4763 case ADDR_WINDOWS:
4764 eap->line2 = LAST_WIN_NR;
4765 break;
4766 case ADDR_TABS:
4767 eap->line2 = LAST_TAB_NR;
4768 break;
4769 case ADDR_TABS_RELATIVE:
4770 eap->line2 = 1;
4771 break;
4772 case ADDR_ARGUMENTS:
4773 if (ARGCOUNT == 0)
4774 eap->line1 = eap->line2 = 0;
4775 else
4776 eap->line2 = ARGCOUNT;
4777 break;
4778 case ADDR_QUICKFIX_VALID:
4779#ifdef FEAT_QUICKFIX
4780 eap->line2 = qf_get_valid_size(eap);
4781 if (eap->line2 == 0)
4782 eap->line2 = 1;
4783#endif
4784 break;
4785 case ADDR_NONE:
4786 case ADDR_UNSIGNED:
4787 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004788 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004789 break;
4790 }
4791}
4792
4793
4794/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004795 * Get flags from an Ex command argument.
4796 */
4797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004798get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004799{
4800 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4801 {
4802 if (*eap->arg == 'l')
4803 eap->flags |= EXFLAG_LIST;
4804 else if (*eap->arg == 'p')
4805 eap->flags |= EXFLAG_PRINT;
4806 else
4807 eap->flags |= EXFLAG_NR;
4808 eap->arg = skipwhite(eap->arg + 1);
4809 }
4810}
4811
4812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 * Function called for command which is Not Implemented. NI!
4814 */
4815 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004816ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817{
4818 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004819 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004820 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821}
4822
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004823#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824/*
4825 * Function called for script command which is Not Implemented. NI!
4826 * Skips over ":perl <<EOF" constructs.
4827 */
4828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004829ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
4831 if (!eap->skip)
4832 ex_ni(eap);
4833 else
4834 vim_free(script_get(eap, eap->arg));
4835}
4836#endif
4837
4838/*
4839 * Check range in Ex command for validity.
4840 * Return NULL when valid, error message when invalid.
4841 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004842 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004843invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004845 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if ( eap->line1 < 0
4848 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004849 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004850 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004851
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004852 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004853 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004854 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004855 {
4856 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004857 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004858#ifdef FEAT_DIFF
Yee Cheng Chind75ab0c2025-06-20 18:44:18 +02004859 + (eap->cmdidx == CMD_diffget ||
4860 eap->cmdidx == CMD_diffput)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004861#endif
4862 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004863 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004864 break;
4865 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004866 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004867 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004868 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004869 break;
4870 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004871 // Only a boundary check, not whether the buffers actually
4872 // exist.
4873 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004874 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004875 break;
4876 case ADDR_LOADED_BUFFERS:
4877 buf = firstbuf;
4878 while (buf->b_ml.ml_mfp == NULL)
4879 {
4880 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004881 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004882 buf = buf->b_next;
4883 }
4884 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004885 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004886 buf = lastbuf;
4887 while (buf->b_ml.ml_mfp == NULL)
4888 {
4889 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004890 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004891 buf = buf->b_prev;
4892 }
4893 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004894 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004895 break;
4896 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004897 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004898 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004899 break;
4900 case ADDR_TABS:
4901 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004902 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004903 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004904 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004905 case ADDR_OTHER:
4906 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004907 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004908 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004909#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004910 // No error for value that is too big, will use the last entry.
4911 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004912 {
4913 if (eap->addr_count == 0)
4914 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004915 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004916 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004917#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004918 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004919 case ADDR_QUICKFIX_VALID:
4920#ifdef FEAT_QUICKFIX
4921 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4922 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004923 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004924#endif
4925 break;
4926 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004927 case ADDR_NONE:
4928 // Will give an error elsewhere.
4929 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004930 }
4931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return NULL;
4933}
4934
4935/*
4936 * Correct the range for zero line number, if required.
4937 */
4938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004939correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004941 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 if (eap->line1 == 0)
4944 eap->line1 = 1;
4945 if (eap->line2 == 0)
4946 eap->line2 = 1;
4947 }
4948}
4949
Bram Moolenaar748bf032005-02-02 23:04:36 +00004950#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004951/*
4952 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4953 * pattern. Otherwise return eap->arg.
4954 */
4955 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004956skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004957{
4958 char_u *p = eap->arg;
4959
Bram Moolenaara37420f2006-02-04 22:37:47 +00004960 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4961 || eap->cmdidx == CMD_vimgrepadd
4962 || eap->cmdidx == CMD_lvimgrepadd
4963 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004964 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004965 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004966 if (p == NULL)
4967 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004968 }
4969 return p;
4970}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004971
4972/*
4973 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4974 * in the command line, so that things like % get expanded.
4975 */
4976 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004977replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004978{
4979 char_u *new_cmdline;
4980 char_u *program;
4981 char_u *pos;
4982 char_u *ptr;
4983 int len;
4984 int i;
4985
4986 /*
4987 * Don't do it when ":vimgrep" is used for ":grep".
4988 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004989 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4990 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4991 || eap->cmdidx == CMD_grepadd
4992 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004993 && !grep_internal(eap->cmdidx))
4994 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004995 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4996 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004997 {
4998 if (*curbuf->b_p_gp == NUL)
4999 program = p_gp;
5000 else
5001 program = curbuf->b_p_gp;
5002 }
5003 else
5004 {
5005 if (*curbuf->b_p_mp == NUL)
5006 program = p_mp;
5007 else
5008 program = curbuf->b_p_mp;
5009 }
5010
5011 p = skipwhite(p);
5012
5013 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005015 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005016 i = 1;
5017 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5018 ++i;
5019 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005020 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005021 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005022 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005023 ptr = new_cmdline;
5024 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5025 {
5026 i = (int)(pos - program);
5027 STRNCPY(ptr, program, i);
5028 STRCPY(ptr += i, p);
5029 ptr += len;
5030 program = pos + 2;
5031 }
5032 STRCPY(ptr, program);
5033 }
5034 else
5035 {
John Marriotted908f72024-04-18 22:46:56 +02005036 size_t program_len = STRLEN(program);
5037
5038 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005039 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005041 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005042 STRCPY(new_cmdline + program_len, " ");
5043 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005044 }
5045 msg_make(p);
5046
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005047 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005048 vim_free(*cmdlinep);
5049 *cmdlinep = new_cmdline;
5050 p = new_cmdline;
5051 }
5052 return p;
5053}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005054#endif
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056/*
5057 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005058 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 * Return FAIL for failure, OK otherwise.
5060 */
5061 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005062expand_filename(
5063 exarg_T *eap,
5064 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005065 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005067 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005069 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 char_u *p;
5071 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005072 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
Bram Moolenaar748bf032005-02-02 23:04:36 +00005074#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005075 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005076 p = skip_grep_pat(eap);
5077#else
5078 p = eap->arg;
5079#endif
5080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 /*
5082 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5083 * the file name contains a wildcard it should not cause expanding.
5084 * (it will be expanded anyway if there is a wildcard before replacing).
5085 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005086 has_wildcards = mch_has_wildcard(p);
5087 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005089#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005090 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005091 if (p[0] == '`' && p[1] == '=')
5092 {
5093 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005094 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005095 if (*p == '`')
5096 ++p;
5097 continue;
5098 }
5099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 /*
5101 * Quick check if this cannot be the start of a special string.
5102 * Also removes backslash before '%', '#' and '<'.
5103 */
5104 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5105 {
5106 ++p;
5107 continue;
5108 }
5109
5110 /*
5111 * Try to find a match at this position.
5112 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005113 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005114 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005115 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005117 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 p += srclen;
5120 continue;
5121 }
5122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005123 // Wildcards won't be expanded below, the replacement is taken
5124 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005125 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5126 {
5127 char_u *l = repl;
5128
5129 repl = expand_env_save(repl);
5130 vim_free(l);
5131 }
5132
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005133 // Need to escape white space et al. with a backslash.
5134 // Don't do this for:
5135 // - replacement that already has been escaped: "##"
5136 // - shell commands (may have to use quotes instead).
5137 // - non-unix systems when there is a single argument (spaces don't
5138 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005140 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 && eap->cmdidx != CMD_grep
5143 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005144 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005145 && eap->cmdidx != CMD_lgrep
5146 && eap->cmdidx != CMD_lgrepadd
5147 && eap->cmdidx != CMD_lmake
5148 && eap->cmdidx != CMD_make
5149 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005151 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152#endif
5153 )
5154 {
5155 char_u *l;
5156#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005157 // Don't escape a backslash here, because rem_backslash() doesn't
5158 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 static char_u *nobslash = (char_u *)" \t\"|";
5160# define ESCAPE_CHARS nobslash
5161#else
5162# define ESCAPE_CHARS escape_chars
5163#endif
5164
5165 for (l = repl; *l; ++l)
5166 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5167 {
5168 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5169 if (l != NULL)
5170 {
5171 vim_free(repl);
5172 repl = l;
5173 }
5174 break;
5175 }
5176 }
5177
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005178 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005179 if ((eap->usefilter || eap->cmdidx == CMD_bang
5180 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005181 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
5183 char_u *l;
5184
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005185 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (l != NULL)
5187 {
5188 vim_free(repl);
5189 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 }
5192
Mike Williams51024bb2024-05-30 07:46:30 +02005193 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 vim_free(repl);
5195 if (p == NULL)
5196 return FAIL;
5197 }
5198
5199 /*
5200 * One file argument: Expand wildcards.
5201 * Don't do this with ":r !command" or ":w !command".
5202 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005203 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
5205 /*
5206 * May do this twice:
5207 * 1. Replace environment variables.
5208 * 2. Replace any other wildcards, remove backslashes.
5209 */
5210 for (n = 1; n <= 2; ++n)
5211 {
5212 if (n == 2)
5213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 /*
5215 * Halve the number of backslashes (this is Vi compatible).
5216 * For Unix and OS/2, when wildcards are expanded, this is
5217 * done by ExpandOne() below.
5218 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005219#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (!has_wildcards)
5221#endif
5222 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
5224
5225 if (has_wildcards)
5226 {
5227 if (n == 1)
5228 {
5229 /*
5230 * First loop: May expand environment variables. This
5231 * can be done much faster with expand_env() than with
5232 * something else (e.g., calling a shell).
5233 * After expanding environment variables, check again
5234 * if there are still wildcards present.
5235 */
5236 if (vim_strchr(eap->arg, '$') != NULL
5237 || vim_strchr(eap->arg, '~') != NULL)
5238 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005239 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005240 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 has_wildcards = mch_has_wildcard(NameBuff);
5242 p = NameBuff;
5243 }
5244 else
5245 p = NULL;
5246 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005247 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
5249 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005250 int options = WILD_LIST_NOTFOUND
5251 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253 ExpandInit(&xpc);
5254 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005255 if (p_wic)
5256 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005258 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 if (p == NULL)
5260 return FAIL;
5261 }
5262 if (p != NULL)
5263 {
John Marriotted908f72024-04-18 22:46:56 +02005264 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005266 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 vim_free(p);
5268 }
5269 }
5270 }
5271 }
5272 return OK;
5273}
5274
5275/*
5276 * Replace part of the command line, keeping eap->cmd, eap->arg and
5277 * eap->nextcmd correct.
5278 * "src" points to the part that is to be replaced, of length "srclen".
5279 * "repl" is the replacement string.
5280 * Returns a pointer to the character after the replaced string.
5281 * Returns NULL for failure.
5282 */
5283 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005284repl_cmdline(
5285 exarg_T *eap,
5286 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005287 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005288 char_u *repl,
5289 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290{
John Marriotted908f72024-04-18 22:46:56 +02005291 size_t repllen;
5292 size_t taillen;
5293 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005295 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 /*
5298 * The new command line is build in new_cmdline[].
5299 * First allocate it.
5300 * Careful: a "+cmd" argument may have been NUL terminated.
5301 */
John Marriotted908f72024-04-18 22:46:56 +02005302 repllen = STRLEN(repl);
5303 taillen = STRLEN(src + srclen);
5304 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005305 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005306 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005307 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005308 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309
5310 /*
5311 * Copy the stuff before the expanded part.
5312 * Copy the expanded stuff.
5313 * Copy what came after the expanded part.
5314 * Copy the next commands, if there are any.
5315 */
John Marriotted908f72024-04-18 22:46:56 +02005316 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5317 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005318
John Marriotted908f72024-04-18 22:46:56 +02005319 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5320 new_cmdlinelen += repllen; // remember the end of the string
5321 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5322 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005324 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
John Marriotted908f72024-04-18 22:46:56 +02005326 new_cmdlinelen += taillen + 1;
5327 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5328 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5331 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5332 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5333 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5334 vim_free(*cmdlinep);
5335 *cmdlinep = new_cmdline;
5336
5337 return src;
5338}
5339
5340/*
5341 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005342 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 */
5344 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005345separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 char_u *p;
5348
Bram Moolenaar86b68352004-12-27 21:59:20 +00005349#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005350 p = skip_grep_pat(eap);
5351#else
5352 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005353#endif
5354
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005355 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
5357 if (*p == Ctrl_V)
5358 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005359 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005360 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005362 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005363 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005364 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 break;
5366 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005367
5368#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005369 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005370 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005371 {
5372 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005373 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005374 if (*p == NUL) // stop at NUL after CTRL-V
5375 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005376 }
5377#endif
5378
Bram Moolenaareda29c92022-10-02 12:59:00 +01005379 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 // :@" and :*" do not start a comment!
5381 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005382 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005383 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005384 && !(eap->argt & EX_NOTRLCOM)
5385 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5386 || p != eap->arg)
5387 && (eap->cmdidx != CMD_redir
5388 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005389 || (*p == '#'
5390 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005391 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005392 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005393 || (*p == '|'
5394 && eap->cmdidx != CMD_append
5395 && eap->cmdidx != CMD_change
5396 && eap->cmdidx != CMD_insert)
5397 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {
5399 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005400 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 * AND 'b' is present in 'cpoptions'.
5402 */
5403 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005404 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005406 if (!keep_backslash)
5407 {
5408 STRMOVE(p - 1, p); // remove the '\'
5409 --p;
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
5412 else
5413 {
5414 eap->nextcmd = check_nextcmd(p);
5415 *p = NUL;
5416 break;
5417 }
5418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005421 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 del_trailing_spaces(eap->arg);
5423}
5424
5425/*
5426 * get + command from ex argument
5427 */
5428 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005429getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 char_u *arg = *argp;
5432 char_u *command = NULL;
5433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005434 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
5436 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005437 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 command = dollar_command;
5439 else
5440 {
5441 command = arg;
5442 arg = skip_cmd_arg(command, TRUE);
5443 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005444 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005447 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 *argp = arg;
5449 }
5450 return command;
5451}
5452
5453/*
5454 * Find end of "+command" argument. Skip over "\ " and "\\".
5455 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005456 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005457skip_cmd_arg(
5458 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005459 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460{
5461 while (*p && !vim_isspace(*p))
5462 {
5463 if (*p == '\\' && p[1] != NUL)
5464 {
5465 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005466 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 else
5468 ++p;
5469 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005470 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 return p;
5473}
5474
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005475 int
5476get_bad_opt(char_u *p, exarg_T *eap)
5477{
5478 if (STRICMP(p, "keep") == 0)
5479 eap->bad_char = BAD_KEEP;
5480 else if (STRICMP(p, "drop") == 0)
5481 eap->bad_char = BAD_DROP;
5482 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5483 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005484 else
5485 return FAIL;
5486 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005487}
5488
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005490 * Function given to ExpandGeneric() to obtain the list of bad= names.
5491 */
5492 static char_u *
5493get_bad_name(expand_T *xp UNUSED, int idx)
5494{
5495 // Note: Keep this in sync with getargopt.
5496 static char *(p_bad_values[]) =
5497 {
5498 "?",
5499 "keep",
5500 "drop",
5501 };
5502
John Marriotted908f72024-04-18 22:46:56 +02005503 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5504 return NULL;
5505
5506 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005507}
5508
5509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 * Get "++opt=arg" argument.
5511 * Return FAIL or OK.
5512 */
5513 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005514getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 char_u *arg = eap->arg + 2;
5517 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005518 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005521 // Note: Keep this in sync with get_argopt_name.
5522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005523 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5525 {
5526 if (*arg == 'n')
5527 {
5528 arg += 2;
5529 eap->force_bin = FORCE_NOBIN;
5530 }
5531 else
5532 eap->force_bin = FORCE_BIN;
5533 if (!checkforcmd(&arg, "binary", 3))
5534 return FAIL;
5535 eap->arg = skipwhite(arg);
5536 return OK;
5537 }
5538
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005539 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005540 if (STRNCMP(arg, "edit", 4) == 0)
5541 {
5542 eap->read_edit = TRUE;
5543 eap->arg = skipwhite(arg + 4);
5544 return OK;
5545 }
5546
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 if (STRNCMP(arg, "ff", 2) == 0)
5548 {
5549 arg += 2;
5550 pp = &eap->force_ff;
5551 }
5552 else if (STRNCMP(arg, "fileformat", 10) == 0)
5553 {
5554 arg += 10;
5555 pp = &eap->force_ff;
5556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 else if (STRNCMP(arg, "enc", 3) == 0)
5558 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005559 if (STRNCMP(arg, "encoding", 8) == 0)
5560 arg += 8;
5561 else
5562 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 pp = &eap->force_enc;
5564 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005565 else if (STRNCMP(arg, "bad", 3) == 0)
5566 {
5567 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005568 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
5571 if (pp == NULL || *arg != '=')
5572 return FAIL;
5573
5574 ++arg;
5575 *pp = (int)(arg - eap->cmd);
5576 arg = skip_cmd_arg(arg, FALSE);
5577 eap->arg = skipwhite(arg);
5578 *arg = NUL;
5579
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 if (pp == &eap->force_ff)
5581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5583 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005584 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005586 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005588 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5590 *p = TOLOWER_ASC(*p);
5591 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005592 else
5593 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005594 // Check ++bad= argument. Must be a single-byte character, "keep" or
5595 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005596 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005597 return FAIL;
5598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
5600 return OK;
5601}
5602
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005603/*
5604 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5605 */
5606 static char_u *
5607get_argopt_name(expand_T *xp UNUSED, int idx)
5608{
5609 // Note: Keep this in sync with getargopt.
5610 static char *(p_opt_values[]) =
5611 {
5612 "fileformat=",
5613 "encoding=",
5614 "binary",
5615 "nobinary",
5616 "bad=",
5617 "edit",
5618 };
5619
John Marriotted908f72024-04-18 22:46:56 +02005620 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5621 return NULL;
5622
5623 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005624}
5625
5626/*
5627 * Command-line expansion for ++opt=name.
5628 */
5629 int
5630expand_argopt(
5631 char_u *pat,
5632 expand_T *xp,
5633 regmatch_T *rmp,
5634 char_u ***matches,
5635 int *numMatches)
5636{
5637 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5638 {
5639 char_u *(*cb)(expand_T *, int) = NULL;
5640
5641 char_u *name_end = xp->xp_pattern - 1;
5642 if (name_end - xp->xp_line >= 2
5643 && STRNCMP(name_end - 2, "ff", 2) == 0)
5644 cb = get_fileformat_name;
5645 else if (name_end - xp->xp_line >= 10
5646 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5647 cb = get_fileformat_name;
5648 else if (name_end - xp->xp_line >= 3
5649 && STRNCMP(name_end - 3, "enc", 3) == 0)
5650 cb = get_encoding_name;
5651 else if (name_end - xp->xp_line >= 8
5652 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5653 cb = get_encoding_name;
5654 else if (name_end - xp->xp_line >= 3
5655 && STRNCMP(name_end - 3, "bad", 3) == 0)
5656 cb = get_bad_name;
5657
5658 if (cb != NULL)
5659 {
5660 return ExpandGeneric(
5661 pat,
5662 xp,
5663 rmp,
5664 matches,
5665 numMatches,
5666 cb,
5667 FALSE);
5668 }
5669 return FAIL;
5670 }
5671
5672 // Special handling of "ff" which acts as a short form of
5673 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005674 if (xp->xp_pattern_len == 2
5675 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005676 {
5677 *matches = ALLOC_MULT(char_u *, 1);
5678 if (*matches == NULL)
5679 return FAIL;
5680 *numMatches = 1;
5681 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5682 return OK;
5683 }
5684
5685 return ExpandGeneric(
5686 pat,
5687 xp,
5688 rmp,
5689 matches,
5690 numMatches,
5691 get_argopt_name,
5692 FALSE);
5693}
5694
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005696ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005699 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 * directory for security reasons.
5701 */
5702 if (secure)
5703 {
5704 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005705 eap->errmsg =
5706 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005709 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 else
5711 do_augroup(eap->arg, eap->forceit);
5712}
5713
5714/*
5715 * ":doautocmd": Apply the automatic commands to the current buffer.
5716 */
5717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005720 char_u *arg = eap->arg;
5721 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005722 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005723
Bram Moolenaar1610d052016-06-09 22:53:01 +02005724 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005725 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005726 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005727 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730/*
5731 * :[N]bunload[!] [N] [bufname] unload buffer
5732 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5733 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5734 */
5735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005738 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005739 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 eap->errmsg = do_bufdel(
5741 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5742 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5743 : DOBUF_UNLOAD, eap->arg,
5744 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5745}
5746
5747/*
5748 * :[N]buffer [N] to buffer N
5749 * :[N]sbuffer [N] to buffer N
5750 */
5751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005752ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005754 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005755 return;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01005756 do_exbuffer(eap);
5757}
5758
5759/*
5760 * ":buffer" command and alike.
5761 */
5762 static void
5763do_exbuffer(exarg_T *eap)
5764{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005766 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 else
5768 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005769 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5771 else
5772 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005773 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005774 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776}
5777
5778/*
5779 * :[N]bmodified [N] to next mod. buffer
5780 * :[N]sbmodified [N] to next mod. buffer
5781 */
5782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005783ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
5785 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005786 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005787 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788}
5789
5790/*
5791 * :[N]bnext [N] to next buffer
5792 * :[N]sbnext [N] split and to next buffer
5793 */
5794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005795ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005797 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005798 return;
5799
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005801 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005802 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803}
5804
5805/*
5806 * :[N]bNext [N] to previous buffer
5807 * :[N]bprevious [N] to previous buffer
5808 * :[N]sbNext [N] split and to previous buffer
5809 * :[N]sbprevious [N] split and to previous buffer
5810 */
5811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005812ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005814 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005815 return;
5816
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005818 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005819 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820}
5821
5822/*
5823 * :brewind to first buffer
5824 * :bfirst to first buffer
5825 * :sbrewind split and to first buffer
5826 * :sbfirst split and to first buffer
5827 */
5828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005829ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005831 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005832 return;
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005835 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005836 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837}
5838
5839/*
5840 * :blast to last buffer
5841 * :sblast split and to last buffer
5842 */
5843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005844ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005846 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005847 return;
5848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005850 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005851 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
Bram Moolenaar7a092242020-04-16 22:10:49 +02005854/*
5855 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005856 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005859ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005861 int comment_char = '"';
5862
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005863 if (in_vim9script())
5864 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005865 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005866}
5867
5868/*
5869 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5870 * to "cmd_start" or has a white space character before it.
5871 */
5872 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005873ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005874{
5875 int c = *cmd;
5876
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005877 if (c == NUL || c == '|' || c == '\n')
5878 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005879 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005880 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5881 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005882 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005883 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884}
5885
5886#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5887 || defined(PROTO)
5888/*
5889 * Return the next command, after the first '|' or '\n'.
5890 * Return NULL if not found.
5891 */
5892 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005893find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 while (*p != '|' && *p != '\n')
5896 {
5897 if (*p == NUL)
5898 return NULL;
5899 ++p;
5900 }
5901 return (p + 1);
5902}
5903#endif
5904
5905/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005906 * Check if *p is a separator between Ex commands, skipping over white space.
5907 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 */
5909 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005910check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005912 char_u *s = skipwhite(p);
5913
5914 if (*s == '|' || *s == '\n')
5915 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 else
5917 return NULL;
5918}
5919
5920/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005921 * If "eap->nextcmd" is not set, check for a next command at "p".
5922 */
5923 void
5924set_nextcmd(exarg_T *eap, char_u *arg)
5925{
5926 char_u *p = check_nextcmd(arg);
5927
5928 if (eap->nextcmd == NULL)
5929 eap->nextcmd = p;
5930 else if (p != NULL)
5931 // cannot use "| command" inside a {} block
5932 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5933}
5934
5935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 * - if there are more files to edit
5937 * - and this is the last window
5938 * - and forceit not used
5939 * - and not repeated twice on a row
5940 * return FAIL and give error message if 'message' TRUE
5941 * return OK otherwise
5942 */
5943 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005944check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005945 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005946 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947{
5948 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5949
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005950 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005951 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 {
5953 if (message)
5954 {
5955#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005956 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5957 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005959 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005961 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5962 NGETTEXT("%d more file to edit. Quit anyway?",
5963 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5965 return OK;
5966 return FAIL;
5967 }
5968#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005969 semsg(NGETTEXT(e_nr_more_file_to_edit,
5970 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005971 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 }
5973 return FAIL;
5974 }
5975 return OK;
5976}
5977
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978/*
5979 * Function given to ExpandGeneric() to obtain the list of command names.
5980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005982get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005985 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005986 // the following are no real commands
5987 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02005988 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
Christian Brabandt70a11a62024-07-26 19:13:55 +02005989 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 return cmdnames[idx].cmd_name;
5991}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005994ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
Bram Moolenaare6850792010-05-14 15:28:44 +02005996 if (*eap->arg == NUL)
5997 {
5998#ifdef FEAT_EVAL
5999 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6000 char_u *p = NULL;
6001
6002 if (expr != NULL)
6003 {
6004 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006005 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02006006 --emsg_off;
6007 vim_free(expr);
6008 }
6009 if (p != NULL)
6010 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006011 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02006012 vim_free(p);
6013 }
6014 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006015 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006016#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006017 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006018#endif
6019 }
6020 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006021 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006022
6023#ifdef FEAT_VTP
6024 else if (has_vtp_working())
6025 {
6026 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006027 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006028 redrawcmd();
6029 }
6030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031}
6032
6033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006034ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006037 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 do_highlight(eap->arg, eap->forceit, FALSE);
6039}
6040
6041
6042/*
6043 * Call this function if we thought we were going to exit, but we won't
6044 * (because of an error). May need to restore the terminal mode.
6045 */
6046 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006047not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
6049 exiting = FALSE;
6050 settmode(TMODE_RAW);
6051}
6052
Bram Moolenaar3552e742021-05-29 12:21:58 +02006053 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006054before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6055{
6056 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6057
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006058 // Bail out when autocommands closed the window.
6059 // Refuse to quit when the buffer in the last window is being closed (can
6060 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006061 if (!win_valid(wp)
6062 || curbuf_locked()
6063 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6064 return TRUE;
6065
6066 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6067 {
6068 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006069 // Refuse to quit when locked or when the window was closed or the
6070 // buffer in the last window is being closed (can only happen in
6071 // autocommands).
6072 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006073 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6074 return TRUE;
6075 }
6076
6077 return FALSE;
6078}
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006081 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006082 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006083 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006085 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006086ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006088 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if (cmdwin_type != 0)
6091 {
6092 cmdwin_result = Ctrl_C;
6093 return;
6094 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006095 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006096 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006097 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006098 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006099 return;
6100 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006101 if (eap->addr_count > 0)
6102 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006103 int wnr = eap->line2;
6104
6105 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6106 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006107 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006108 }
6109 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006110 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006112 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006113 if (curbuf_locked())
6114 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006116 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006117 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006118 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
6120#ifdef FEAT_NETBEANS_INTG
6121 netbeansForcedQuit = eap->forceit;
6122#endif
6123
6124 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006125 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 */
6127 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6128 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006129 if ((!buf_hide(wp->w_buffer)
6130 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006131 | (eap->forceit ? CCGD_FORCEIT : 0)
6132 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006134 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
6136 not_exiting();
6137 }
6138 else
6139 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006140 // quit last window
6141 // Note: only_one_window() returns true, even so a help window is
6142 // still open. In that case only quit, if no address has been
6143 // specified. Example:
6144 // :h|wincmd w|1q - don't quit
6145 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006146 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006148 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006149#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006151#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006152 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006153 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 }
6155}
6156
6157/*
6158 * ":cquit".
6159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006161ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006163 // this does not always pass on the exit code to the Manx compiler. why?
6164 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165}
6166
6167/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006168 * Do preparations for "qall" and "wqall".
6169 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006171 int
6172before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (cmdwin_type != 0)
6175 {
6176 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006177 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 else
6179 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006180 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006183 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006184 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006185 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006186 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006187 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006188 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006189
6190 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006191 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006192
Bram Moolenaar411da642023-05-10 16:53:27 +01006193 return OK;
6194}
6195
6196/*
6197 * ":qall": try to quit all windows
6198 */
6199 static void
6200ex_quit_all(exarg_T *eap)
6201{
6202 if (before_quit_all(eap) == FAIL)
6203 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006205 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 getout(0);
6207 not_exiting();
6208}
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210/*
6211 * ":close": close current window, unless it is the last one
6212 */
6213 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006214ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006216 win_T *win;
6217 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006219 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006221 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006222 {
6223 if (eap->addr_count == 0)
6224 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006225 else
6226 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006227 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006228 {
6229 winnr++;
6230 if (winnr == eap->line2)
6231 break;
6232 }
6233 if (win == NULL)
6234 win = lastwin;
6235 ex_win_close(eap->forceit, win, NULL);
6236 }
6237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238}
6239
Bram Moolenaar4033c552017-09-16 20:54:51 +02006240#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006241/*
6242 * ":pclose": Close any preview window.
6243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006245ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006246{
6247 win_T *win;
6248
Bram Moolenaar79648732019-07-18 21:43:07 +02006249 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006250 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251 if (win->w_p_pvw)
6252 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006253 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006254 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006255 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006256# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006257 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006258 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006259# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006260}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006261#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006262
Bram Moolenaarf740b292006-02-16 22:11:02 +00006263/*
6264 * Close window "win" and take care of handling closing the last window for a
6265 * modified buffer.
6266 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006268ex_win_close(
6269 int forceit,
6270 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006271 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 int need_hide;
6274 buf_T *buf = win->w_buffer;
6275
Bram Moolenaarcf844172020-06-26 19:44:06 +02006276 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006277 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006278 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006279 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006280 return;
6281 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006282 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006283 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006284
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006286 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006288#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006289 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006291# ifdef FEAT_TERMINAL
6292 if (term_job_running(buf->b_term))
6293 {
6294 if (term_confirm_stop(buf) == FAIL)
6295 return;
6296 // Manually kill the terminal here because this command will
6297 // hide it otherwise.
6298 free_terminal(buf);
6299 need_hide = FALSE;
6300 }
6301 else
6302# endif
6303 {
6304 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006305
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006306 set_bufref(&bufref, buf);
6307 dialog_changed(buf, FALSE);
6308 if (bufref_valid(&bufref) && bufIsChanged(buf))
6309 return;
6310 need_hide = FALSE;
6311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 }
6313 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006316 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 return;
6318 }
6319 }
6320
Bram Moolenaar4033c552017-09-16 20:54:51 +02006321#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006323#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006324
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006325 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006326 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006327 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006328 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006329 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330}
6331
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006333 * Handle the argument for a tabpage related ex command.
6334 * Returns a tabpage number.
6335 * When an error is encountered then eap->errmsg is set.
6336 */
6337 static int
6338get_tabpage_arg(exarg_T *eap)
6339{
6340 int tab_number;
6341 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6342
6343 if (eap->arg && *eap->arg != NUL)
6344 {
6345 char_u *p = eap->arg;
6346 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006347 int relative = 0; // argument +N/-N means: go to N places to the
6348 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006349
6350 if (*p == '-')
6351 {
6352 relative = -1;
6353 p++;
6354 }
6355 else if (*p == '+')
6356 {
6357 relative = 1;
6358 p++;
6359 }
6360
6361 p_save = p;
6362 tab_number = getdigits(&p);
6363
6364 if (relative == 0)
6365 {
6366 if (STRCMP(p, "$") == 0)
6367 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006368 else if (STRCMP(p, "#") == 0)
6369 if (valid_tabpage(lastused_tabpage))
6370 tab_number = tabpage_index(lastused_tabpage);
6371 else
6372 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006373 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6374 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006375 tab_number = 0;
6376 goto theend;
6377 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006378 else if (p == p_save || *p_save == '-' || *p != NUL
6379 || tab_number > LAST_TAB_NR)
6380 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006381 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006382 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006383 goto theend;
6384 }
6385 }
6386 else
6387 {
6388 if (*p_save == NUL)
6389 tab_number = 1;
6390 else if (p == p_save || *p_save == '-' || *p != NUL
6391 || tab_number == 0)
6392 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006393 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006394 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006395 goto theend;
6396 }
6397 tab_number = tab_number * relative + tabpage_index(curtab);
6398 if (!unaccept_arg0 && relative == -1)
6399 --tab_number;
6400 }
6401 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006402 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006403 }
6404 else if (eap->addr_count > 0)
6405 {
6406 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006407 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006408 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006409 tab_number = 0;
6410 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006411 else
6412 {
6413 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006414 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006415 {
zeertzjq076faac2024-03-25 16:41:06 +01006416 char_u *cmdp = eap->cmd;
6417
6418 while (--cmdp > *eap->cmdlinep
6419 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6420 ;
6421 if (*cmdp == '-')
6422 {
6423 --tab_number;
6424 if (tab_number < unaccept_arg0)
6425 eap->errmsg = _(e_invalid_range);
6426 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006427 }
6428 }
6429 }
6430 else
6431 {
6432 switch (eap->cmdidx)
6433 {
6434 case CMD_tabnext:
6435 tab_number = tabpage_index(curtab) + 1;
6436 if (tab_number > LAST_TAB_NR)
6437 tab_number = 1;
6438 break;
6439 case CMD_tabmove:
6440 tab_number = LAST_TAB_NR;
6441 break;
6442 default:
6443 tab_number = tabpage_index(curtab);
6444 }
6445 }
6446
6447theend:
6448 return tab_number;
6449}
6450
6451/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006452 * ":tabclose": close current tab page, unless it is the last one.
6453 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 */
6455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006456ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006458 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006459 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006460
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006461 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006462 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006463 cmdwin_result = K_IGNORE;
6464 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006465 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006466
6467 if (first_tabpage->tp_next == NULL)
6468 {
6469 emsg(_(e_cannot_close_last_tab_page));
6470 return;
6471 }
6472
6473 if (window_layout_locked(CMD_tabclose))
6474 return;
6475
6476 tab_number = get_tabpage_arg(eap);
6477 if (eap->errmsg != NULL)
6478 return;
6479
6480 tp = find_tabpage(tab_number);
6481 if (tp == NULL)
6482 {
6483 beep_flush();
6484 return;
6485 }
6486 if (tp != curtab)
6487 {
6488 tabpage_close_other(tp, eap->forceit);
6489 return;
6490 }
6491 else if (!text_locked() && !curbuf_locked())
6492 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006493}
6494
6495/*
6496 * ":tabonly": close all tab pages except the current one
6497 */
6498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006499ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006500{
6501 tabpage_T *tp;
6502 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006503 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006504
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006505 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006506 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006507 cmdwin_result = K_IGNORE;
6508 return;
6509 }
6510
6511 if (first_tabpage->tp_next == NULL)
6512 {
6513 msg(_("Already only one tab page"));
6514 return;
6515 }
6516
6517 if (window_layout_locked(CMD_tabonly))
6518 return;
6519
6520 tab_number = get_tabpage_arg(eap);
6521 if (eap->errmsg != NULL)
6522 return;
6523
6524 goto_tabpage(tab_number);
6525 // Repeat this up to a 1000 times, because autocommands may
6526 // mess up the lists.
6527 for (done = 0; done < 1000; ++done)
6528 {
6529 FOR_ALL_TABPAGES(tp)
6530 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006531 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006532 tabpage_close_other(tp, eap->forceit);
6533 // if we failed to close it quit
6534 if (valid_tabpage(tp))
6535 done = 1000;
6536 // start over, "tp" is now invalid
6537 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006538 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006539 if (first_tabpage->tp_next == NULL)
6540 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
6544/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006545 * Close the current tab page.
6546 */
6547 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006548tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006549{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006550 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006551 return;
6552
Jim Zhoubcf66e02025-03-16 20:24:57 +01006553 trigger_tabclosedpre(curtab, TRUE);
6554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006555 // First close all the windows but the current one. If that worked then
6556 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006557 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006558 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006559 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006560 ex_win_close(forceit, curwin, NULL);
6561# ifdef FEAT_GUI
6562 need_mouse_correct = TRUE;
6563# endif
6564}
6565
6566/*
6567 * Close tab page "tp", which is not the current tab page.
6568 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006569 * Also takes care of the tab pages line disappearing when closing the
6570 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006571 */
6572 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006573tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006574{
6575 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006576 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006577
Jim Zhoubcf66e02025-03-16 20:24:57 +01006578 trigger_tabclosedpre(tp, TRUE);
6579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006580 // Limit to 1000 windows, autocommands may add a window while we close
6581 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006582 while (++done < 1000)
6583 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006584 wp = tp->tp_firstwin;
6585 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006587 // Autocommands may delete the tab page under our fingers and we may
6588 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006589 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006590 break;
6591 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006592
Bram Moolenaar29323592016-07-24 22:04:11 +02006593 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006594}
6595
6596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 * ":only".
6598 */
6599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006600ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006602 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006603 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604# ifdef FEAT_GUI
6605 need_mouse_correct = TRUE;
6606# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006607 if (eap->addr_count > 0)
6608 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006609 win_T *wp;
6610 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006611 for (wp = firstwin; --wnr > 0; )
6612 {
6613 if (wp->w_next == NULL)
6614 break;
6615 else
6616 wp = wp->w_next;
6617 }
6618 win_goto(wp);
6619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 close_others(TRUE, eap->forceit);
6621}
6622
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006624ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006626 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006627 if (eap->skip)
6628 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006629
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006630 if (window_layout_locked(CMD_hide))
6631 return;
6632#ifdef FEAT_GUI
6633 need_mouse_correct = TRUE;
6634#endif
6635 if (eap->addr_count == 0)
6636 win_close(curwin, FALSE); // don't free buffer
6637 else
6638 {
6639 int winnr = 0;
6640 win_T *win;
6641
6642 FOR_ALL_WINDOWS(win)
6643 {
6644 winnr++;
6645 if (winnr == eap->line2)
6646 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006648 if (win == NULL)
6649 win = lastwin;
6650 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 }
6652}
6653
6654/*
6655 * ":stop" and ":suspend": Suspend Vim.
6656 */
dbivolaruab16ad32021-12-29 19:41:47 +00006657 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006658ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 /*
6661 * Disallow suspending for "rvim".
6662 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006663 if (check_restricted())
6664 return;
6665
6666 if (!eap->forceit)
6667 autowrite_all();
6668 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6669 windgoto((int)Rows - 1, 0);
6670 out_char('\n');
6671 out_flush();
6672 stoptermcap();
6673 out_flush(); // needed for SUN to restore xterm buffer
6674 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6675 ui_suspend(); // call machine specific function
6676 maketitle();
6677 resettitle(); // force updating the title
6678 starttermcap();
6679 scroll_start(); // scroll screen before redrawing
6680 redraw_later_clear();
6681 shell_resized(); // may have resized window
6682 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683}
6684
6685/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006686 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 */
6688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006689ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006691#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006692 if (not_in_vim9(eap) == FAIL)
6693 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 if (cmdwin_type != 0)
6696 {
6697 cmdwin_result = Ctrl_C;
6698 return;
6699 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006700 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006701 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006702 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006703 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006704 return;
6705 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006706
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006708 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 */
6710 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6711 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006712
6713 // Write the buffer for ":wq" or when it was changed.
6714 // Trigger QuitPre and ExitPre.
6715 // Check if we can exit now, after autocommands have changed things.
6716 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6717 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006719 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {
6721 not_exiting();
6722 }
6723 else
6724 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006725 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006727 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728# ifdef FEAT_GUI
6729 need_mouse_correct = TRUE;
6730# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006731 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006732 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 }
6734}
6735
6736/*
6737 * ":print", ":list", ":number".
6738 */
6739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006740ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006742 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006743 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006744 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006746 for ( ;!got_int; ui_breakcheck())
6747 {
6748 print_line(eap->line1,
6749 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6750 || (eap->flags & EXFLAG_NR)),
6751 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6752 if (++eap->line1 > eap->line2)
6753 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006754 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006755 }
6756 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006757 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006758 curwin->w_cursor.lnum = eap->line2;
6759 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 }
6761
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763}
6764
6765#ifdef FEAT_BYTEOFF
6766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 goto_byte(eap->line2);
6770}
6771#endif
6772
6773/*
6774 * ":shell".
6775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006777ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 do_shell(NULL, 0);
6780}
6781
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006782#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006784static int drop_busy = FALSE;
6785static int drop_filec;
6786static char_u **drop_filev = NULL;
6787static int drop_split;
6788static void (*drop_callback)(void *);
6789static void *drop_cookie;
6790
6791 static void
6792handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 exarg_T ea;
6795 int save_msg_scroll = msg_scroll;
6796
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006797 // Setting the argument list may cause screen updates and being called
6798 // recursively. Avoid that by setting drop_busy.
6799 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006801 // Check whether the current buffer is changed. If so, we will need
6802 // to split the current window or data could be lost.
6803 // We don't need to check if the 'hidden' option is set, as in this
6804 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006805 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
6807 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006808 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 --emsg_off;
6810 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006811 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (win_split(0, 0) == FAIL)
6814 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006815 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006817 // When splitting the window, create a new alist. Otherwise the
6818 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 alist_unlink(curwin->w_alist);
6820 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
6822
6823 /*
6824 * Set up the new argument list.
6825 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006826 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827
6828 /*
6829 * Move to the first file.
6830 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006831 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006832 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 ea.cmd = (char_u *)"next";
6834 do_argfile(&ea, 0);
6835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006836 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6837 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 need_start_insertmode = FALSE;
6839
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006840 // Restore msg_scroll, otherwise a following command may cause scrolling
6841 // unexpectedly. The screen will be redrawn by the caller, thus
6842 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006844
6845 if (drop_callback != NULL)
6846 drop_callback(drop_cookie);
6847
6848 drop_filev = NULL;
6849 drop_busy = FALSE;
6850}
6851
6852/*
6853 * Handle a file drop. The code is here because a drop is *nearly* like an
6854 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006855 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006856 * code is very similar to :args and hence needs access to a lot of the static
6857 * functions in this file.
6858 *
6859 * The "filev" list must have been allocated using alloc(), as should each item
6860 * in the list. This function takes over responsibility for freeing the "filev"
6861 * list.
6862 */
6863 void
6864handle_drop(
6865 int filec, // the number of files dropped
6866 char_u **filev, // the list of files dropped
6867 int split, // force splitting the window
6868 void (*callback)(void *), // to be called after setting the argument
6869 // list
6870 void *cookie) // argument for "callback" (allocated)
6871{
6872 // Cannot handle recursive drops, finish the pending one.
6873 if (drop_busy)
6874 {
6875 FreeWild(filec, filev);
6876 vim_free(cookie);
6877 return;
6878 }
6879
6880 // When calling handle_drop() more than once in a row we only use the last
6881 // one.
6882 if (drop_filev != NULL)
6883 {
6884 FreeWild(drop_filec, drop_filev);
6885 vim_free(drop_cookie);
6886 }
6887
6888 drop_filec = filec;
6889 drop_filev = filev;
6890 drop_split = split;
6891 drop_callback = callback;
6892 drop_cookie = cookie;
6893
6894 // Postpone this when:
6895 // - editing the command line
6896 // - not possible to change the current buffer
6897 // - updating the screen
6898 // As it may change buffers and window structures that are in use and cause
6899 // freed memory to be used.
6900 if (text_locked() || curbuf_locked() || updating_screen)
6901 return;
6902
6903 handle_drop_internal();
6904}
6905
6906/*
6907 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6908 * reset: Handle a postponed drop.
6909 */
6910 void
6911handle_any_postponed_drop(void)
6912{
6913 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006914 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006915 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916}
6917#endif
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 * ":preserve".
6921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006923ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006925 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 ml_preserve(curbuf, TRUE);
6927}
6928
6929/*
6930 * ":recover".
6931 */
6932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006935 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006937 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6938 | CCGD_MULTWIN
6939 | (eap->forceit ? CCGD_FORCEIT : 0)
6940 | CCGD_EXCMD)
6941
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 && (*eap->arg == NUL
6943 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006944 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 recoverymode = FALSE;
6946}
6947
6948/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006949 * Command modifier used in a wrong way. Also for other commands that can't
6950 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 */
6952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006953ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006955 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956}
6957
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006958#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006959
6960// callback function for 'findfunc'
6961static callback_T ffu_cb;
6962
6963 static callback_T *
6964get_findfunc_callback(void)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006965{
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006966 return *curbuf->b_p_ffu != NUL ? &curbuf->b_ffu_cb : &ffu_cb;
6967}
6968
zeertzjq6eda2692024-11-03 09:23:33 +01006969/*
6970 * Call 'findfunc' to obtain a list of file names.
6971 */
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006972 static list_T *
6973call_findfunc(char_u *pat, int cmdcomplete)
6974{
6975 typval_T args[3];
6976 callback_T *cb;
6977 typval_T rettv;
6978 int retval;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006979 sctx_T saved_sctx = current_sctx;
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006980 sctx_T *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006981
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006982 args[0].v_type = VAR_STRING;
6983 args[0].vval.v_string = pat;
6984 args[1].v_type = VAR_BOOL;
6985 args[1].vval.v_number = cmdcomplete;
6986 args[2].v_type = VAR_UNKNOWN;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006987
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006988 // Lock the text to prevent weird things from happening. Also disallow
6989 // switching to another window, it should not be needed and may end up in
6990 // Insert mode in another buffer.
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006991 ++textlock;
6992
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006993 ctx = get_option_sctx("findfunc");
6994 if (ctx != NULL)
6995 current_sctx = *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006996
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006997 cb = get_findfunc_callback();
6998 retval = call_callback(cb, -1, &rettv, 2, args);
6999
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007000 current_sctx = saved_sctx;
7001
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007002 --textlock;
7003
7004 list_T *retlist = NULL;
7005
7006 if (retval == OK)
7007 {
7008 if (rettv.v_type == VAR_LIST)
7009 retlist = list_copy(rettv.vval.v_list, FALSE, FALSE, get_copyID());
7010 else
7011 emsg(_(e_invalid_return_type_from_findfunc));
7012
7013 clear_tv(&rettv);
7014 }
7015
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007016 return retlist;
7017}
7018
7019/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007020 * Find file names matching "pat" using 'findfunc' and return it in "files".
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007021 * Used for expanding the :find, :sfind and :tabfind command argument.
7022 * Returns OK on success and FAIL otherwise.
7023 */
7024 int
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007025expand_findfunc(char_u *pat, char_u ***files, int *numMatches)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007026{
7027 list_T *l;
7028 int len;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007029
7030 *numMatches = 0;
7031 *files = NULL;
7032
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007033 l = call_findfunc(pat, VVAL_TRUE);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007034
7035 if (l == NULL)
7036 return FAIL;
7037
7038 len = list_len(l);
7039 if (len == 0) // empty List
7040 return FAIL;
7041
7042 *files = ALLOC_MULT(char_u *, len);
7043 if (*files == NULL)
7044 return FAIL;
7045
7046 // Copy all the List items
7047 listitem_T *li;
7048 int idx = 0;
7049 FOR_ALL_LIST_ITEMS(l, li)
7050 {
7051 if (li->li_tv.v_type == VAR_STRING)
7052 {
7053 (*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
7054 idx++;
7055 }
7056 }
7057
7058 *numMatches = idx;
7059 list_free(l);
7060
7061 return OK;
7062}
7063
7064/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007065 * Use 'findfunc' to find file 'findarg'. The 'count' argument is used to find
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007066 * the n'th matching file.
7067 */
7068 static char_u *
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007069findfunc_find_file(char_u *findarg, int findarg_len, int count)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007070{
7071 list_T *fname_list;
7072 char_u *ret_fname = NULL;
7073 char_u cc;
7074 int fname_count;
7075
7076 cc = findarg[findarg_len];
7077 findarg[findarg_len] = NUL;
7078
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007079 fname_list = call_findfunc(findarg, VVAL_FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007080 fname_count = list_len(fname_list);
7081
7082 if (fname_count == 0)
7083 semsg(_(e_cant_find_file_str_in_path), findarg);
7084 else
7085 {
7086 if (count > fname_count)
7087 semsg(_(e_no_more_file_str_found_in_path), findarg);
7088 else
7089 {
7090 listitem_T *li = list_find(fname_list, count - 1);
7091 if (li != NULL && li->li_tv.v_type == VAR_STRING)
7092 ret_fname = vim_strsave(li->li_tv.vval.v_string);
7093 }
7094 }
7095
7096 if (fname_list != NULL)
7097 list_free(fname_list);
7098
7099 findarg[findarg_len] = cc;
7100
7101 return ret_fname;
7102}
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007103
7104/*
7105 * Process the 'findfunc' option value.
7106 * Returns NULL on success and an error message on failure.
7107 */
7108 char *
7109did_set_findfunc(optset_T *args UNUSED)
7110{
7111 int retval;
7112
zeertzjq6eda2692024-11-03 09:23:33 +01007113 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007114 // buffer-local option set
7115 retval = option_set_callback_func(curbuf->b_p_ffu, &curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007116 else
7117 {
7118 // global option set
7119 retval = option_set_callback_func(p_ffu, &ffu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01007120 // when using :set, free the local callback
7121 if (!(args->os_flags & OPT_GLOBAL))
7122 free_callback(&curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007123 }
7124
7125 if (retval == FAIL)
7126 return e_invalid_argument;
7127
7128 // If the option value starts with <SID> or s:, then replace that with
7129 // the script identifier.
7130 char_u **varp = (char_u **)args->os_varp;
7131 char_u *name = get_scriptlocal_funcname(*varp);
7132 if (name != NULL)
7133 {
7134 free_string_option(*varp);
7135 *varp = name;
7136 }
7137
7138 return NULL;
7139}
7140
7141# if defined(EXITFREE) || defined(PROTO)
7142 void
7143free_findfunc_option(void)
7144{
7145 free_callback(&ffu_cb);
7146}
7147# endif
7148
7149/*
7150 * Mark the global 'findfunc' callback with "copyID" so that it is not
7151 * garbage collected.
7152 */
7153 int
7154set_ref_in_findfunc(int copyID UNUSED)
7155{
7156 int abort = FALSE;
7157 abort = set_ref_in_callback(&ffu_cb, copyID);
7158 return abort;
7159}
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007160#endif
7161
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162/*
7163 * :sview [+command] file split window with new file, read-only
7164 * :split [[+command] file] split window with current or new file
7165 * :vsplit [[+command] file] split window vertically with current or new file
7166 * :new [[+command] file] split window with no or new file
7167 * :vnew [[+command] file] split vertically window with no or new file
7168 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007169 *
7170 * :tabedit open new Tab page with empty window
7171 * :tabedit [+command] file open new Tab page and edit "file"
7172 * :tabnew [[+command] file] just like :tabedit
7173 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 */
7175 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007176ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007178 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007180#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007181 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02007182 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007183#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02007184 int use_tab = eap->cmdidx == CMD_tabedit
7185 || eap->cmdidx == CMD_tabfind
7186 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007188 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007189 return;
7190
Bram Moolenaar4033c552017-09-16 20:54:51 +02007191#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
Bram Moolenaar4033c552017-09-16 20:54:51 +02007195#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007196 // A ":split" in the quickfix window works like ":new". Don't want two
7197 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02007198 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
7200 if (eap->cmdidx == CMD_split)
7201 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 if (eap->cmdidx == CMD_vsplit)
7203 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007207 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007209 char_u *file_to_find = NULL;
7210 char *search_ctx = NULL;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007211
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007212 if (*get_findfunc() != NUL)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007213 {
7214#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007215 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007216 eap->addr_count > 0 ? eap->line2 : 1);
7217#endif
7218 }
7219 else
7220 {
7221 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7222 FNAME_MESS, TRUE, curbuf->b_ffname,
7223 &file_to_find, &search_ctx);
7224 vim_free(file_to_find);
7225 vim_findfile_cleanup(search_ctx);
7226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 if (fname == NULL)
7228 goto theend;
7229 eap->arg = fname;
7230 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007231# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007232 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 && eap->cmdidx != CMD_new)
7235 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007236 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007237# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007238 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007239# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007240 au_has_group((char_u *)"FileExplorer"))
7241 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007242 // No browsing supported but we do have the file explorer:
7243 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007244 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007245 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007246 }
7247 else
7248 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007249 fname = do_browse(0, (char_u *)(use_tab
7250 ? _("Edit File in new tab page")
7251 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007253 if (fname == NULL)
7254 goto theend;
7255 eap->arg = fname;
7256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007258 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007261 /*
7262 * Either open new tab page or split the window.
7263 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007264 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007265 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007266 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007267 : eap->addr_count == 0 ? 0
7268 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007269 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007270 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007272 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007273 if (curwin != old_curwin
7274 && win_valid(old_curwin)
7275 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007276 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007277 old_curwin->w_alt_fnum = curbuf->b_fnum;
7278 }
7279 }
7280 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007283 // Reset 'scrollbind' when editing another file, but keep it when
7284 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007285 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007286 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 else
7288 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 do_exedit(eap, old_curwin);
7290 }
7291
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007292# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007293 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296theend:
7297 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007299
7300/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007301 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007302 */
7303 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007304tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007305{
7306 exarg_T ea;
7307
Bram Moolenaara80faa82020-04-12 19:37:17 +02007308 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007309 ea.cmdidx = CMD_tabnew;
7310 ea.cmd = (char_u *)"tabn";
7311 ea.arg = (char_u *)"";
7312 ex_splitview(&ea);
7313}
7314
7315/*
7316 * :tabnext command
7317 */
7318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007319ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007320{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007321 int tab_number;
7322
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007323 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007324 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007325 switch (eap->cmdidx)
7326 {
7327 case CMD_tabfirst:
7328 case CMD_tabrewind:
7329 goto_tabpage(1);
7330 break;
7331 case CMD_tablast:
7332 goto_tabpage(9999);
7333 break;
7334 case CMD_tabprevious:
7335 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007336 if (eap->arg && *eap->arg != NUL)
7337 {
7338 char_u *p = eap->arg;
7339 char_u *p_save = p;
7340
7341 tab_number = getdigits(&p);
7342 if (p == p_save || *p_save == '-' || *p != NUL
7343 || tab_number == 0)
7344 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007345 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007346 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007347 return;
7348 }
7349 }
7350 else
7351 {
7352 if (eap->addr_count == 0)
7353 tab_number = 1;
7354 else
7355 {
7356 tab_number = eap->line2;
7357 if (tab_number < 1)
7358 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007359 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007360 return;
7361 }
7362 }
7363 }
7364 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007365 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007366 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007367 tab_number = get_tabpage_arg(eap);
7368 if (eap->errmsg == NULL)
7369 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007370 break;
7371 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007372}
7373
7374/*
7375 * :tabmove command
7376 */
7377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007378ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007379{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007380 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007381
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007382 tab_number = get_tabpage_arg(eap);
7383 if (eap->errmsg == NULL)
7384 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007385}
7386
7387/*
7388 * :tabs command: List tabs and their contents.
7389 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007391ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392{
7393 tabpage_T *tp;
7394 win_T *wp;
7395 int tabcount = 1;
7396
7397 msg_start();
7398 msg_scroll = TRUE;
7399 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7400 {
7401 msg_putchar('\n');
7402 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007403 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007404 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007405 ui_breakcheck();
7406
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007407 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007408 wp = firstwin;
7409 else
7410 wp = tp->tp_firstwin;
7411 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7412 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007413 msg_putchar('\n');
7414 msg_putchar(wp == curwin ? '>' : ' ');
7415 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007416 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7417 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007418 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007419 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007420 else
7421 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7422 IObuff, IOSIZE, TRUE);
7423 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007424 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007425 ui_breakcheck();
7426 }
7427 }
7428}
7429
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430/*
7431 * ":mode": Set screen mode.
7432 * If no argument given, just get the screen size and redraw.
7433 */
7434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007435ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
7437 if (*eap->arg == NUL)
7438 shell_resized();
7439 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007440 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441}
7442
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443/*
7444 * ":resize".
7445 * set, increment or decrement current window height
7446 */
7447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007448ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
7450 int n;
7451 win_T *wp = curwin;
7452
7453 if (eap->addr_count > 0)
7454 {
7455 n = eap->line2;
7456 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7457 ;
7458 }
7459
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007460# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007464 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 {
7466 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007467 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007468 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007470 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 }
7472 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
7474 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007475 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007476 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007478 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 }
7480}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
7482/*
7483 * ":find [+command] <file>" command.
7484 */
7485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007486ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
Colin Kennedy21570352024-03-03 16:16:47 +01007488 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007489 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007490
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007491 char_u *fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007493 char_u *file_to_find = NULL;
7494 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007496 if (*get_findfunc() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007498#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007499 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007500 eap->addr_count > 0 ? eap->line2 : 1);
7501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007503 else
7504 {
7505 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7506 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
7507 if (eap->addr_count > 0)
7508 {
7509 // Repeat finding the file "count" times. This matters when it appears
7510 // several times in the path.
7511 count = eap->line2;
7512 while (fname != NULL && --count > 0)
7513 {
7514 vim_free(fname);
7515 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7516 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
7517 }
7518 }
7519 VIM_CLEAR(file_to_find);
7520 vim_findfile_cleanup(search_ctx);
7521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007523 if (fname == NULL)
7524 return;
7525
7526 eap->arg = fname;
7527 do_exedit(eap, NULL);
7528 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529}
7530
7531/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007532 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007533 */
7534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007535ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007536{
7537 regmatch_T regmatch;
7538 char_u *p;
7539
Bram Moolenaar65088802021-03-13 21:07:21 +01007540#ifdef FEAT_EVAL
7541 if (not_in_vim9(eap) == FAIL)
7542 return;
7543#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007544 curwin->w_cursor.lnum = eap->line2;
7545 beginline(BL_SOL | BL_FIX);
7546 if (*eap->arg == '/')
7547 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007548 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007549 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007550 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007551 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007552 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007553 if (regmatch.regprog != NULL)
7554 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007555 // make a copy of the line, when searching for a mark it might be
7556 // flushed
7557 char_u *line = vim_strsave(ml_get_curline());
7558
Bram Moolenaardf177f62005-02-22 08:39:57 +00007559 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007560 if (vim_regexec(&regmatch, line, (colnr_T)0))
7561 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007562 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007563 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007564 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007565 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007566 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007567 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007568 eap->arg += STRLEN(eap->arg);
7569 }
7570 check_cursor();
7571
7572 eap->cmdidx = CMD_visual;
7573 do_exedit(eap, NULL);
7574}
7575
7576/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007577 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 */
7579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007580ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007582 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7583
Colin Kennedy21570352024-03-03 16:16:47 +01007584 // Exclude commands which keep the window's current buffer
7585 if (
7586 eap->cmdidx != CMD_badd
7587 && eap->cmdidx != CMD_balt
7588 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007589 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7590 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007591 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007592
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 do_exedit(eap, NULL);
7594}
7595
7596/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007597 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007600do_exedit(
7601 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007602 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
7604 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007606 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007608 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7609 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007610 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 /*
7612 * ":vi" command ends Ex mode.
7613 */
7614 if (exmode_active && (eap->cmdidx == CMD_visual
7615 || eap->cmdidx == CMD_view))
7616 {
7617 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007618 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007621 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007622 if (global_busy)
7623 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007624 if (eap->nextcmd != NULL)
7625 {
7626 stuffReadbuff(eap->nextcmd);
7627 eap->nextcmd = NULL;
7628 }
7629
7630 if (exmode_was != EXMODE_VIM)
7631 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007632 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007633 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007634 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007635 no_wait_return = 0;
7636 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007637 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007638 msg_scroll = 0;
7639#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007640 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007641 hold_gui_events = 0;
7642#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007643 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007644 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007645
7646 main_loop(FALSE, TRUE);
7647
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007648 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007649 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007650 no_wait_return = save_nwr;
7651 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007652#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007653 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007654#endif
7655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659
7660 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007661 || eap->cmdidx == CMD_tabnew
7662 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007663 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007665 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 setpcmark();
7667 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007668 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7669 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007671 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 || *eap->arg != NUL
7673#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007674 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675#endif
7676 )
7677 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007678 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7679 // Only ":edit" or ":script" can bring us here, others are stopped
7680 // earlier.
7681 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007682 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 n = readonlymode;
7685 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7686 readonlymode = TRUE;
7687 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007688 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7689 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007690 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7691 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7693 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007694 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7696 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7697 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007698 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007700 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007701 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007702 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7703 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007704 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007706 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 if (old_curwin != NULL)
7708 {
7709 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007710 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007712#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007713 cleanup_T cs;
7714
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007715 // Reset the error/interrupt/exception state here so that
7716 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007717 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007718#endif
7719#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007721#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007722 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007723
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007724#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007725 // Restore the error/interrupt/exception state if not
7726 // discarded by a new aborting error, interrupt, or
7727 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007728 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
7731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 }
7733 else if (readonlymode && curbuf->b_nwindows == 1)
7734 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007735 // When editing an already visited buffer, 'readonly' won't be set
7736 // but the previous value is kept. With ":view" and ":sview" we
7737 // want the file to be readonly, except when another window is
7738 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 curbuf->b_p_ro = TRUE;
7740 }
7741 readonlymode = n;
7742 }
7743 else
7744 {
7745 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007746 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 if (n != curwin->w_arg_idx_invalid)
7750 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 }
7752
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 /*
7754 * if ":split file" worked, set alternate file name in old window to new
7755 * file
7756 */
7757 if (old_curwin != NULL
7758 && *eap->arg != NUL
7759 && curwin != old_curwin
7760 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007761 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007762 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764
7765 ex_no_reprint = TRUE;
7766}
7767
7768#ifndef FEAT_GUI
7769/*
7770 * ":gui" and ":gvim" when there is no GUI.
7771 */
7772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007773ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007775 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776}
7777#endif
7778
Bram Moolenaar4f974752019-02-17 17:44:42 +01007779#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007781ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 gui_make_tearoff(eap->arg);
7784}
7785#endif
7786
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007787#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7788 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007790ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007792# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7793 if (gui.in_use)
7794 gui_make_popup(eap->arg, eap->forceit);
7795# ifdef FEAT_TERM_POPUP_MENU
7796 else
7797# endif
7798# endif
7799# ifdef FEAT_TERM_POPUP_MENU
7800 pum_make_popup(eap->arg, eap->forceit);
7801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802}
7803#endif
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007806ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007809 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007811 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812}
7813
7814/*
7815 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7816 * offset.
7817 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007820ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007823 win_T *save_curwin = curwin;
7824 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 long topline;
7826 long y;
7827 linenr_T old_linenr = curwin->w_cursor.lnum;
7828
7829 setpcmark();
7830
7831 /*
7832 * determine max topline
7833 */
7834 if (curwin->w_p_scb)
7835 {
7836 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007837 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {
7839 if (wp->w_p_scb && wp->w_buffer)
7840 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007841 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 if (topline > y)
7843 topline = y;
7844 }
7845 }
7846 if (topline < 1)
7847 topline = 1;
7848 }
7849 else
7850 {
7851 topline = 1;
7852 }
7853
7854
7855 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007856 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007858 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {
7860 if (curwin->w_p_scb)
7861 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007862 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 y = topline - curwin->w_topline;
7864 if (y > 0)
7865 scrollup(y, TRUE);
7866 else
7867 scrolldown(-y, TRUE);
7868 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007869 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
7873 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007874 curwin = save_curwin;
7875 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 if (curwin->w_p_scb)
7877 {
7878 did_syncbind = TRUE;
7879 checkpcmark();
7880 if (old_linenr != curwin->w_cursor.lnum)
7881 {
7882 char_u ctrl_o[2];
7883
7884 ctrl_o[0] = Ctrl_O;
7885 ctrl_o[1] = 0;
7886 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7887 }
7888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891
7892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007893ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007895 int i;
7896 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7897 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007899 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007901 do_bang(1, eap, FALSE, FALSE, TRUE);
7902 return;
7903 }
7904
7905 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7906 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
7908#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007909 if (cmdmod.cmod_flags & CMOD_BROWSE)
7910 {
7911 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007913 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7914 NULL, NULL, NULL, curbuf);
7915 if (browseFile != NULL)
7916 {
7917 i = readfile(browseFile, NULL,
7918 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7919 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 }
7921 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007922 i = OK;
7923 }
7924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007926 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007928 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 return;
7930 i = readfile(curbuf->b_ffname, curbuf->b_fname,
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 else
7934 {
7935 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7936 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7937 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007938 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939
7940 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007941 if (i != OK)
7942 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007943#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007944 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007946 semsg(_(e_cant_open_file_str), eap->arg);
7947 }
7948 else
7949 {
7950 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007951 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007952 // Delete the empty line that remains. Historically ex does
7953 // this but vi doesn't.
7954 if (eap->line2 == 0)
7955 lnum = curbuf->b_ml.ml_line_count;
7956 else
7957 lnum = 1;
7958 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007959 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007960 ml_delete(lnum);
7961 if (curwin->w_cursor.lnum > 1
7962 && curwin->w_cursor.lnum >= lnum)
7963 --curwin->w_cursor.lnum;
7964 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007965 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007966 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007967 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 }
7969}
7970
Bram Moolenaarea408852005-06-25 22:49:46 +00007971static char_u *prev_dir = NULL;
7972
7973#if defined(EXITFREE) || defined(PROTO)
7974 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007975free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007976{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007977 VIM_CLEAR(prev_dir);
7978 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007979}
7980#endif
7981
Bram Moolenaarf4258302013-06-02 18:20:17 +02007982/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007983 * Get the previous directory for the given chdir scope.
7984 */
7985 static char_u *
7986get_prevdir(cdscope_T scope)
7987{
7988 if (scope == CDSCOPE_WINDOW)
7989 return curwin->w_prevdir;
7990 else if (scope == CDSCOPE_TABPAGE)
7991 return curtab->tp_prevdir;
7992 return prev_dir;
7993}
7994
7995/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007996 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007997 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7998 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007999 */
8000 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008001post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008002{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008003 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008004 // Clear tab local directory for both :cd and :tcd
8005 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008006 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008007 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008008 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008009 char_u *pdir = get_prevdir(scope);
8010
8011 // If still in the global directory, need to remember current
8012 // directory as the global directory.
8013 if (globaldir == NULL && pdir != NULL)
8014 globaldir = vim_strsave(pdir);
8015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008016 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02008017 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008018 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008019 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008020 curtab->tp_localdir = vim_strsave(NameBuff);
8021 else
8022 curwin->w_localdir = vim_strsave(NameBuff);
8023 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02008024 }
8025 else
8026 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008027 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01008028 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008029 }
8030
zeertzjq64be6aa2021-11-19 11:59:08 +00008031 last_chdir_reason = NULL;
glepnir4ade6682025-07-03 20:41:23 +02008032 shorten_fnames(vim_strchr(p_cpo, CPO_NOSYMLINKS) == NULL);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008033}
8034
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008035/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008036 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
8037 */
8038 void
8039trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
8040{
8041#ifdef FEAT_EVAL
8042 dict_T *v_event;
8043 save_v_event_T save_v_event;
8044
8045 v_event = get_v_event(&save_v_event);
8046 (void)dict_add_string(v_event, "directory", new_dir);
8047 dict_set_items_ro(v_event);
8048#endif
8049 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
8050#ifdef FEAT_EVAL
8051 restore_v_event(v_event, &save_v_event);
8052#endif
8053}
8054
8055/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008056 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02008057 * chdir() function.
8058 * scope == CDSCOPE_WINDOW: changes the window-local directory
8059 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
8060 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008061 * Returns TRUE if the directory is successfully changed.
8062 */
8063 int
8064changedir_func(
8065 char_u *new_dir,
8066 int forceit,
8067 cdscope_T scope)
8068{
Bram Moolenaar002bc792020-06-05 22:33:42 +02008069 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008070 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008071 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00008072 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008073 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008074
Bram Moolenaar7cc96922020-01-31 22:41:38 +01008075 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008076 return FALSE;
8077
8078 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
8079 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00008080 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008081 return FALSE;
8082 }
8083
8084 // ":cd -": Change to previous directory
8085 if (STRCMP(new_dir, "-") == 0)
8086 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008087 pdir = get_prevdir(scope);
8088 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008089 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008090 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008091 return FALSE;
8092 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02008093 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008094 }
8095
8096 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008097 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02008098 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008099 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02008100 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008101
Bakudankun29f3a452021-12-11 12:28:08 +00008102 // For UNIX ":cd" means: go to home directory.
8103 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008104#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008105 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00008106#else
8107 if (*new_dir == NUL && p_cdh)
8108#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008109 {
8110 // use NameBuff for home directory name
8111# ifdef VMS
8112 char_u *p;
8113
8114 p = mch_getenv((char_u *)"SYS$LOGIN");
8115 if (p == NULL || *p == NUL) // empty is the same as not set
8116 NameBuff[0] = NUL;
8117 else
8118 vim_strncpy(NameBuff, p, MAXPATHL - 1);
8119# else
8120 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8121# endif
8122 new_dir = NameBuff;
8123 }
zeertzjqf38aad82021-12-30 13:45:57 +00008124 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008125 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
8126 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00008127 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008128 if (scope == CDSCOPE_WINDOW)
8129 acmd_fname = (char_u *)"window";
8130 else if (scope == CDSCOPE_TABPAGE)
8131 acmd_fname = (char_u *)"tabpage";
8132 else
8133 acmd_fname = (char_u *)"global";
8134 trigger_DirChangedPre(acmd_fname, new_dir);
8135
8136 if (vim_chdir(new_dir))
8137 {
8138 emsg(_(e_command_failed));
8139 vim_free(pdir);
8140 return FALSE;
8141 }
Richard Doty3d0abad2021-12-29 14:39:08 +00008142 }
zeertzjq73257142022-02-02 13:16:37 +00008143
8144 if (scope == CDSCOPE_WINDOW)
8145 pp = &curwin->w_prevdir;
8146 else if (scope == CDSCOPE_TABPAGE)
8147 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008148 else
zeertzjq73257142022-02-02 13:16:37 +00008149 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008150 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00008151 *pp = pdir;
8152
8153 post_chdir(scope);
8154
8155 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008156 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008157 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00008158 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008159}
Bram Moolenaarea408852005-06-25 22:49:46 +00008160
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008162 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008164 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008165ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01008167 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
8169 new_dir = eap->arg;
8170#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00008171 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
8172 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008174 ex_pwd(NULL);
8175 return;
8176 }
8177#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008178
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008179 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008180
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008181 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8182 scope = CDSCOPE_WINDOW;
8183 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
8184 scope = CDSCOPE_TABPAGE;
8185
8186 if (changedir_func(new_dir, eap->forceit, scope))
8187 {
8188 // Echo the new current directory if the command was typed.
8189 if (KeyTyped || p_verbose >= 5)
8190 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 }
8192}
8193
8194/*
8195 * ":pwd".
8196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008198ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
8200 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8201 {
8202#ifdef BACKSLASH_IN_FILENAME
8203 slash_adjust(NameBuff);
8204#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02008205 if (p_verbose > 0)
8206 {
8207 char *context = "global";
8208
Bram Moolenaar05268152021-11-18 18:53:45 +00008209 if (last_chdir_reason != NULL)
8210 context = last_chdir_reason;
8211 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02008212 context = "window";
8213 else if (curtab->tp_localdir != NULL)
8214 context = "tabpage";
8215 smsg("[%s] %s", context, (char *)NameBuff);
8216 }
8217 else
8218 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
8220 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00008221 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222}
8223
8224/*
8225 * ":=".
8226 */
8227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008228ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008230 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008231 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232}
8233
8234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008235ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008237 int n;
8238 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239
8240 if (cursor_valid())
8241 {
8242 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8243 if (n >= 0)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02008244 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008246
8247 len = eap->line2;
8248 switch (*eap->arg)
8249 {
8250 case 'm': break;
8251 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008252 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008253 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008254
8255 // Hide the cursor if invoked with !
8256 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257}
8258
8259/*
8260 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008261 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 */
8263 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008264do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
Bram Moolenaar52953192019-08-16 10:27:13 +02008266 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008267 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008268# ifdef ELAPSED_FUNC
8269 elapsed_T start_tv;
8270
8271 // Remember at what time we started, so that we know how much longer we
8272 // should wait after waiting for a bit.
8273 ELAPSED_INIT(start_tv);
8274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008276 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008277 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008278 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008279 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008280
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008281 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008282 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008284 wait_now = msec - done > 1000L ? 1000L : msec - done;
8285#ifdef FEAT_TIMERS
8286 {
8287 long due_time = check_due_timer();
8288
8289 if (due_time > 0 && due_time < wait_now)
8290 wait_now = due_time;
8291 }
8292#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008293#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008294 if (has_any_channel() && wait_now > 20L)
8295 wait_now = 20L;
8296#endif
8297#ifdef FEAT_SOUND
8298 if (has_any_sound_callback() && wait_now > 20L)
8299 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008300#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008301 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008302
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008303#ifdef FEAT_JOB_CHANNEL
8304 if (has_any_channel())
8305 ui_breakcheck_force(TRUE);
8306 else
8307#endif
8308 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008309#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008310 // Process the netbeans and clientserver messages that may have been
8311 // received in the call to ui_breakcheck() when the GUI is in use. This
8312 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008313 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008314#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008315
8316# ifdef ELAPSED_FUNC
8317 // actual time passed
8318 done = ELAPSED_FUNC(start_tv);
8319# else
8320 // guestimate time passed (will actually be more)
8321 done += wait_now;
8322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008324
8325 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8326 // input buffer, otherwise a following call to input() fails.
8327 if (got_int)
8328 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008329
8330 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008331 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332}
8333
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334/*
8335 * ":winsize" command (obsolete).
8336 */
8337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008338ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339{
8340 int w, h;
8341 char_u *arg = eap->arg;
8342 char_u *p;
8343
Keith Thompson184f71c2024-01-04 21:19:04 +01008344 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008345 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008346 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008347 return;
8348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 w = getdigits(&arg);
8350 arg = skipwhite(arg);
8351 p = arg;
8352 h = getdigits(&arg);
8353 if (*p != NUL && *arg == NUL)
8354 set_shellsize(w, h, TRUE);
8355 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008356 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357}
8358
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008360ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361{
8362 int xchar = NUL;
8363 char_u *p;
8364
8365 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8366 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008367 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 if (eap->arg[1] == NUL)
8369 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008370 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 return;
8372 }
8373 xchar = eap->arg[1];
8374 p = eap->arg + 2;
8375 }
8376 else
8377 p = eap->arg + 1;
8378
Bram Moolenaar63b91732021-08-05 20:40:03 +02008379 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008381 if (*p != NUL && *p != (
8382#ifdef FEAT_EVAL
8383 in_vim9script() ? '#' :
8384#endif
8385 '"')
8386 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008387 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008388 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008391 postponed_split_flags = cmdmod.cmod_split;
8392 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8394 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008395 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 }
8397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398
Bram Moolenaar843ee412004-06-30 16:16:41 +00008399#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400/*
8401 * ":winpos".
8402 */
8403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008404ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405{
8406 int x, y;
8407 char_u *arg = eap->arg;
8408 char_u *p;
8409
8410 if (*arg == NUL)
8411 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008412# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008413# ifdef VIMDLL
8414 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8415 mch_get_winpos(&x, &y) != FAIL)
8416# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008418# else
8419 if (mch_get_winpos(&x, &y) != FAIL)
8420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {
8422 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008423 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 }
8425 else
8426# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008427 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 }
8429 else
8430 {
8431 x = getdigits(&arg);
8432 arg = skipwhite(arg);
8433 p = arg;
8434 y = getdigits(&arg);
8435 if (*p == NUL || *arg != NUL)
8436 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008437 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 return;
8439 }
8440# ifdef FEAT_GUI
8441 if (gui.in_use)
8442 gui_mch_set_winpos(x, y);
8443 else if (gui.starting)
8444 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008445 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 gui_win_x = x;
8447 gui_win_y = y;
8448 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008449# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 else
8451# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008452# endif
8453# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008454 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455# endif
8456# ifdef HAVE_TGETENT
8457 if (*T_CWP)
8458 term_set_winpos(x, y);
8459# endif
8460 }
8461}
8462#endif
8463
8464/*
8465 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8466 */
8467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008468ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
8470 oparg_T oa;
8471
8472 clear_oparg(&oa);
8473 oa.regname = eap->regname;
8474 oa.start.lnum = eap->line1;
8475 oa.end.lnum = eap->line2;
8476 oa.line_count = eap->line2 - eap->line1 + 1;
8477 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008479 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
8481 setpcmark();
8482 curwin->w_cursor.lnum = eap->line1;
8483 beginline(BL_SOL | BL_FIX);
8484 }
8485
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008486 if (VIsual_active)
8487 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008488
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 switch (eap->cmdidx)
8490 {
8491 case CMD_delete:
8492 oa.op_type = OP_DELETE;
8493 op_delete(&oa);
8494 break;
8495
8496 case CMD_yank:
8497 oa.op_type = OP_YANK;
8498 (void)op_yank(&oa, FALSE, TRUE);
8499 break;
8500
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008501 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008502 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008504 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8505#else
8506 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008508 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 oa.op_type = OP_RSHIFT;
8510 else
8511 oa.op_type = OP_LSHIFT;
8512 op_shift(&oa, FALSE, eap->amount);
8513 break;
8514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008516 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517}
8518
8519/*
8520 * ":put".
8521 */
8522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008523ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008525 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (eap->line2 == 0)
8527 {
8528 eap->line2 = 1;
8529 eap->forceit = TRUE;
8530 }
8531 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008532 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008533 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008534 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535}
8536
8537/*
64-bitmane08f10a2025-03-18 22:14:34 +01008538 * ":iput".
8539 */
8540 static void
8541ex_iput(exarg_T *eap)
8542{
8543 // ":0iput" works like ":1iput!".
8544 if (eap->line2 == 0)
8545 {
8546 eap->line2 = 1;
8547 eap->forceit = TRUE;
8548 }
8549 curwin->w_cursor.lnum = eap->line2;
8550 check_cursor_col();
8551 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
8552 PUT_LINE|PUT_CURSLINE
8553 |PUT_FIXINDENT);
8554}
8555
8556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 * Handle ":copy" and ":move".
8558 */
8559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008560ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562 long n;
8563
Bram Moolenaar491799b2020-08-01 19:23:43 +02008564#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008565 if (not_in_vim9(eap) == FAIL)
8566 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008567#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008568 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008569 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 {
8571 eap->nextcmd = NULL;
8572 return;
8573 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008574 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575
8576 /*
8577 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8578 */
8579 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8580 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008581 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 return;
8583 }
8584
8585 if (eap->cmdidx == CMD_move)
8586 {
8587 if (do_move(eap->line1, eap->line2, n) == FAIL)
8588 return;
8589 }
8590 else
8591 ex_copy(eap->line1, eap->line2, n);
8592 u_clearline();
8593 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008594 ex_may_print(eap);
8595}
8596
8597/*
8598 * Print the current line if flags were given to the Ex command.
8599 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008600 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008601ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008602{
8603 if (eap->flags != 0)
8604 {
8605 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8606 (eap->flags & EXFLAG_LIST));
8607 ex_no_reprint = TRUE;
8608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
8610
8611/*
8612 * ":smagic" and ":snomagic".
8613 */
8614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008615ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008617 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008619 magic_overruled = eap->cmdidx == CMD_smagic
8620 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008621 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008622 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623}
8624
8625/*
8626 * ":join".
8627 */
8628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008629ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 curwin->w_cursor.lnum = eap->line1;
8632 if (eap->line1 == eap->line2)
8633 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008634 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 return;
8636 if (eap->line2 == curbuf->b_ml.ml_line_count)
8637 {
8638 beep_flush();
8639 return;
8640 }
8641 ++eap->line2;
8642 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008643 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008645 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646}
8647
8648/*
8649 * ":[addr]@r" or ":[addr]*r": execute register
8650 */
8651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008652ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653{
8654 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008655 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
8657 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008658 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659
8660#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008661 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662#endif
8663
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008664 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 c = *eap->arg;
8666 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8667 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008668 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008669 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8670 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008673 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008676 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008678 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008680 /*
8681 * Execute from the typeahead buffer.
8682 * Continue until the stuff buffer is empty and all added characters
8683 * have been consumed.
8684 */
8685 while (!stuff_empty() || typebuf.tb_len > prev_len)
8686 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8687
8688 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689}
8690
8691/*
8692 * ":!".
8693 */
8694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008695ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8698}
8699
8700/*
8701 * ":undo".
8702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008704ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008706 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008707 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008708 else
8709 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710}
8711
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008712#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008714ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008715{
8716 char_u hash[UNDO_HASH_SIZE];
8717
8718 u_compute_hash(hash);
8719 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8720}
8721
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008723ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008724{
8725 char_u hash[UNDO_HASH_SIZE];
8726
8727 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008728 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008729}
8730#endif
8731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732/*
8733 * ":redo".
8734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008736ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737{
8738 u_redo(1);
8739}
8740
8741/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008742 * ":earlier" and ":later".
8743 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008745ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008746{
8747 long count = 0;
8748 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008749 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008750 char_u *p = eap->arg;
8751
8752 if (*p == NUL)
8753 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008754 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008755 {
8756 count = getdigits(&p);
8757 switch (*p)
8758 {
8759 case 's': ++p; sec = TRUE; break;
8760 case 'm': ++p; sec = TRUE; count *= 60; break;
8761 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008762 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8763 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008764 }
8765 }
8766
8767 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008768 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008769 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008770 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8771 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008772}
8773
8774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 * ":redir": start/stop redirection.
8776 */
8777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008778ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
8780 char *mode;
8781 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008782 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
Bram Moolenaarba768492016-07-08 15:32:54 +02008784#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008785 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008786 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008787 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008788 return;
8789 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008790#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 if (STRICMP(eap->arg, "END") == 0)
8793 close_redir();
8794 else
8795 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008796 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008798 ++arg;
8799 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008801 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 mode = "a";
8803 }
8804 else
8805 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008806 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
8808 close_redir();
8809
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008810 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008811 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 if (fname == NULL)
8813 return;
8814#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008815 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 {
8817 char_u *browseFile;
8818
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008819 browseFile = do_browse(BROWSE_SAVE,
8820 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008821 fname, NULL, NULL,
8822 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008824 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 vim_free(fname);
8826 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008827 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 }
8829#endif
8830
8831 redir_fd = open_exfile(fname, eap->forceit, mode);
8832 vim_free(fname);
8833 }
8834#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008835 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008837 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008839 ++arg;
8840 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008842 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008843 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008845 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008847 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008848 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008849 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008850 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008852 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008853 if (*arg == '>')
8854 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008855 // Make register empty when not using @A-@Z and the
8856 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008857 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008858 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008860 }
8861 if (*arg != NUL)
8862 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008863 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008864 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008865 }
8866 }
8867 else if (*arg == '=' && arg[1] == '>')
8868 {
8869 int append;
8870
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008871 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008872 close_redir();
8873 arg += 2;
8874
8875 if (*arg == '>')
8876 {
8877 ++arg;
8878 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 }
8880 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008881 append = FALSE;
8882
8883 if (var_redir_start(skipwhite(arg), append) == OK)
8884 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
8886#endif
8887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008888 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008891 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008894 // Make sure redirection is not off. Can happen for cmdline completion
8895 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008896 if (redir_fd != NULL
8897#ifdef FEAT_EVAL
8898 || redir_reg || redir_vname
8899#endif
8900 )
8901 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902}
8903
8904/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008905 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008907 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008908ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008910 redraw_cmd(eap->forceit);
8911}
8912
8913/*
8914 * ":redraw": force redraw, with clear if "clear" is TRUE.
8915 */
8916 void
8917redraw_cmd(int clear)
8918{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008919 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008921
8922 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008924
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008925 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008927 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 if (need_maketitle)
8929 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008930#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8931# ifdef VIMDLL
8932 if (!gui.in_use)
8933# endif
8934 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008935#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008936 RedrawingDisabled = save_RedrawingDisabled;
8937 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938
Bram Moolenaar5017c662022-04-07 18:06:08 +01008939 // After drawing the statusline screen_attr may still be set.
8940 screen_stop_highlight();
8941
Bram Moolenaara2a89732022-08-31 14:46:18 +01008942 // Reset msg_didout, so that a message that's there is overwritten.
8943 msg_didout = FALSE;
8944 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008946 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008947 need_wait_return = FALSE;
8948
Bram Moolenaara653e532022-04-19 11:38:24 +01008949 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008950 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008951 redrawcmdline();
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 out_flush();
8954}
8955
8956/*
8957 * ":redrawstatus": force redraw of status line(s)
8958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008960ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 if (eap->forceit)
8963 status_redraw_all();
8964 else
8965 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008966 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008967 return; // redraw later
8968
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008969 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008970 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008971
8972 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008973 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008974
zeertzjq320d9102022-09-20 17:12:13 +01008975 if (State & MODE_CMDLINE)
8976 redraw_statuslines();
8977 else
8978 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008979 RedrawingDisabled = save_RedrawingDisabled;
8980 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
8983
Bram Moolenaare12bab32019-01-08 22:02:56 +01008984/*
8985 * ":redrawtabline": force redraw of the tabline
8986 */
8987 static void
8988ex_redrawtabline(exarg_T *eap UNUSED)
8989{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008990 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008991 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008992
8993 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008994 p_lz = FALSE;
8995
8996 draw_tabline();
8997
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008998 RedrawingDisabled = save_RedrawingDisabled;
8999 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01009000 out_flush();
9001}
9002
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02009003/*
9004 * ":redrawtabpanel": force redraw of the tabpanel
9005 */
9006 static void
9007ex_redrawtabpanel(exarg_T *eap UNUSED)
9008{
9009 int save_RedrawingDisabled = RedrawingDisabled;
9010 RedrawingDisabled = 0;
9011
9012 int save_p_lz = p_lz;
9013 p_lz = FALSE;
9014
9015#if defined(FEAT_TABPANEL)
9016 draw_tabpanel();
9017#else
9018 emsg(_(e_cannot_not_support_redrawtabpanel));
9019#endif
9020
9021 RedrawingDisabled = save_RedrawingDisabled;
9022 p_lz = save_p_lz;
9023 out_flush();
9024}
9025
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009027close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
9029 if (redir_fd != NULL)
9030 {
9031 fclose(redir_fd);
9032 redir_fd = NULL;
9033 }
9034#ifdef FEAT_EVAL
9035 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009036 if (redir_vname)
9037 {
9038 var_redir_stop();
9039 redir_vname = 0;
9040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041#endif
9042}
9043
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02009044#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009045 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02009046vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009047{
9048 if (vim_mkdir(name, prot) != 0)
9049 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00009050 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009051 return FAIL;
9052 }
9053 return OK;
9054}
9055#endif
9056
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057/*
9058 * Open a file for writing for an Ex command, with some checks.
9059 * Return file descriptor, or NULL on failure.
9060 */
9061 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009062open_exfile(
9063 char_u *fname,
9064 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009065 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
9067 FILE *fd;
9068
9069#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009070 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (mch_isdir(fname))
9072 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01009073 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 return NULL;
9075 }
9076#endif
9077 if (!forceit && *mode != 'a' && vim_fexists(fname))
9078 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00009079 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 return NULL;
9081 }
9082
9083 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00009084 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
9086 return fd;
9087}
9088
9089/*
9090 * ":mark" and ":k".
9091 */
9092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009093ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094{
9095 pos_T pos;
9096
Bram Moolenaar10b94212021-02-19 21:42:57 +01009097#ifdef FEAT_EVAL
9098 if (not_in_vim9(eap) == FAIL)
9099 return;
9100#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009101 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009103 emsg(_(e_argument_required));
9104 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009106
9107 if (eap->arg[1] != NUL) // more than one character?
9108 {
9109 semsg(_(e_trailing_characters_str), eap->arg);
9110 return;
9111 }
9112
9113 pos = curwin->w_cursor; // save curwin->w_cursor
9114 curwin->w_cursor.lnum = eap->line2;
9115 beginline(BL_WHITE | BL_FIX);
9116 if (setmark(*eap->arg) == FAIL) // set mark
9117 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
9118 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119}
9120
9121/*
9122 * Update w_topline, w_leftcol and the cursor position.
9123 */
9124 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009125update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009127 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 update_topline();
9129 if (!curwin->w_p_wrap)
9130 validate_cursor();
9131 update_curswant();
9132}
9133
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009135 * Save the current State and go to Normal mode.
9136 * Return TRUE if the typeahead could be saved.
9137 */
9138 int
9139save_current_state(save_state_T *sst)
9140{
9141 sst->save_msg_scroll = msg_scroll;
9142 sst->save_restart_edit = restart_edit;
9143 sst->save_msg_didout = msg_didout;
9144 sst->save_State = State;
9145 sst->save_insertmode = p_im;
9146 sst->save_finish_op = finish_op;
9147 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009148 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009149 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009150
Bram Moolenaar4324d872020-12-01 20:12:24 +01009151 msg_scroll = FALSE; // no msg scrolling in Normal mode
9152 restart_edit = 0; // don't go to Insert mode
9153 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01009154
Bram Moolenaara452b802020-12-01 21:47:59 +01009155 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01009156 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009157
9158 /*
9159 * Save the current typeahead. This is required to allow using ":normal"
9160 * from an event handler and makes sure we don't hang when the argument
9161 * ends with half a command.
9162 */
9163 save_typeahead(&sst->tabuf);
9164 return sst->tabuf.typebuf_valid;
9165}
9166
9167 void
9168restore_current_state(save_state_T *sst)
9169{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009170 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02009171 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009172
9173 msg_scroll = sst->save_msg_scroll;
9174 restart_edit = sst->save_restart_edit;
9175 p_im = sst->save_insertmode;
9176 finish_op = sst->save_finish_op;
9177 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009178 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009179 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009180 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01009181 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009183 // Restore the state (needed when called from a function executed for
9184 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009185 State = sst->save_State;
9186#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009187 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009188#endif
9189}
9190
9191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 * ":normal[!] {commands}": Execute normal mode commands.
9193 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009194 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009195ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009197 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 char_u *arg = NULL;
9199 int l;
9200 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009202 if (ex_normal_lock > 0)
9203 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00009204 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009205 return;
9206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 if (ex_normal_busy >= p_mmd)
9208 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009209 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 return;
9211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 /*
9214 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9215 * this for the K_SPECIAL leading byte, otherwise special keys will not
9216 * work.
9217 */
9218 if (has_mbyte)
9219 {
9220 int len = 0;
9221
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009222 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 for (p = eap->arg; *p != NUL; ++p)
9224 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009225#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009226 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01009228#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009229 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009230 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009231#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 )
9235 len += 2;
9236 }
9237 if (len > 0)
9238 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009239 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 if (arg != NULL)
9241 {
9242 len = 0;
9243 for (p = eap->arg; *p != NUL; ++p)
9244 {
9245 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01009246#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 if (*p == CSI)
9248 {
9249 arg[len++] = KS_EXTRA;
9250 arg[len++] = (int)KE_CSI;
9251 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009252#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009253 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 {
9255 arg[len++] = *++p;
9256 if (*p == K_SPECIAL)
9257 {
9258 arg[len++] = KS_SPECIAL;
9259 arg[len++] = KE_FILLER;
9260 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009261#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 else if (*p == CSI)
9263 {
9264 arg[len++] = KS_EXTRA;
9265 arg[len++] = (int)KE_CSI;
9266 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 }
9269 arg[len] = NUL;
9270 }
9271 }
9272 }
9273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009275 ++ex_normal_busy;
9276 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 {
9278 /*
9279 * Repeat the :normal command for each line in the range. When no
9280 * range given, execute it just once, without positioning the cursor
9281 * first.
9282 */
9283 do
9284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 if (eap->addr_count != 0)
9286 {
9287 curwin->w_cursor.lnum = eap->line1++;
9288 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009289 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 }
9291
Bram Moolenaar13505972019-01-24 15:04:48 +01009292 exec_normal_cmd(arg != NULL
9293 ? arg
9294 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 }
9296 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9297 }
9298
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009299 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 update_topline_cursor();
9301
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009302 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009304 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009305#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009306 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009307#endif
9308
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310}
9311
9312/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009313 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 */
9315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009316ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009318 if (eap->forceit)
9319 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009320 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009321 if (!curwin->w_cursor.lnum)
9322 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009323 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009324 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009325#ifdef FEAT_TERMINAL
9326 // Ignore this when running in an active terminal.
9327 if (term_job_running(curbuf->b_term))
9328 return;
9329#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009330
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009331 // Ignore the command when already in Insert mode. Inserting an
9332 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009333 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009334 return;
9335
Bram Moolenaar64969662005-12-14 21:59:55 +00009336 if (eap->cmdidx == CMD_startinsert)
9337 restart_edit = 'a';
9338 else if (eap->cmdidx == CMD_startreplace)
9339 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009341 restart_edit = 'V';
9342
9343 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009345 if (eap->cmdidx == CMD_startinsert)
9346 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009347 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009349
9350 if (VIsual_active)
9351 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352}
9353
9354/*
9355 * ":stopinsert"
9356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009358ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359{
9360 restart_edit = 0;
9361 stop_insert_mode = TRUE;
Christian Brabandtcf665cc2025-04-12 18:09:28 +02009362#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
9363 // when called from remote_expr in insert mode, make sure insert mode is
9364 // ended by adding K_NOP to the typeahead buffer
9365 if (vgetc_busy)
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02009366 ins_char_typebuf(K_NOP, 0);
Christian Brabandtcf665cc2025-04-12 18:09:28 +02009367#endif
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009368 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009371/*
9372 * Execute normal mode command "cmd".
9373 * "remap" can be REMAP_NONE or REMAP_YES.
9374 */
9375 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009376exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009377{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009378 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009379 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009380 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009381}
Bram Moolenaar25281632016-01-21 23:32:32 +01009382
Bram Moolenaar25281632016-01-21 23:32:32 +01009383/*
9384 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009385 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009386 */
9387 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009388exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009389{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009390 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009391 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009392
Bram Moolenaar905dd902019-04-07 14:21:47 +02009393 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9394 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009395 clear_oparg(&oa);
9396 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009397 while ((!stuff_empty()
9398 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009399 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009400 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009401 {
9402 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009403#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009404 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009405 && oa.op_type == OP_NOP && oa.regname == NUL
9406 && !VIsual_active)
9407 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009408 // If terminal_loop() returns OK we got a key that is handled
9409 // in Normal model. With FAIL we first need to position the
9410 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009411 if (terminal_loop(TRUE) == OK)
9412 normal_cmd(&oa, TRUE);
9413 }
9414 else
9415#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009416 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009417 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009418 }
9419}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009420
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421#ifdef FEAT_FIND_ID
9422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009423ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424{
9425 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9426 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009427 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428}
9429
Bram Moolenaar4033c552017-09-16 20:54:51 +02009430#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431/*
9432 * ":psearch"
9433 */
9434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009435ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
9437 g_do_tagpreview = p_pvh;
9438 ex_findpat(eap);
9439 g_do_tagpreview = 0;
9440}
9441#endif
9442
9443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009444ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446 int whole = TRUE;
9447 long n;
9448 char_u *p;
9449 int action;
9450
9451 switch (cmdnames[eap->cmdidx].cmd_name[2])
9452 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009453 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9455 action = ACTION_GOTO;
9456 else
9457 action = ACTION_SHOW;
9458 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009459 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 action = ACTION_SHOW_ALL;
9461 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009462 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 action = ACTION_GOTO;
9464 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009465 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 action = ACTION_SPLIT;
9467 break;
9468 }
9469
9470 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009471 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 {
9473 n = getdigits(&eap->arg);
9474 eap->arg = skipwhite(eap->arg);
9475 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009476 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 {
9478 whole = FALSE;
9479 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009480 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (*p)
9482 {
9483 *p++ = NUL;
9484 p = skipwhite(p);
9485
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009486 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009487 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009488 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009490 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 }
9492 }
9493 if (!eap->skip)
9494 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9495 whole, !eap->forceit,
9496 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009497 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498}
9499#endif
9500
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
Bram Moolenaar4033c552017-09-16 20:54:51 +02009502#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
9504 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9505 */
9506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009507ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009509 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9511}
9512
9513/*
9514 * ":pedit"
9515 */
9516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009517ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 win_T *curwin_save = curwin;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009520 prepare_preview_window();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009522 // Edit the file.
9523 do_exedit(eap, NULL);
9524
9525 back_to_current_window(curwin_save);
9526}
9527
9528/*
9529 * ":pbuffer"
9530 */
9531 static void
9532ex_pbuffer(exarg_T *eap)
9533{
9534 win_T *curwin_save = curwin;
9535 prepare_preview_window();
9536
9537 // Go to the buffer.
9538 do_exbuffer(eap);
9539
9540 back_to_current_window(curwin_save);
9541}
9542
9543 static void
9544prepare_preview_window(void)
9545{
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009546 if (ERROR_IF_ANY_POPUP_WINDOW)
9547 return;
9548
Bram Moolenaar026587b2019-08-17 15:08:00 +02009549 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009551 prepare_tagpreview(TRUE, TRUE, FALSE);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009552}
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009553
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009554 static void
9555back_to_current_window(win_T *curwin_save)
9556{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 if (curwin != curwin_save && win_valid(curwin_save))
9558 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009559 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009561 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 win_enter(curwin_save, TRUE);
9563 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009564# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009565 else if (WIN_IS_POPUP(curwin))
9566 {
9567 // can't keep focus in popup window
9568 win_enter(firstwin, TRUE);
9569 }
9570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 g_do_tagpreview = 0;
9572}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574
9575/*
9576 * ":stag", ":stselect" and ":stjump".
9577 */
9578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009579ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009582 postponed_split_flags = cmdmod.cmod_split;
9583 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9585 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009586 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588
9589/*
9590 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9591 */
9592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009593ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9596}
9597
9598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009599ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
9601 int cmd;
9602
9603 switch (name[1])
9604 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009605 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009607 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009609 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009611 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009613 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009615 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009617 case 'f': // ":tfirst"
9618 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009620 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009622 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009624 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009626 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 return;
9628 }
9629#endif
9630 cmd = DT_TAG;
9631 break;
9632 }
9633
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009634 if (name[0] == 'l')
9635 {
9636#ifndef FEAT_QUICKFIX
9637 ex_ni(eap);
9638 return;
9639#else
9640 cmd = DT_LTAG;
9641#endif
9642 }
9643
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9645 eap->forceit, TRUE);
9646}
9647
John Marriotted908f72024-04-18 22:46:56 +02009648enum {
9649 SPEC_PERC = 0,
9650 SPEC_HASH,
9651 SPEC_CWORD, // cursor word
9652 SPEC_CCWORD, // cursor WORD
9653 SPEC_CEXPR, // expr under cursor
9654 SPEC_CFILE, // cursor path name
9655 SPEC_SFILE, // ":so" file name
9656 SPEC_SLNUM, // ":so" file line number
9657 SPEC_STACK, // call stack
9658 SPEC_SCRIPT, // script file name
9659 SPEC_AFILE, // autocommand file name
9660 SPEC_ABUF, // autocommand buffer number
9661 SPEC_AMATCH, // autocommand match name
9662 SPEC_SFLNUM, // script file line number
9663 SPEC_SID // script ID: <SNR>123_
9664#ifdef FEAT_CLIENTSERVER
9665 , SPEC_CLIENT
9666#endif
9667};
9668
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009670 * Check "str" for starting with a special cmdline variable.
9671 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9672 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9673 */
9674 int
Mike Williams51024bb2024-05-30 07:46:30 +02009675find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009676{
John Marriotted908f72024-04-18 22:46:56 +02009677 // must be sorted by the 'value' field because it is used by bsearch()!
9678 static keyvalue_T spec_str_tab[] = {
9679 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9680 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9681 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9682 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9683 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9684 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9685 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009686#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009687 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009688#endif
John Marriotted908f72024-04-18 22:46:56 +02009689 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9690 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9691 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9692 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9693 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9694 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009695 };
John Marriotted908f72024-04-18 22:46:56 +02009696 keyvalue_T target;
9697 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009698
John Marriotted908f72024-04-18 22:46:56 +02009699 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009700 {
John Marriotted908f72024-04-18 22:46:56 +02009701 case '%':
9702 *usedlen = 1;
9703 return SPEC_PERC;
9704
9705 case '#':
9706 *usedlen = 1;
9707 return SPEC_HASH;
9708
9709 case '<':
9710 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01009711 target.value.string = src + 1; // skip over '<'
9712 target.value.length = 0; // not used, see cmp_keyvalue_value_n()
John Marriotted908f72024-04-18 22:46:56 +02009713
John Marriott8d4477e2024-11-02 15:59:01 +01009714 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab,
9715 ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]),
9716 cmp_keyvalue_value_n);
John Marriotted908f72024-04-18 22:46:56 +02009717 if (entry == NULL)
9718 return -1;
9719
John Marriott8d4477e2024-11-02 15:59:01 +01009720 *usedlen = entry->value.length + 1;
John Marriotted908f72024-04-18 22:46:56 +02009721 return entry->key;
9722
9723 default:
9724 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009725 }
John Marriotted908f72024-04-18 22:46:56 +02009726
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009727 return -1;
9728}
9729
9730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 * Evaluate cmdline variables.
9732 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009733 * change "%" to curbuf->b_ffname
9734 * "#" to curwin->w_alt_fnum
9735 * "%%" to curwin->w_alt_fnum in Vim9 script
9736 * "<cword>" to word under the cursor
9737 * "<cWORD>" to WORD under the cursor
9738 * "<cexpr>" to C-expression under the cursor
9739 * "<cfile>" to path name under the cursor
9740 * "<sfile>" to sourced file name
9741 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009742 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009743 * "<slnum>" to sourced file line number
9744 * "<afile>" to file name for autocommand
9745 * "<abuf>" to buffer number for autocommand
9746 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 *
9748 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9749 * "" for error without a message) and NULL is returned.
9750 * Returns an allocated string if a valid match was found.
9751 * Returns NULL if no match was found. "usedlen" then still contains the
9752 * number of characters to skip.
9753 */
9754 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009755eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009756 char_u *src, // pointer into commandline
9757 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009758 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009759 linenr_T *lnump, // line number for :e command, or NULL
9760 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009761 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009762 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009763 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764{
9765 int i;
9766 char_u *s;
9767 char_u *result;
9768 char_u *resultbuf = NULL;
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +01009769 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009771 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009773 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776
9777 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009778 if (escaped != NULL)
9779 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780
9781 /*
9782 * Check if there is something to do.
9783 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009784 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009785 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 {
9787 *usedlen = 1;
9788 return NULL;
9789 }
9790
9791 /*
9792 * Skip when preceded with a backslash "\%" and "\#".
9793 * Note: In "\\%" the % is also not recognized!
9794 */
9795 if (src > srcstart && src[-1] == '\\')
9796 {
9797 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009798 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 return NULL;
9800 }
9801
9802 /*
9803 * word or WORD under cursor
9804 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009805 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9806 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009808 resultlen = find_ident_under_cursor(&result,
9809 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9810 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9811 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 if (resultlen == 0)
9813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009814 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 return NULL;
9816 }
9817 }
9818
9819 /*
9820 * '#': Alternate file name
9821 * '%': Current file name
9822 * File name under the cursor
9823 * File name for autocommand
9824 * and following modifiers
9825 */
9826 else
9827 {
Mike Williams51024bb2024-05-30 07:46:30 +02009828 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009829
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 switch (spec_idx)
9831 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009832 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009833#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009834 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009837 // '%': current file
9838 if (curbuf->b_fname == NULL)
9839 {
9840 result = (char_u *)"";
9841 valid = 0; // Must have ":p:h" to be valid
9842 }
9843 else
9844 {
9845 result = curbuf->b_fname;
9846 tilde_file = STRCMP(result, "~") == 0;
9847 }
9848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009850#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009851 // "%%" alternate file
9852 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009853#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009854 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009855 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009856 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009858 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 result = arg_all();
9860 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009861 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009862 if (escaped != NULL)
9863 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 break;
9866 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009867 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009868 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009869 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009871 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009872 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009873 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009874 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009876 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009878 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009879 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009880 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009881 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009882 return NULL;
9883 }
9884#ifdef FEAT_EVAL
9885 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9886 (long)i);
9887 if (result == NULL)
9888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009889 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009890 return NULL;
9891 }
9892#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009893 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 }
9897 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009898 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009899 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9900 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009901 buf = buflist_findnr(i);
9902 if (buf == NULL)
9903 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009904 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009905 return NULL;
9906 }
9907 if (lnump != NULL)
9908 *lnump = ECMD_LAST;
9909 if (buf->b_fname == NULL)
9910 {
9911 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009912 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009913 }
9914 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009915 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009916 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009917 tilde_file = STRCMP(result, "~") == 0;
9918 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 break;
9921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009922 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009923 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 if (result == NULL)
9925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009926 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 return NULL;
9928 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009929 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009932 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009934 if (result != NULL && !autocmd_fname_full)
9935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009936 // Still need to turn the fname into a full path. It is
9937 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009938 autocmd_fname_full = TRUE;
9939 result = FullName_save(autocmd_fname, FALSE);
9940 vim_free(autocmd_fname);
9941 autocmd_fname = result;
9942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 if (result == NULL)
9944 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009945 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 return NULL;
9947 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009948 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 break;
9950
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009951 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 if (autocmd_bufnr <= 0)
9953 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009954 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 return NULL;
9956 }
9957 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9958 result = strbuf;
9959 break;
9960
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009961 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 result = autocmd_match;
9963 if (result == NULL)
9964 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009965 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 return NULL;
9967 }
9968 break;
9969
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009970 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009971 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 if (result == NULL)
9973 {
LemonBoy6013d002022-04-09 21:42:10 +01009974 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9975 return NULL;
9976 }
9977 resultbuf = result; // remember allocated string
9978 break;
9979 case SPEC_STACK: // call stack
9980 result = estack_sfile(ESTACK_STACK);
9981 if (result == NULL)
9982 {
9983 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9984 return NULL;
9985 }
9986 resultbuf = result; // remember allocated string
9987 break;
9988 case SPEC_SCRIPT: // script file name
9989 result = estack_sfile(ESTACK_SCRIPT);
9990 if (result == NULL)
9991 {
9992 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 return NULL;
9994 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009995 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009998 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009999 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010000 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +000010001 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010002 return NULL;
10003 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010010004 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010005 result = strbuf;
10006 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010007
10008#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010009 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010010010 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010011 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +000010012 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010013 return NULL;
10014 }
10015 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010010016 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010017 result = strbuf;
10018 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010019
Bram Moolenaar90944302020-08-01 20:45:11 +020010020 case SPEC_SID:
10021 if (current_sctx.sc_sid <= 0)
10022 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +000010023 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +020010024 return NULL;
10025 }
10026 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
10027 result = strbuf;
10028 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +020010029#endif
Bram Moolenaar90944302020-08-01 20:45:11 +020010030
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010031#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010032 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010033 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10034 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 result = strbuf;
10036 break;
10037#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010038
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010039 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010040 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010041 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 }
10043
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010044 resultlen = STRLEN(result); // length of new string
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010045 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 {
10047 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010049 resultlen = s - result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 else if (!skip_mod)
10052 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010053 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 &resultlen);
10055 if (result == NULL)
10056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010057 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 return NULL;
10059 }
10060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 }
10062
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010063 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010065 if (empty_is_error)
10066 {
10067 if (valid != VALID_HEAD + VALID_PATH)
10068 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
10069 else
10070 *errormsg = _(e_evaluates_to_an_empty_string);
10071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 result = NULL;
10073 }
10074 else
10075 result = vim_strnsave(result, resultlen);
10076 vim_free(resultbuf);
10077 return result;
10078}
10079
10080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 * Expand the <sfile> string in "arg".
10082 *
10083 * Returns an allocated string, or NULL for any error.
10084 */
10085 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010086expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010088 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +020010090 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +020010092 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +020010094 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +020010095 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 char_u *p;
10097
John Marriotted908f72024-04-18 22:46:56 +020010098 resultlen = STRLEN(arg);
10099 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 if (result == NULL)
10101 return NULL;
10102
10103 for (p = result; *p; )
10104 {
10105 if (STRNCMP(p, "<sfile>", 7) != 0)
10106 ++p;
10107 else
10108 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010109 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +010010110 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 if (errormsg != NULL)
10112 {
10113 if (*errormsg)
10114 emsg(errormsg);
10115 vim_free(result);
10116 return NULL;
10117 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010118 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 {
10120 p += srclen;
10121 continue;
10122 }
John Marriotted908f72024-04-18 22:46:56 +020010123 repllen = STRLEN(repl);
10124 resultlen += repllen - srclen;
10125 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 if (newres == NULL)
10127 {
10128 vim_free(repl);
10129 vim_free(result);
10130 return NULL;
10131 }
John Marriotted908f72024-04-18 22:46:56 +020010132 len = p - result;
10133 mch_memmove(newres, result, len);
10134 STRCPY(newres + len, repl);
10135 len += repllen;
10136 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 vim_free(repl);
10138 vim_free(result);
10139 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010140 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 }
10142 }
10143
10144 return result;
10145}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010148/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010149 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010150 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010153dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 if (fname == NULL)
10156 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010157 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158}
10159#endif
10160
10161/*
10162 * ":behave {mswin,xterm}"
10163 */
10164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010165ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166{
10167 if (STRCMP(eap->arg, "mswin") == 0)
10168 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010169 set_option_value_give_err((char_u *)"selection",
10170 0L, (char_u *)"exclusive", 0);
10171 set_option_value_give_err((char_u *)"selectmode",
10172 0L, (char_u *)"mouse,key", 0);
10173 set_option_value_give_err((char_u *)"mousemodel",
10174 0L, (char_u *)"popup", 0);
10175 set_option_value_give_err((char_u *)"keymodel",
10176 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 }
10178 else if (STRCMP(eap->arg, "xterm") == 0)
10179 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010180 set_option_value_give_err((char_u *)"selection",
10181 0L, (char_u *)"inclusive", 0);
10182 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
10183 set_option_value_give_err((char_u *)"mousemodel",
10184 0L, (char_u *)"extend", 0);
10185 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 }
10187 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010188 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189}
10190
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191static int filetype_detect = FALSE;
10192static int filetype_plugin = FALSE;
10193static int filetype_indent = FALSE;
10194
10195/*
10196 * ":filetype [plugin] [indent] {on,off,detect}"
10197 * on: Load the filetype.vim file to install autocommands for file types.
10198 * off: Load the ftoff.vim file to remove all autocommands for file types.
10199 * plugin on: load filetype.vim and ftplugin.vim
10200 * plugin off: load ftplugof.vim
10201 * indent on: load filetype.vim and indent.vim
10202 * indent off: load indoff.vim
10203 */
10204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010205ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206{
10207 char_u *arg = eap->arg;
10208 int plugin = FALSE;
10209 int indent = FALSE;
10210
10211 if (*eap->arg == NUL)
10212 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010213 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010214 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 filetype_detect ? "ON" : "OFF",
10216 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10217 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10218 return;
10219 }
10220
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010221 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 for (;;)
10223 {
10224 if (STRNCMP(arg, "plugin", 6) == 0)
10225 {
10226 plugin = TRUE;
10227 arg = skipwhite(arg + 6);
10228 continue;
10229 }
10230 if (STRNCMP(arg, "indent", 6) == 0)
10231 {
10232 indent = TRUE;
10233 arg = skipwhite(arg + 6);
10234 continue;
10235 }
10236 break;
10237 }
10238 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10239 {
10240 if (*arg == 'o' || !filetype_detect)
10241 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010242 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 filetype_detect = TRUE;
10244 if (plugin)
10245 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010246 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 filetype_plugin = TRUE;
10248 }
10249 if (indent)
10250 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010251 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 filetype_indent = TRUE;
10253 }
10254 }
10255 if (*arg == 'd')
10256 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010257 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010258 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 }
10260 }
10261 else if (STRCMP(arg, "off") == 0)
10262 {
10263 if (plugin || indent)
10264 {
10265 if (plugin)
10266 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010267 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 filetype_plugin = FALSE;
10269 }
10270 if (indent)
10271 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010272 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 filetype_indent = FALSE;
10274 }
10275 }
10276 else
10277 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010278 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 filetype_detect = FALSE;
10280 }
10281 }
10282 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010283 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284}
10285
10286/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010287 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 */
10289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010290ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291{
zeertzjq5bf6c212024-03-31 18:41:27 +020010292 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010293 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010294
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010295 char_u *arg = eap->arg;
10296 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10297 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010298
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010299 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10300 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +020010301 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010305ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306{
10307#ifdef FEAT_DIGRAPHS
10308 if (*eap->arg != NUL)
10309 putdigraph(eap->arg);
10310 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010311 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312#else
mityu61065042021-07-19 20:07:21 +020010313 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314#endif
10315}
10316
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010317#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10318 void
10319set_no_hlsearch(int flag)
10320{
10321 no_hlsearch = flag;
10322# ifdef FEAT_EVAL
10323 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10324# endif
10325}
10326
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327/*
10328 * ":nohlsearch"
10329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010331ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010333 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010334 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336#endif
10337
10338#ifdef FEAT_CRYPT
10339/*
10340 * ":X": Get crypt key
10341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010343ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010345 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010346 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347}
10348#endif
10349
10350#ifdef FEAT_FOLDING
10351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010352ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
10354 if (foldManualAllowed(TRUE))
10355 foldCreate(eap->line1, eap->line2);
10356}
10357
10358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010359ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
10361 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10362 eap->forceit, FALSE);
10363}
10364
10365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010366ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367{
10368 linenr_T lnum;
10369
Bram Moolenaar4facea32019-10-12 20:17:40 +020010370# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010371 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010372# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010373
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010374 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10376 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10377 ml_setmarked(lnum);
10378
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010379 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010381 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010382# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010383 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385}
10386#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010388#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010389/*
10390 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10391 * instead of a quickfix command.
10392 */
10393 int
10394is_loclist_cmd(int cmdidx)
10395{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010396 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010397 return FALSE;
10398 return cmdnames[cmdidx].cmd_name[0] == 'l';
10399}
10400#endif
10401
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010402 int
10403get_pressedreturn(void)
10404{
10405 return ex_pressedreturn;
10406}
10407
10408 void
10409set_pressedreturn(int val)
10410{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +020010411 ex_pressedreturn = val;
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010412}