blob: 4ccc5f7592992c9adffb261f147267ab7c7983bf [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +010039static void do_exbuffer(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010040static void ex_bmodified(exarg_T *eap);
41static void ex_bnext(exarg_T *eap);
42static void ex_bprevious(exarg_T *eap);
43static void ex_brewind(exarg_T *eap);
44static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifndef FEAT_QUICKFIX
48# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000049# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# define ex_cc ex_ni
51# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020052# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define ex_cfile ex_ni
54# define qf_list ex_ni
55# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020056# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000058# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# define ex_cclose ex_ni
60# define ex_copen ex_ni
61# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020062# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000064#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
65# define ex_cexpr ex_ni
66#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020068static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020069static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020070static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020072#if !defined(FEAT_PERL) \
73 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
74 || !defined(FEAT_TCL) \
75 || !defined(FEAT_RUBY) \
76 || !defined(FEAT_LUA) \
77 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000078# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010081static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010082static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000083#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000085#endif
John Marriotted908f72024-04-18 22:46:56 +020086static char_u *repl_cmdline(exarg_T *eap, char_u *src, size_t srclen, char_u *repl, char_u **cmdlinep);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static void ex_highlight(exarg_T *eap);
88static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void ex_cquit(exarg_T *eap);
90static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static void ex_close(exarg_T *eap);
92static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
93static void ex_only(exarg_T *eap);
94static void ex_resize(exarg_T *eap);
95static void ex_stag(exarg_T *eap);
96static void ex_tabclose(exarg_T *eap);
97static void ex_tabonly(exarg_T *eap);
98static void ex_tabnext(exarg_T *eap);
99static void ex_tabmove(exarg_T *eap);
100static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200101#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100102static void ex_pclose(exarg_T *eap);
103static void ex_ptag(exarg_T *eap);
104static void ex_pedit(exarg_T *eap);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100105static void ex_pbuffer(exarg_T *eap);
106static void prepare_preview_window(void);
107static void back_to_current_window(win_T *curwin_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#else
109# define ex_pclose ex_ni
110# define ex_ptag ex_ni
111# define ex_pedit ex_ni
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +0100112# define ex_pbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100114static void ex_hide(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_exit(exarg_T *eap);
116static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#else
120# define ex_goto ex_ni
121#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_shell(exarg_T *eap);
123static void ex_preserve(exarg_T *eap);
124static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100125static void ex_mode(exarg_T *eap);
126static void ex_wrongmodifier(exarg_T *eap);
127static void ex_find(exarg_T *eap);
128static void ex_open(exarg_T *eap);
129static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifndef FEAT_GUI
131# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100134#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100135static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#else
137# define ex_tearoff ex_ni
138#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100139#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
140 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#else
143# define ex_popup ex_ni
144#endif
145#ifndef FEAT_GUI_MSWIN
146# define ex_simalt ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define gui_mch_find_dialog ex_ni
150# define gui_mch_replace_dialog ex_ni
151#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000152#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define ex_helpfind ex_ni
154#endif
155#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100156# define ex_cscope ex_ni
157# define ex_scscope ex_ni
158# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
160#ifndef FEAT_SYN_HL
161# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200162# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200164#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200165# define ex_syntime ex_ni
166#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000167#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000168# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000170# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000171# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000172# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000173#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200174#ifndef FEAT_PERSISTENT_UNDO
175# define ex_rundo ex_ni
176# define ex_wundo ex_ni
177#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200178#ifndef FEAT_LUA
179# define ex_lua ex_script_ni
180# define ex_luado ex_ni
181# define ex_luafile ex_ni
182#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000183#ifndef FEAT_MZSCHEME
184# define ex_mzscheme ex_script_ni
185# define ex_mzfile ex_ni
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#ifndef FEAT_PERL
188# define ex_perl ex_script_ni
189# define ex_perldo ex_ni
190#endif
191#ifndef FEAT_PYTHON
192# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200193# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# define ex_pyfile ex_ni
195#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200197# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200198# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200199# define ex_py3file ex_ni
200#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100201#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
202# define ex_pyx ex_script_ni
203# define ex_pyxdo ex_ni
204# define ex_pyxfile ex_ni
205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifndef FEAT_TCL
207# define ex_tcl ex_script_ni
208# define ex_tcldo ex_ni
209# define ex_tclfile ex_ni
210#endif
211#ifndef FEAT_RUBY
212# define ex_ruby ex_script_ni
213# define ex_rubydo ex_ni
214# define ex_rubyfile ex_ni
215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifndef FEAT_KEYMAP
217# define ex_loadkeymap ex_ni
218#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void ex_swapname(exarg_T *eap);
220static void ex_syncbind(exarg_T *eap);
221static void ex_read(exarg_T *eap);
222static void ex_pwd(exarg_T *eap);
223static void ex_equal(exarg_T *eap);
224static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000227#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#else
230# define ex_winpos ex_ni
231#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ex_operators(exarg_T *eap);
233static void ex_put(exarg_T *eap);
64-bitmane08f10a2025-03-18 22:14:34 +0100234static void ex_iput(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100235static void ex_copymove(exarg_T *eap);
236static void ex_submagic(exarg_T *eap);
237static void ex_join(exarg_T *eap);
238static void ex_at(exarg_T *eap);
239static void ex_bang(exarg_T *eap);
240static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_wundo(exarg_T *eap);
243static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200244#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redo(exarg_T *eap);
246static void ex_later(exarg_T *eap);
247static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100249static void ex_redrawtabline(exarg_T *eap);
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200250static void ex_redrawtabpanel(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ex_startinsert(exarg_T *eap);
254static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ex_checkpath(exarg_T *eap);
257static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#else
259# define ex_findpat ex_ni
260# define ex_checkpath ex_ni
261#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200262#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#else
265# define ex_psearch ex_ni
266#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ex_tag(exarg_T *eap);
268static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200270# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200271# define ex_break ex_ni
272# define ex_breakadd ex_ni
273# define ex_breakdel ex_ni
274# define ex_breaklist ex_ni
275# define ex_call ex_ni
276# define ex_catch ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000277# define ex_class ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200279# define ex_continue ex_ni
280# define ex_debug ex_ni
281# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200282# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_echo ex_ni
286# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200288# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200289# define ex_endfunction ex_ni
290# define ex_endif ex_ni
291# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200293# define ex_eval ex_ni
294# define ex_execute ex_ni
295# define ex_finally ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000296# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_finish ex_ni
298# define ex_function ex_ni
299# define ex_if ex_ni
300# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200301# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200302# define ex_lockvar ex_ni
303# define ex_oldfiles ex_ni
304# define ex_options ex_ni
305# define ex_packadd ex_ni
306# define ex_packloadall ex_ni
307# define ex_return ex_ni
308# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_throw ex_ni
310# define ex_try ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000311# define ex_type ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_unlet ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200313# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314# define ex_import ex_ni
315# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200317#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318# define ex_loadview ex_ni
319#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200320#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321# define ex_viminfo ex_ni
322#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_filetype(exarg_T *eap);
325static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000327# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328# define ex_diffpatch ex_ni
329# define ex_diffgetput ex_ni
330# define ex_diffsplit ex_ni
331# define ex_diffthis ex_ni
332# define ex_diffupdate ex_ni
333#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_nohlsearch ex_ni
339# define ex_match ex_ni
340#endif
341#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_X ex_ni
345#endif
346#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100347static void ex_fold(exarg_T *eap);
348static void ex_foldopen(exarg_T *eap);
349static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#else
351# define ex_fold ex_ni
352# define ex_foldopen ex_ni
353# define ex_folddo ex_ni
354#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100355#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356# define ex_language ex_ni
357#endif
358#ifndef FEAT_SIGNS
359# define ex_sign ex_ni
360#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000361#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200362# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000363# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200364# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
Bram Moolenaar05159a02005-02-26 23:04:13 +0000367#ifndef FEAT_PROFILE
368# define ex_profile ex_ni
369#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200370#ifndef FEAT_TERMINAL
371# define ex_terminal ex_ni
372#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200373#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
374# define ex_xrestore ex_ni
375#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100376#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200377# define ex_popupclear ex_ni
378#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Declare cmdnames[].
382 */
383#define DO_DECLARE_EXCMD
384#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200385#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static char_u dollar_command[2] = {'$', 0};
388
389
390#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392typedef struct
393{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100394 char_u *line; // command line
395 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396} wcmd_T;
397
398/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 garray_T *lines_gap; // growarray with line info
406 int current_line; // last read line from growarray
407 int repeating; // TRUE when looping a second time
408 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100409 char_u *(*lc_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 void *cookie;
411};
412
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200413static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static int store_loop_line(garray_T *gap, char_u *line);
415static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000418struct dbg_stuff
419{
420 int trylevel;
421 int force_abort;
422 except_T *caught_stack;
423 char_u *vv_exception;
424 char_u *vv_throwpoint;
425 int did_emsg;
426 int got_int;
427 int did_throw;
428 int need_rethrow;
429 int check_cstack;
430 except_T *current_exception;
431};
432
Bram Moolenaared203462004-06-16 11:19:22 +0000433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100434save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000435{
436 dsp->trylevel = trylevel; trylevel = 0;
437 dsp->force_abort = force_abort; force_abort = FALSE;
438 dsp->caught_stack = caught_stack; caught_stack = NULL;
439 dsp->vv_exception = v_exception(NULL);
440 dsp->vv_throwpoint = v_throwpoint(NULL);
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000443 dsp->did_emsg = did_emsg; did_emsg = FALSE;
444 dsp->got_int = got_int; got_int = FALSE;
445 dsp->did_throw = did_throw; did_throw = FALSE;
446 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
447 dsp->check_cstack = check_cstack; check_cstack = FALSE;
448 dsp->current_exception = current_exception; current_exception = NULL;
449}
450
451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100452restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000453{
454 suppress_errthrow = FALSE;
455 trylevel = dsp->trylevel;
456 force_abort = dsp->force_abort;
457 caught_stack = dsp->caught_stack;
458 (void)v_exception(dsp->vv_exception);
459 (void)v_throwpoint(dsp->vv_throwpoint);
460 did_emsg = dsp->did_emsg;
461 got_int = dsp->got_int;
462 did_throw = dsp->did_throw;
463 need_rethrow = dsp->need_rethrow;
464 check_cstack = dsp->check_cstack;
465 current_exception = dsp->current_exception;
466}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467#endif
468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469/*
Colin Kennedy65e580b2024-03-26 18:29:30 +0100470 * Check if ffname differs from fnum.
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200471 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid
472 * buffer ID.
Colin Kennedy65e580b2024-03-26 18:29:30 +0100473 * ffname is a full path to where a buffer lives on-disk or would live on-disk.
474 *
475 */
476 static int
477is_other_file(int fnum, char_u *ffname)
478{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200479 if (fnum != 0)
480 {
481 if (fnum == curbuf->b_fnum)
482 return FALSE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100483
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200484 return TRUE;
485 }
Colin Kennedy65e580b2024-03-26 18:29:30 +0100486
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200487 if (ffname == NULL)
488 return TRUE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100489
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200490 if (*ffname == NUL)
491 return FALSE;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100492
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200493 // TODO: Need a reliable way to know whether a buffer is meant to live
494 // on-disk !curbuf->b_dev_valid is not always available (example: missing
495 // on Windows)
496 if (curbuf->b_sfname != NULL
497 && *curbuf->b_sfname != NUL)
498 // This occurs with unsaved buffers. In which case `ffname` actually
499 // corresponds to curbuf->b_sfname
500 return fnamecmp(ffname, curbuf->b_sfname) != 0;
Colin Kennedy65e580b2024-03-26 18:29:30 +0100501
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200502 return otherfile(ffname);
Colin Kennedy65e580b2024-03-26 18:29:30 +0100503}
504
505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
507 * command is given.
508 */
509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100510do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100511 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512{
513 int save_msg_scroll;
514 int prev_msg_row;
515 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100516 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000517
518 if (improved)
519 exmode_active = EXMODE_VIM;
520 else
521 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100522 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100523 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000524
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100525 // When using ":global /pat/ visual" and then "Q" we return to continue
526 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if (global_busy)
528 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
530 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 ++RedrawingDisabled; // don't redisplay the window
532 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100534 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 ++hold_gui_events;
536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
Bram Moolenaar32526b32019-01-19 17:43:09 +0100538 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 while (exmode_active)
540 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100541 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000542 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
543 {
544 exmode_active = FALSE;
545 break;
546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 msg_scroll = TRUE;
548 need_wait_return = FALSE;
549 ex_pressedreturn = FALSE;
550 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100551 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 prev_msg_row = msg_row;
553 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (improved)
555 {
556 cmdline_row = msg_row;
557 do_cmdline(NULL, getexline, NULL, 0);
558 }
559 else
560 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
561 lines_left = Rows - 1;
562
Bram Moolenaardf177f62005-02-22 08:39:57 +0000563 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100564 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000566 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000567 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000568 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000570 if (ex_pressedreturn)
571 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100572 // go up one line, to overwrite the ":<CR>" line, so the
573 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000574 msg_row = prev_msg_row;
575 if (prev_msg_row == Rows - 1)
576 msg_row--;
577 }
578 msg_col = 0;
579 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
580 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100583 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000584 {
585 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000586 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000587 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000588 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 }
591
592#ifdef FEAT_GUI
593 --hold_gui_events;
594#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100595 if (RedrawingDisabled > 0)
596 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100598 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 need_wait_return = FALSE;
600 msg_scroll = save_msg_scroll;
601}
602
603/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200604 * Print the executed command for when 'verbose' is set.
605 * When "lnum" is 0 only print the command.
606 */
607 static void
608msg_verbose_cmd(linenr_T lnum, char_u *cmd)
609{
610 ++no_wait_return;
611 verbose_enter_scroll();
612
613 if (lnum == 0)
614 smsg(_("Executing: %s"), cmd);
615 else
616 smsg(_("line %ld: %s"), (long)lnum, cmd);
617 if (msg_silent == 0)
618 msg_puts("\n"); // don't overwrite this
619
620 verbose_leave_scroll();
621 --no_wait_return;
622}
623
624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 * Execute a simple command line. Used for translated commands like "*".
626 */
627 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100628do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 return do_cmdline(cmd, NULL, NULL,
631 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
632}
633
634/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100635 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
636 * This allows for using a range without ":" in Vim9 script.
637 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200638 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100639do_cmd_argument(char_u *cmd)
640{
641 return do_cmdline(cmd, NULL, NULL,
642 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
643}
644
645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 * do_cmdline(): execute one Ex command line
647 *
648 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 * 2. Split up in parts separated with '|'.
651 *
652 * This function can be called recursively!
653 *
654 * flags:
655 * DOCMD_VERBOSE - The command will be included in the error message.
656 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100657 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 * DOCMD_KEYTYPED - Don't reset KeyTyped.
659 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
660 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
661 *
662 * return FAIL if cmdline could not be executed, OK otherwise
663 */
664 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100665do_cmdline(
666 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200667 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100669 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100671 char_u *next_cmdline; // next cmd to execute
672 char_u *cmdline_copy = NULL; // copy of cmd line
673 int used_getline = FALSE; // used "fgetline" to obtain command
674 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100677 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 int retval = OK;
679#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100680 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100681 garray_T lines_ga; // keep lines for ":while"/":for"
682 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200683 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100684 char_u *fname = NULL; // function or script name
685 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
686 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
687 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200689 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200690 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100692 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200693 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000695 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100699# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700# define cmd_cookie cookie
701#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200703#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
705 // location for storing error messages to be converted to an exception.
706 // This ensures that the do_errthrow() call in do_one_cmd() does not
707 // combine the messages stored by an earlier invocation of do_one_cmd()
708 // with the command name of the later one. This would happen when
709 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 saved_msg_list = msg_list;
711 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#endif
713
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100714 // It's possible to create an endless loop with ":execute", catch that
715 // here. The value of 200 allows nested function calls, ":source", etc.
716 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100717 if (call_depth >= 200
718#ifdef FEAT_EVAL
719 && call_depth >= p_mfd
720#endif
721 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000723 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100725 // When converting to an exception, we do not include the command name
726 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100727 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 msg_list = saved_msg_list;
729#endif
730 return FAIL;
731 }
732 ++call_depth;
733
734#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100735 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000737 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100739 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100741 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100742 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000743 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 ++ex_nesting_level;
745
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100746 // Get the function or script name and the address where the next breakpoint
747 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000748 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
750 fname = func_name(real_cookie);
751 breakpoint = func_breakpoint(real_cookie);
752 dbg_tick = func_dbg_tick(real_cookie);
753 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100756 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 breakpoint = source_breakpoint(real_cookie);
758 dbg_tick = source_dbg_tick(real_cookie);
759 }
760
761 /*
762 * Initialize "force_abort" and "suppress_errthrow" at the top level.
763 */
764 if (!recursive)
765 {
766 force_abort = FALSE;
767 suppress_errthrow = FALSE;
768 }
769
770 /*
771 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000772 * exception handling (used when debugging). Otherwise clear it to avoid
773 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000775 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000776 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000777 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200778 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 initial_trylevel = trylevel;
781
782 /*
783 * "did_throw" will be set to TRUE when an exception is being thrown.
784 */
785 did_throw = FALSE;
786#endif
787 /*
788 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * If force_abort is set, we cancel everything.
791 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100792#ifdef FEAT_EVAL
793 did_emsg_cumul += did_emsg;
794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 did_emsg = FALSE;
796
797 /*
798 * KeyTyped is only set when calling vgetc(). Reset it here when not
799 * calling vgetc() (sourced command lines).
800 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100801 if (!(flags & DOCMD_KEYTYPED)
802 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 KeyTyped = FALSE;
804
805 /*
806 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000807 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 * - for multiple commands on one line, separated with '|'
809 * - when repeating until there are no more lines (for ":source")
810 */
811 next_cmdline = cmdline;
812 do
813 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816#endif
817
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100818 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (next_cmdline == NULL
820#ifdef FEAT_EVAL
821 && !force_abort
822 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000823 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#endif
825 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100826 {
827#ifdef FEAT_EVAL
828 did_emsg_cumul += did_emsg;
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000834 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * 3. If a line is given: Make a copy, so we can mess with it.
837 */
838
839#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000841 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100843 // Each '|' separated command is stored separately in lines_ga, to
844 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100845 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100847 // Check if a function has returned or, unless it has an unclosed
848 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000852 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853 func_line_end(real_cookie);
854# endif
855 if (func_has_ended(real_cookie))
856 {
857 retval = FAIL;
858 break;
859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000862 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864 script_line_end();
865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100867 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 {
870 retval = FAIL;
871 break;
872 }
873
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (breakpoint != NULL && dbg_tick != NULL
876 && *dbg_tick != debug_tick)
877 {
878 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100879 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100880 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 *dbg_tick = debug_tick;
882 }
883
884 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100885 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100887 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100889 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100891 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100892 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100894 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100895 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 *dbg_tick = debug_tick;
897 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000899 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000900 {
901 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100902 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100903 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904 script_line_start();
905 }
906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#endif
909
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100910 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (next_cmdline == NULL)
912 {
913 /*
914 * Need to set msg_didout for the first line after an ":if",
915 * otherwise the ":if" will be overwritten.
916 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100917 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100919 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920#ifdef FEAT_EVAL
921 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
922#else
923 0
924#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100925 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
926 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100928 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100929 // returned for the end of a sourced file or executed function
930 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (KeyTyped && !(flags & DOCMD_REPEAT))
932 need_wait_return = FALSE;
933 retval = FAIL;
934 break;
935 }
936 used_getline = TRUE;
937
938 /*
939 * Keep the first typed line. Clear it when more lines are typed.
940 */
941 if (flags & DOCMD_KEEPLINE)
942 {
943 vim_free(repeat_cmdline);
944 if (count == 0)
945 repeat_cmdline = vim_strsave(next_cmdline);
946 else
947 repeat_cmdline = NULL;
948 }
949 }
950
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100951 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 else if (cmdline_copy == NULL)
953 {
954 next_cmdline = vim_strsave(next_cmdline);
955 if (next_cmdline == NULL)
956 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200957 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = FAIL;
959 break;
960 }
961 }
962 cmdline_copy = next_cmdline;
963
964#ifdef FEAT_EVAL
965 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200966 * Inside a while/for loop, and when the command looks like a ":while"
967 * or ":for", the line is stored, because we may need it later when
968 * looping.
969 *
970 * When there is a '|' and another command, it is stored separately,
971 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000972 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200973 *
974 * Pass a different "fgetline" function to do_one_cmd() below,
975 * that it stores lines in or reads them from "lines_ga". Makes it
976 * possible to define a function inside a while/for loop and handles
977 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200979 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200981 cmd_getline = get_loop_line;
982 cmd_cookie = (void *)&cmd_loop_cookie;
983 cmd_loop_cookie.lines_gap = &lines_ga;
984 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100985 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200986 cmd_loop_cookie.cookie = cookie;
987 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
988
989 // Save the current line when encountering it the first time.
990 if (current_line == lines_ga.ga_len
991 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
993 retval = FAIL;
994 break;
995 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200996 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200998 else
999 {
1000 cmd_getline = fgetline;
1001 cmd_cookie = cookie;
1002 }
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 did_endif = FALSE;
1005#endif
1006
1007 if (count++ == 0)
1008 {
1009 /*
1010 * All output from the commands is put below each other, without
1011 * waiting for a return. Don't do this when executing commands
1012 * from a script or when being called recursive (e.g. for ":e
1013 * +command file").
1014 */
1015 if (!(flags & DOCMD_NOWAIT) && !recursive)
1016 {
1017 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001018 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001020 msg_scroll = TRUE; // put messages below each other
1021 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001023 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
1025 }
1026
Bram Moolenaar823654b2020-05-29 23:03:09 +02001027 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001028 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 /*
1031 * 2. Execute one '|' separated command.
1032 * do_one_cmd() will return NULL if there is no trailing '|'.
1033 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1034 */
1035 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001036 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037#ifdef FEAT_EVAL
1038 &cstack,
1039#endif
1040 cmd_getline, cmd_cookie);
1041 --recursive;
1042
1043#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001045 // Use "current_line" from "cmd_loop_cookie", it may have been
1046 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048#endif
1049
1050 if (next_cmdline == NULL)
1051 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001052 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 /*
1055 * If the command was typed, remember it for the ':' register.
1056 * Do this AFTER executing the command to make :@: work.
1057 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001058 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 && new_last_cmdline != NULL)
1060 {
1061 vim_free(last_cmdline);
1062 last_cmdline = new_last_cmdline;
1063 new_last_cmdline = NULL;
1064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 else
1067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // need to copy the command after the '|' to cmdline_copy, for the
1069 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001070 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 next_cmdline = cmdline_copy;
1072 }
1073
1074
1075#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001078 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001080 {
1081 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
1087 ++current_line;
1088
1089 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 * An ":endwhile", ":endfor" and ":continue" is handled here.
1091 * If we were executing commands, jump back to the ":while" or
1092 * ":for".
1093 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001099 // Jump back to the matching ":while" or ":for". Be careful
1100 // not to use a cs_line[] from an entry that isn't a ":while"
1101 // or ":for": It would make "current_line" invalid and can
1102 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (!did_emsg && !got_int && !did_throw
1104 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001105 && (cstack.cs_flags[cstack.cs_idx]
1106 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 && cstack.cs_line[cstack.cs_idx] >= 0
1108 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1109 {
1110 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001111 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001113 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001115 // Check for the next breakpoint at or after the ":while"
1116 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001117 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001120 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 fname,
1122 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1123 *dbg_tick = debug_tick;
1124 }
1125 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001126 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001128 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001130 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1131 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133 }
1134
1135 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001141 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 }
1144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001145 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001146 if (breakpoint != NULL && has_watchexpr())
1147 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001148 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001149 *dbg_tick = debug_tick;
1150 }
1151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 /*
1153 * When not inside any ":while" loop, clear remembered lines.
1154 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
1157 if (lines_ga.ga_len > 0)
1158 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001159 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1161 free_cmdlines(&lines_ga);
1162 }
1163 current_line = 0;
1164 }
1165
1166 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001167 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1168 * being restored at the ":endtry". Reset them here and set the
1169 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1170 * This includes the case where a missing ":endif", ":endwhile" or
1171 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 cstack.cs_lflags &= ~CSL_HAD_FINA;
1176 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1177 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 did_throw ? (void *)current_exception : NULL);
1179 did_emsg = got_int = did_throw = FALSE;
1180 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1181 }
1182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001183 // Update global "trylevel" for recursive calls to do_cmdline() from
1184 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 trylevel = initial_trylevel + cstack.cs_trylevel;
1186
1187 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001188 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 * files) is aborted because of an error, an interrupt, or an uncaught
1190 * exception, cancel everything. If it is left normally, reset
1191 * force_abort to get the non-EH compatible abortion behavior for
1192 * the rest of the script.
1193 */
1194 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1195 force_abort = FALSE;
1196
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001197 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001199#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201 }
1202 /*
1203 * Continue executing command lines when:
1204 * - no CTRL-C typed, no aborting error, no exception thrown or try
1205 * conditionals need to be checked for executing finally clauses or
1206 * catching an interrupt exception
1207 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 * looping for ":source" command or function call.
1210 */
1211 while (!((got_int
1212#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001213 || (did_emsg && (force_abort || in_vim9script()))
1214 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#endif
1216 )
1217#ifdef FEAT_EVAL
1218 && cstack.cs_trylevel == 0
1219#endif
1220 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001221 && !(did_emsg
1222#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001223 // Keep going when inside try/catch, so that the error can be
1224 // deal with, except when it is a syntax error, it may cause
1225 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001226 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1227#endif
1228 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001229 && (getline_equal(fgetline, cookie, getexmodeline)
1230 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 && (next_cmdline != NULL
1232#ifdef FEAT_EVAL
1233 || cstack.cs_idx >= 0
1234#endif
1235 || (flags & DOCMD_REPEAT)));
1236
1237 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001238 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#ifdef FEAT_EVAL
1240 free_cmdlines(&lines_ga);
1241 ga_clear(&lines_ga);
1242
1243 if (cstack.cs_idx >= 0)
1244 {
1245 /*
1246 * If a sourced file or executed function ran to its end, report the
1247 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001248 * In Vim9 script do not give a second error, executing aborts after
1249 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001251 if (!got_int && !did_throw && !aborting()
1252 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001253 && ((getline_equal(fgetline, cookie, getsourceline)
1254 && !source_finished(fgetline, cookie))
1255 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 && !func_has_ended(real_cookie))))
1257 {
1258 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001259 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001261 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001262 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001263 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001265 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267
1268 /*
1269 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1270 * ":endtry" in a sourced file or executed function. If the try
1271 * conditional is in its finally clause, ignore anything pending.
1272 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001273 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 */
1275 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001276 {
1277 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1278
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001279 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001280 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001281 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1282 &cstack.cs_looplevel);
1283 }
1284 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 trylevel = initial_trylevel;
1286 }
1287
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001288 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1289 // lack was reported above and the error message is to be converted to an
1290 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001291 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ? (char_u *)"endfunction" : (char_u *)NULL);
1293
1294 if (trylevel == 0)
1295 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001296 // Just in case did_throw got set but current_exception wasn't.
1297 if (current_exception == NULL)
1298 did_throw = FALSE;
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 /*
1301 * When an exception is being thrown out of the outermost try
1302 * conditional, discard the uncaught exception, disable the conversion
1303 * of interrupts or errors to exceptions, and ensure that no more
1304 * commands are executed.
1305 */
1306 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001307 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308
1309 /*
1310 * On an interrupt or an aborting error not converted to an exception,
1311 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001312 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * when force_abort is set and did_emsg unset in case of an interrupt
1314 * from a finally clause after an error.
1315 */
1316 else if (got_int || (did_emsg && force_abort))
1317 suppress_errthrow = TRUE;
1318 }
1319
1320 /*
1321 * The current cstack will be freed when do_cmdline() returns. An uncaught
1322 * exception will have to be rethrown in the previous cstack. If a function
1323 * has just returned or a script file was just finished and the previous
1324 * cstack belongs to the same function or, respectively, script file, it
1325 * will have to be checked for finally clauses to be executed due to the
1326 * ":return" or ":finish". This is done in do_one_cmd().
1327 */
1328 if (did_throw)
1329 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001330 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 && ex_nesting_level > func_level(real_cookie) + 1))
1334 {
1335 if (!did_throw)
1336 check_cstack = TRUE;
1337 }
1338 else
1339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001340 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001341 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 --ex_nesting_level;
1343 /*
1344 * Go to debug mode when returning from a function in which we are
1345 * single-stepping.
1346 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001347 if ((getline_equal(fgetline, cookie, getsourceline)
1348 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001350 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 ? (char_u *)_("End of sourced file")
1352 : (char_u *)_("End of function"));
1353 }
1354
1355 /*
1356 * Restore the exception environment (done after returning from the
1357 * debugger).
1358 */
1359 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001360 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
1362 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001363
1364 // Cleanup if "cs_emsg_silent_list" remains.
1365 if (cstack.cs_emsg_silent_list != NULL)
1366 {
1367 eslist_T *elem, *temp;
1368
1369 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1370 {
1371 temp = elem->next;
1372 vim_free(elem);
1373 }
1374 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001375#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 /*
1378 * If there was too much output to fit on the command line, ask the user to
1379 * hit return before redrawing the screen. With the ":global" command we do
1380 * this only once after the command is finished.
1381 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001382 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001384 if (RedrawingDisabled > 0)
1385 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 --no_wait_return;
1387 msg_scroll = FALSE;
1388
1389 /*
1390 * When just finished an ":if"-":else" which was typed, no need to
1391 * wait for hit-return. Also for an error situation.
1392 */
1393 if (retval == FAIL
1394#ifdef FEAT_EVAL
1395 || (did_endif && KeyTyped && !did_emsg)
1396#endif
1397 )
1398 {
1399 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001400 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402 else if (need_wait_return)
1403 {
1404 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001405 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * here should not overwrite the command that may be shown before
1407 * doing that.
1408 */
1409 msg_didout |= msg_didout_before_start;
1410 wait_return(FALSE);
1411 }
1412 }
1413
Bram Moolenaar2a942252012-11-28 23:03:07 +01001414#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001415 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001416#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 /*
1418 * Reset if_level, in case a sourced script file contains more ":if" than
1419 * ":endif" (could be ":if x | foo | endif").
1420 */
1421 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001422#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 --call_depth;
1425 return retval;
1426}
1427
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001428#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001429/*
1430 * Handle when "did_throw" is set after executing commands.
1431 */
1432 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001433handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001434{
1435 char *p = NULL;
1436 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001437 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001438
1439 /*
1440 * If the uncaught exception is a user exception, report it as an
1441 * error. If it is an error exception, display the saved error
1442 * message now. For an interrupt exception, do nothing; the
1443 * interrupt message is given elsewhere.
1444 */
1445 switch (current_exception->type)
1446 {
1447 case ET_USER:
1448 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001449 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001450 current_exception->value);
1451 p = (char *)vim_strsave(IObuff);
1452 break;
1453 case ET_ERROR:
1454 messages = current_exception->messages;
1455 current_exception->messages = NULL;
1456 break;
1457 case ET_INTERRUPT:
1458 break;
1459 }
1460
1461 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1462 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001463 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001464 current_exception->throw_name = NULL;
1465
1466 discard_current_exception(); // uses IObuff if 'verbose'
LemonBoy749ba0f2024-07-04 19:23:16 +02001467
1468 // If "silent!" is active the uncaught exception is not fatal.
1469 if (emsg_silent == 0)
1470 {
1471 suppress_errthrow = TRUE;
1472 force_abort = TRUE;
1473 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001474
1475 if (messages != NULL)
1476 {
1477 do
1478 {
1479 msglist_T *next = messages->next;
1480 int save_compiling = estack_compiling;
1481
1482 estack_compiling = messages->msg_compiling;
1483 emsg(messages->msg);
1484 vim_free(messages->msg);
1485 vim_free(messages->sfile);
1486 vim_free(messages);
1487 messages = next;
1488 estack_compiling = save_compiling;
1489 }
1490 while (messages != NULL);
1491 }
1492 else if (p != NULL)
1493 {
1494 emsg(p);
1495 vim_free(p);
1496 }
1497 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001498 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001499 estack_pop();
1500}
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001503 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 */
1505 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001506get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001508 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 wcmd_T *wp;
1510 char_u *line;
1511
1512 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1513 {
1514 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001515 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001517 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001518 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001519 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001521 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001522 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 ++cp->current_line;
1524
1525 return line;
1526 }
1527
1528 KeyTyped = FALSE;
1529 ++cp->current_line;
1530 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001531 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 return vim_strsave(wp->line);
1533}
1534
1535/*
1536 * Store a line in "gap" so that a ":while" loop can execute it again.
1537 */
1538 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001539store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
1541 if (ga_grow(gap, 1) == FAIL)
1542 return FAIL;
1543 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001544 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 return OK;
1547}
1548
1549/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001550 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
1552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001553free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 while (gap->ga_len > 0)
1556 {
1557 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1558 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 }
1560}
1561#endif
1562
1563/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001564 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1565 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001568getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001569 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001570 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001571 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001574 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001575 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001577 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1578 // function that's originally used to obtain the lines. This may be
1579 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001580 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001581 cp = (struct loop_cookie *)cookie;
1582 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001584 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 cp = cp->cookie;
1586 }
1587 return gp == func;
1588#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001589 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590#endif
1591}
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001594 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * getline function. Otherwise return "cookie".
1596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001598getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001599 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001600 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
Bram Moolenaar13505972019-01-24 15:04:48 +01001602#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001603 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001604 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1607 // cookie that's originally used to obtain the lines. This may be nested
1608 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001609 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001610 cp = (struct loop_cookie *)cookie;
1611 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001613 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 cp = cp->cookie;
1615 }
1616 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001617#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001622#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001623/*
1624 * Get the next line source line without advancing.
1625 */
1626 char_u *
1627getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001628 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001629 void *cookie) // argument for fgetline()
1630{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001631 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001632 struct loop_cookie *cp;
1633 wcmd_T *wp;
1634
1635 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1636 // cookie that's originally used to obtain the lines. This may be nested
1637 // several levels.
1638 gp = fgetline;
1639 cp = (struct loop_cookie *)cookie;
1640 while (gp == get_loop_line)
1641 {
1642 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1643 {
1644 // executing lines a second time, use the stored copy
1645 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1646 return wp->line;
1647 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001648 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001649 cp = cp->cookie;
1650 }
1651 if (gp == getsourceline)
1652 return source_nextline(cp);
1653 return NULL;
1654}
1655#endif
1656
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001657
1658/*
1659 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1660 * ":bwipeout", etc.
1661 * Returns the buffer number.
1662 */
1663 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001664compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001665{
1666 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001667 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001668 int count = offset;
1669
1670 buf = firstbuf;
1671 while (buf->b_next != NULL && buf->b_fnum < lnum)
1672 buf = buf->b_next;
1673 while (count != 0)
1674 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001675 count += (offset < 0) ? 1 : -1;
1676 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1677 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001678 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001679 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001680 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001681 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001682 while (buf->b_ml.ml_mfp == NULL)
1683 {
1684 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1685 if (nextbuf == NULL)
1686 break;
1687 buf = nextbuf;
1688 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001689 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001690 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001691 if (addr_type == ADDR_LOADED_BUFFERS)
1692 while (buf->b_ml.ml_mfp == NULL)
1693 {
1694 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1695 if (nextbuf == NULL)
1696 break;
1697 buf = nextbuf;
1698 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699 return buf->b_fnum;
1700}
1701
Bram Moolenaarb7316892019-05-01 18:08:42 +02001702/*
1703 * Return the window number of "win".
1704 * When "win" is NULL return the number of windows.
1705 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001706 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001707current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001708{
1709 win_T *wp;
1710 int nr = 0;
1711
Bram Moolenaar29323592016-07-24 22:04:11 +02001712 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001713 {
1714 ++nr;
1715 if (wp == win)
1716 break;
1717 }
1718 return nr;
1719}
1720
1721 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001722current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001723{
1724 tabpage_T *tp;
1725 int nr = 0;
1726
Bram Moolenaar29323592016-07-24 22:04:11 +02001727 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001728 {
1729 ++nr;
1730 if (tp == tab)
1731 break;
1732 }
1733 return nr;
1734}
1735
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001736 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001737comment_start(char_u *p, int starts_with_colon UNUSED)
1738{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001739 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001740 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001741 return *p == '"';
1742}
1743
Bram Moolenaarf240e182014-11-27 18:33:02 +01001744# define CURRENT_WIN_NR current_win_nr(curwin)
1745# define LAST_WIN_NR current_win_nr(NULL)
1746# define CURRENT_TAB_NR current_tab_nr(curtab)
1747# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749/*
1750 * Execute one Ex command.
1751 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001752 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1753 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 *
1755 * 1. skip comment lines and leading space
1756 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001757 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001759 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001760 * 6. parse arguments
1761 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001763 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 *
1765 * This function may be called recursively!
1766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001768do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001769 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001770 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001772 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001774 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001775 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001777 char_u *p;
1778 linenr_T lnum;
1779 long n;
1780 char *errormsg = NULL; // error message
1781 char_u *after_modifier = NULL;
1782 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001783 cmdmod_T save_cmdmod;
1784 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001785 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001786 int ni; // set when Not Implemented
1787 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001788 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001789 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001790#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001791 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001792#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001793 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001794 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaara80faa82020-04-12 19:37:17 +02001796 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 ea.line1 = 1;
1798 ea.line2 = 1;
1799#ifdef FEAT_EVAL
1800 ++ex_nesting_level;
1801#endif
1802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001803 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (quitmore
1805#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001806 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001807 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001808#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001809 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001810 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 --quitmore;
1812
1813 /*
1814 * Reset browse, confirm, etc.. They are restored when returning, for
1815 * recursive calls.
1816 */
1817 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001819 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001820 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1821 goto doend;
1822
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001823/*
1824 * 1. Skip comment lines and leading white space and colons.
1825 * 2. Handle command modifiers.
1826 */
1827 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001829 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001830 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001831 ea.cookie = cookie;
1832#ifdef FEAT_EVAL
1833 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001834 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001836 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001837 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001838 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001839 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841#ifdef FEAT_EVAL
1842 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1843 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1844#else
1845 ea.skip = (if_level > 0);
1846#endif
1847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001849 * 3. Skip over the range to find the command. Let "p" point to after it.
1850 *
1851 * We need the command to know what kind of range it uses.
1852 */
1853 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001854
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001855 // In Vim9 script a colon is required before the range. This may also be
1856 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001857 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001858 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001859 {
1860 may_have_range = FALSE;
1861 for (p = ea.cmd; p >= *cmdlinep; --p)
1862 {
1863 if (*p == ':')
1864 may_have_range = TRUE;
1865 if (p < ea.cmd && !VIM_ISWHITE(*p))
1866 break;
1867 }
1868 }
1869 else
1870 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001871 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001872 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001874#ifdef FEAT_EVAL
1875 // Handle ":export" - it functions almost like a command modifier.
1876 // ":export var Name: type"
1877 // ":export def Name(..."
1878 // etc.
1879 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1880 is_export = TRUE;
1881#endif
1882
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001883 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001884 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001885 if (ea.cmd == cmd + 1 && *cmd == '$')
1886 // should be "$VAR = val"
1887 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001888#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001889 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001890#else
1891 p = find_ex_command(&ea, NULL, NULL, NULL);
1892#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001893 if (ea.cmdidx == CMD_SIZE)
1894 {
1895 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1896
1897 // If a ':' before the range is missing, give a clearer error
1898 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001899 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001900 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001901 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001902 goto doend;
1903 }
1904 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001908
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001909#ifdef FEAT_EVAL
1910# ifdef FEAT_PROFILE
1911 // Count this line for profiling if skip is TRUE.
1912 if (do_profiling == PROF_YES
1913 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1914 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1915 {
1916 int skip = did_emsg || got_int || did_throw;
1917
1918 if (ea.cmdidx == CMD_catch)
1919 skip = !skip && !(cstack->cs_idx >= 0
1920 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1921 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1922 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1923 skip = skip || !(cstack->cs_idx >= 0
1924 && !(cstack->cs_flags[cstack->cs_idx]
1925 & (CSF_ACTIVE | CSF_TRUE)));
1926 else if (ea.cmdidx == CMD_finally)
1927 skip = FALSE;
1928 else if (ea.cmdidx != CMD_endif
1929 && ea.cmdidx != CMD_endfor
1930 && ea.cmdidx != CMD_endtry
1931 && ea.cmdidx != CMD_endwhile)
1932 skip = ea.skip;
1933
1934 if (!skip)
1935 {
1936 if (getline_equal(fgetline, cookie, get_func_line))
1937 func_line_exec(getline_cookie(fgetline, cookie));
1938 else if (getline_equal(fgetline, cookie, getsourceline))
1939 script_line_exec();
1940 }
1941 }
1942# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001943#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001944
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001945 ea.cmd = cmd;
1946
1947#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001948 // May go to debug mode. If this happens and the ">quit" debug command is
1949 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001950 dbg_check_breakpoint(&ea);
1951 if (!ea.skip && got_int)
1952 {
1953 ea.skip = TRUE;
1954 (void)do_intthrow(cstack);
1955 }
1956#endif
1957
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001958/*
1959 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 *
1961 * where 'addr' is:
1962 *
1963 * % (entire file)
1964 * $ [+-NUM]
1965 * 'x [+-NUM] (where x denotes a currently defined mark)
1966 * . [+-NUM]
1967 * [+-NUM]..
1968 * NUM
1969 *
1970 * The ea.cmd pointer is updated to point to the first character following the
1971 * range spec. If an initial address is found, but no second, the upper bound
1972 * is equal to the lower.
1973 */
1974
Bram Moolenaar25190db2019-05-04 15:05:28 +02001975 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001976 if (!IS_USER_CMDIDX(ea.cmdidx))
1977 {
1978 if (ea.cmdidx != CMD_SIZE)
1979 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1980 else
1981 ea.addr_type = ADDR_LINES;
1982
Bram Moolenaar25190db2019-05-04 15:05:28 +02001983 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001984 if (ea.cmdidx == CMD_wincmd && p != NULL)
1985 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001986#ifdef FEAT_QUICKFIX
1987 // :.cc in quickfix window uses line number
1988 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1989 ea.addr_type = ADDR_OTHER;
1990#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001991 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001992
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001993 if (!may_have_range)
1994 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001995 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1996 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001999 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 */
2001
2002 /*
2003 * Skip ':' and any white space
2004 */
2005 ea.cmd = skipwhite(ea.cmd);
2006 while (*ea.cmd == ':')
2007 ea.cmd = skipwhite(ea.cmd + 1);
2008
2009 /*
2010 * If we got a line, but no command, then go to the line.
2011 * If we find a '|' or '\n' we set ea.nextcmd.
2012 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002013 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2014 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
2016 /*
2017 * strange vi behaviour:
2018 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002019 * ":3|..." prints line 3 (not in Vim9 script)
2020 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002022 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002024 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 goto doend;
2026 }
2027
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002028 // If this looks like an undefined user command and there are CmdUndefined
2029 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002030 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2031 && ASCII_ISUPPER(*ea.cmd)
2032 && has_cmdundefined())
2033 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002034 int ret;
2035
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002036 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002037 while (ASCII_ISALNUM(*p))
2038 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002039 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002040 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2041 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002042 // If the autocommands did something and didn't cause an error, try
2043 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002044 p = (ret
2045#ifdef FEAT_EVAL
2046 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002047#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002049 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (p == NULL)
2052 {
2053 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002054 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 goto doend;
2056 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002057 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002058 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2059 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
2061 errormsg = uc_fun_cmd();
2062 goto doend;
2063 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 if (ea.cmdidx == CMD_SIZE)
2066 {
2067 if (!ea.skip)
2068 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002069 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // If the modifier was parsed OK the error must be in the
2073 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002074 if (after_modifier != NULL)
2075 append_command(after_modifier);
2076 else
2077 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002078 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002079 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002081 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 goto doend;
2084 }
2085
Bram Moolenaar958636c2014-10-21 20:01:58 +02002086 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2087 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002088#ifdef HAVE_EX_SCRIPT_NI
2089 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2090#endif
2091 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093#ifndef FEAT_EVAL
2094 /*
2095 * When the expression evaluation is disabled, recognize the ":if" and
2096 * ":endif" commands and ignore everything in between it.
2097 */
2098 if (ea.cmdidx == CMD_if)
2099 ++if_level;
2100 if (if_level)
2101 {
2102 if (ea.cmdidx == CMD_endif)
2103 --if_level;
2104 goto doend;
2105 }
2106
2107#endif
2108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002110 if (*p == '!' && ea.cmdidx != CMD_substitute
2111 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
2113 ++p;
2114 ea.forceit = TRUE;
2115 }
2116 else
2117 ea.forceit = FALSE;
2118
2119/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002120 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002122 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002123 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 if (!ea.skip)
2126 {
2127#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002128 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002130 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002131 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 goto doend;
2133 }
2134#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002135 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002136 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002137 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002138 goto doend;
2139 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002140 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002142 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002143 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 goto doend;
2145 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002146
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002147 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002149 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2150 {
2151 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002152 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002153 goto doend;
2154 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002155 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2156 {
2157 // Command not allowed when text is locked
2158 errormsg = _(get_text_locked_msg());
2159 goto doend;
2160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002163 // Disallow editing another buffer when "curbuf_lock" is set.
2164 // Do allow ":checktime" (it is postponed).
2165 // Do allow ":edit" (check for an argument later).
2166 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002167 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002168 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002169 && ea.cmdidx != CMD_edit
2170 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002171 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002172 && curbuf_locked())
2173 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002175 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002177 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 goto doend;
2179 }
2180 }
2181
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002182 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002184 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 goto doend;
2186 }
2187
2188 /*
2189 * Don't complain about the range if it is not used
2190 * (could happen if line_count is accidentally set to 0).
2191 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002192 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 /*
2195 * If the range is backwards, ask for confirmation and, if given, swap
2196 * ea.line1 & ea.line2 so it's forwards again.
2197 * When global command is busy, don't ask, will fail below.
2198 */
2199 if (!global_busy && ea.line1 > ea.line2)
2200 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002201 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002203 if (sourcing || exmode_active)
2204 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002205 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002206 goto doend;
2207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (ask_yesno((char_u *)
2209 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002210 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212 lnum = ea.line1;
2213 ea.line1 = ea.line2;
2214 ea.line2 = lnum;
2215 }
2216 if ((errormsg = invalid_range(&ea)) != NULL)
2217 goto doend;
2218 }
2219
Bram Moolenaarb7316892019-05-01 18:08:42 +02002220 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2221 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ea.line2 = 1;
2223
2224 correct_range(&ea);
2225
2226#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002227 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002228 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002230 // Put the first line at the start of a closed fold, put the last line
2231 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 (void)hasFolding(ea.line1, &ea.line1, NULL);
2233 (void)hasFolding(ea.line2, NULL, &ea.line2);
2234 }
2235#endif
2236
2237#ifdef FEAT_QUICKFIX
2238 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002239 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 * option here, so things like % get expanded.
2241 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002242 p = replace_makeprg(&ea, p, cmdlinep);
2243 if (p == NULL)
2244 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245#endif
2246
2247 /*
2248 * Skip to start of argument.
2249 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2250 */
2251 if (ea.cmdidx == CMD_bang)
2252 ea.arg = p;
2253 else
2254 ea.arg = skipwhite(p);
2255
Bram Moolenaar379fb762018-08-30 15:58:28 +02002256 // ":file" cannot be run with an argument when "curbuf_lock" is set
2257 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2258 goto doend;
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 /*
2261 * Check for "++opt=val" argument.
2262 * Must be first, allow ":w ++enc=utf8 !cmd"
2263 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2266 if (getargopt(&ea) == FAIL && !ni)
2267 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002268 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 goto doend;
2270 }
2271
2272 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002274 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002276 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002278 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 goto doend;
2280 }
2281 ea.arg = skipwhite(ea.arg + 1);
2282 ea.append = TRUE;
2283 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002284 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 ++ea.arg;
2287 ea.usefilter = TRUE;
2288 }
2289 }
2290
2291 if (ea.cmdidx == CMD_read)
2292 {
2293 if (ea.forceit)
2294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002295 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 ea.forceit = FALSE;
2297 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002298 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 ++ea.arg;
2301 ea.usefilter = TRUE;
2302 }
2303 }
2304
2305 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2306 {
2307 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002308 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 ++ea.arg;
2311 ++ea.amount;
2312 }
2313 ea.arg = skipwhite(ea.arg);
2314 }
2315
2316 /*
2317 * Check for "+command" argument, before checking for next command.
2318 * Don't do this for ":read !cmd" and ":write !cmd".
2319 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2322
2323 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002324 * For commands that do not use '|' inside their argument: Check for '|' to
2325 * separate commands and '"' or '#' to start comments.
2326 *
2327 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002328 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002329 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002332 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2333 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002334 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002335 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002336 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002337 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002338 || ea.cmdidx == CMD_global
2339 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002340 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002341#ifdef FEAT_EVAL
2342 || inside_block(&ea)
2343#endif
2344 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 for (p = ea.arg; *p; ++p)
2347 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002348 // Remove one backslash before a newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002350 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002351 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 ea.nextcmd = p + 1;
2354 *p = NUL;
2355 break;
2356 }
2357 }
2358 }
2359
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002361 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002364 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002366 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002367 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002368 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002370#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002371 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002372 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002374 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002375 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377#endif
64-bitmane08f10a2025-03-18 22:14:34 +01002378 if (valid_yank_reg(*ea.arg, (!IS_USER_CMDIDX(ea.cmdidx)
2379 && ea.cmdidx != CMD_put && ea.cmdidx != CMD_iput)))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002380 {
2381 ea.regname = *ea.arg++;
2382#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002383 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002384 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2385 {
kuuote08d7b1c2021-10-04 22:17:36 +01002386 if (!ea.skip)
2387 {
2388 set_expr_line(vim_strsave(ea.arg), &ea);
2389 did_set_expr_line = TRUE;
2390 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002391 ea.arg += STRLEN(ea.arg);
2392 }
2393#endif
2394 ea.arg = skipwhite(ea.arg);
2395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397
2398 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002399 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * count, it's a buffer name.
2401 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002402 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002403 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002404 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002406 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002408 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002410 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 goto doend;
2412 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002413 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 ea.line2 = n;
2416 if (ea.addr_count == 0)
2417 ea.addr_count = 1;
2418 }
2419 else
2420 {
2421 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002422 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002423 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002424 else
2425 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 ++ea.addr_count;
2427 /*
2428 * Be vi compatible: no error message for out of range.
2429 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002430 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 ea.line2 = curbuf->b_ml.ml_line_count;
2432 }
2433 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002434
2435 /*
2436 * Check for flags: 'l', 'p' and '#'.
2437 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002438 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002439 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002440 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2441 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002443 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002444 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 goto doend;
2446 }
2447
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002448 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002450 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 goto doend;
2452 }
2453
2454#ifdef FEAT_EVAL
2455 /*
2456 * Skip the command when it's not going to be executed.
2457 * The commands like :if, :endif, etc. always need to be executed.
2458 * Also make an exception for commands that handle a trailing command
2459 * themselves.
2460 */
2461 if (ea.skip)
2462 {
2463 switch (ea.cmdidx)
2464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002465 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_while:
2467 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002468 case CMD_for:
2469 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_if:
2471 case CMD_elseif:
2472 case CMD_else:
2473 case CMD_endif:
2474 case CMD_try:
2475 case CMD_catch:
2476 case CMD_finally:
2477 case CMD_endtry:
2478 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002479 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 break;
2481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002482 // Commands that handle '|' themselves. Check: A command should
2483 // either have the EX_TRLBAR flag, appear in this list or appear in
2484 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_aboveleft:
2486 case CMD_and:
2487 case CMD_belowright:
2488 case CMD_botright:
2489 case CMD_browse:
2490 case CMD_call:
2491 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002492 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 case CMD_delfunction:
2494 case CMD_djump:
2495 case CMD_dlist:
2496 case CMD_dsearch:
2497 case CMD_dsplit:
2498 case CMD_echo:
2499 case CMD_echoerr:
2500 case CMD_echomsg:
2501 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002502 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002504 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002505 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 case CMD_help:
2507 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002508 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_ijump:
2510 case CMD_ilist:
2511 case CMD_isearch:
2512 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002513 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 case CMD_keepjumps:
2515 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002516 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_leftabove:
2518 case CMD_let:
2519 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002520 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002521 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002523 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002524 case CMD_noautocmd:
2525 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 case CMD_perl:
2527 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002528 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002529 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002530 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 case CMD_return:
2532 case CMD_rightbelow:
2533 case CMD_ruby:
2534 case CMD_silent:
2535 case CMD_smagic:
2536 case CMD_snomagic:
2537 case CMD_substitute:
2538 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002539 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 case CMD_tcl:
2541 case CMD_throw:
2542 case CMD_tilde:
2543 case CMD_topleft:
2544 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002545 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002546 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 case CMD_verbose:
2548 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002549 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 break;
2551
2552 default: goto doend;
2553 }
2554 }
2555#endif
2556
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002557 if ((ea.argt & EX_XFILE)
2558 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2559 goto doend;
2560
2561#ifdef FEAT_EVAL
2562 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002564 emsg(_(e_invalid_command_after_export));
2565 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 /*
2570 * Accept buffer name. Cannot be used at the same time with a buffer
2571 * number. Don't do this for a user command.
2572 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002573 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002574 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 /*
2577 * :bdelete, :bwipeout and :bunload take several arguments, separated
2578 * by spaces: find next space (skipping over escaped characters).
2579 * The others take one argument: ignore trailing spaces.
2580 */
2581 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2582 || ea.cmdidx == CMD_bunload)
2583 p = skiptowhite_esc(ea.arg);
2584 else
2585 {
2586 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002587 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 --p;
2589 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002590 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002591 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002592 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 goto doend;
2594 ea.addr_count = 1;
2595 ea.arg = skipwhite(p);
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002598 // The :try command saves the emsg_silent flag, reset it here when
2599 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002600 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002601 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002602 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002603 if (emsg_silent < 0)
2604 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002605 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002606 }
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002609 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaar958636c2014-10-21 20:01:58 +02002612 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
2614 /*
2615 * Execute a user-defined command.
2616 */
2617 do_ucmd(&ea);
2618 }
2619 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 {
2621 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002622 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2625 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002626 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628
2629#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002630 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002631 // set something went wrong or the command was never executed.
2632 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002633 {
2634 if (errormsg == NULL)
2635 errormsg = _(e_export_with_invalid_argument);
2636 is_export = FALSE;
2637 }
2638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002640 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002641 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002642 && current_sctx.sc_sid > 0
2643 && ea.cmdidx != CMD_endif
2644 && (cstack->cs_idx < 0
2645 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002646 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 /*
2649 * If the command just executed called do_cmdline(), any throw or ":return"
2650 * or ":finish" encountered there must also check the cstack of the still
2651 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2652 * exception, or reanimate a returned function or finished script file and
2653 * return or finish it again.
2654 */
2655 if (need_rethrow)
2656 do_throw(cstack);
2657 else if (check_cstack)
2658 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002659 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002661 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 && current_func_returned())
2663 do_return(&ea, TRUE, FALSE, NULL);
2664 }
2665 need_rethrow = check_cstack = FALSE;
2666#endif
2667
2668doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002669 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002672 curwin->w_cursor.col = 0;
2673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2676 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002677 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002679 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
2681 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002682 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002684 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
2686 emsg(errormsg);
2687 }
2688#ifdef FEAT_EVAL
2689 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002690 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2691 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002692
2693 if (did_set_expr_line)
2694 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002695 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696#endif
2697
Bram Moolenaare1004402020-10-24 20:49:43 +02002698 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002700 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002701 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002703 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 ea.nextcmd = NULL;
2705
2706#ifdef FEAT_EVAL
2707 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002708 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#endif
2710
2711 return ea.nextcmd;
2712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002714static char ex_error_buf[MSG_BUF_LEN];
2715
2716/*
2717 * Return an error message with argument included.
2718 * Uses a static buffer, only the last error will be kept.
2719 * "msg" will be translated, caller should use N_().
2720 */
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02002721 char *
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002722ex_errmsg(char *msg, char_u *arg)
2723{
2724 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2725 return ex_error_buf;
2726}
2727
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002729 * The "+" string used in place of an empty command in Ex mode.
2730 * This string is used in pointer comparison.
2731 */
2732static char exmode_plus[] = "+";
2733
2734/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002735 * Handle a range without a command.
2736 * Returns an error message on failure.
2737 */
2738 char *
2739ex_range_without_command(exarg_T *eap)
2740{
2741 char *errormsg = NULL;
2742
zeertzjq7d664bf2024-07-14 10:22:54 +02002743 if ((*eap->cmd == '|' ||
2744 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002745#ifdef FEAT_EVAL
2746 && !in_vim9script()
2747#endif
2748 )
2749 {
2750 eap->cmdidx = CMD_print;
2751 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2752 if ((errormsg = invalid_range(eap)) == NULL)
2753 {
2754 correct_range(eap);
2755 ex_print(eap);
2756 }
2757 }
2758 else if (eap->addr_count != 0)
2759 {
2760 if (eap->line2 > curbuf->b_ml.ml_line_count)
2761 {
2762 // With '-' in 'cpoptions' a line number past the file is an
2763 // error, otherwise put it at the end of the file.
2764 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2765 eap->line2 = -1;
2766 else
2767 eap->line2 = curbuf->b_ml.ml_line_count;
2768 }
2769
2770 if (eap->line2 < 0)
2771 errormsg = _(e_invalid_range);
2772 else
2773 {
2774 if (eap->line2 == 0)
2775 curwin->w_cursor.lnum = 1;
2776 else
2777 curwin->w_cursor.lnum = eap->line2;
2778 beginline(BL_SOL | BL_FIX);
2779 }
2780 }
2781 return errormsg;
2782}
2783
2784/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002785 * Check for an Ex command with optional tail.
2786 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002787 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002788 */
2789 static int
2790checkforcmd_opt(
2791 char_u **pp, // start of command
2792 char *cmd, // name of command
2793 int len, // required length
2794 int noparen)
2795{
2796 int i;
2797
2798 for (i = 0; cmd[i] != NUL; ++i)
2799 if (((char_u *)cmd)[i] != (*pp)[i])
2800 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002801 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002802 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002803 {
2804 *pp = skipwhite(*pp + i);
2805 return TRUE;
2806 }
2807 return FALSE;
2808}
2809
2810/*
2811 * Check for an Ex command with optional tail.
2812 * If there is a match advance "pp" to the argument and return TRUE.
2813 */
2814 int
2815checkforcmd(
2816 char_u **pp, // start of command
2817 char *cmd, // name of command
2818 int len) // required length
2819{
2820 return checkforcmd_opt(pp, cmd, len, FALSE);
2821}
2822
2823/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002824 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002825 * If there is a match advance "pp" to the argument and return TRUE.
2826 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002827 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002828checkforcmd_noparen(
2829 char_u **pp, // start of command
2830 char *cmd, // name of command
2831 int len) // required length
2832{
2833 return checkforcmd_opt(pp, cmd, len, TRUE);
2834}
2835
2836/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 * Parse and skip over command modifiers:
2838 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002839 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002841 * When "skip_only" is TRUE the global variables are not changed, except for
2842 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002843 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2844 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002845 * Call apply_cmdmod() to get the side effects of the modifiers:
2846 * - Increment "sandbox" for ":sandbox"
2847 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002848 * - set msg_silent for ":silent"
2849 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 * Return FAIL when the command is not to be executed.
2851 * May set "errormsg" to an error message.
2852 */
2853 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002854parse_command_modifiers(
2855 exarg_T *eap,
2856 char **errormsg,
2857 cmdmod_T *cmod,
2858 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002859{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002860 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002861 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002862 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002863 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002864 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002865 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866
Bram Moolenaare1004402020-10-24 20:49:43 +02002867 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002868 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002869
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002870 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2871 {
2872 // The automatically inserted Visual area range is skipped, so that
2873 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002874 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2875 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002876 eap->cmd += 5;
2877 cmd_start = eap->cmd;
2878 has_visual_range = TRUE;
2879 }
2880
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002881 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 for (;;)
2883 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002884 char_u *p;
2885
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002887 {
2888 if (*eap->cmd == ':')
2889 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002890 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002891 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002892
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002893 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002895 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2896 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2898 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002899 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002900 if (!skip_only)
2901 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002902 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 }
2904
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002905 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002906 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002907 {
2908 // a comment ends at a NL
zeertzjqf7b86092024-09-05 17:26:30 +02002909 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2910 if (eap->nextcmd != NULL)
2911 ++eap->nextcmd;
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002912 if (vim9script)
2913 {
2914 if (has_cmdmod(cmod, FALSE))
2915 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002916#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002917 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2918 && eap->cmd[2] != '{')
2919 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002920#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002921 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002923 }
zeertzjqf7b86092024-09-05 17:26:30 +02002924 if (*eap->cmd == '\n')
zeertzjq2432b4a2024-09-03 22:58:30 +02002925 {
2926 eap->nextcmd = eap->cmd + 1;
2927 return FAIL;
2928 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002929 if (*eap->cmd == NUL)
2930 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002931 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002932 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002933 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002934 if (vim9script && has_cmdmod(cmod, FALSE))
2935 *errormsg = _(e_command_modifier_without_command);
2936 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002937 return FAIL;
2938 }
2939
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002940 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002941
2942 // In Vim9 script a variable can shadow a command modifier:
2943 // verbose = 123
2944 // verbose += 123
2945 // silent! verbose = func()
2946 // verbose.member = 2
2947 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002948 // But not:
2949 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002950 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002951 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002952 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002953
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002954 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002955 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002956 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002957 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2958 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002959 break;
2960 }
2961
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 switch (*p)
2963 {
John Marriotted908f72024-04-18 22:46:56 +02002964 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002965 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002966 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002967 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 continue;
2969
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002970 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002972 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 continue;
2974 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002975 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002976 {
2977#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002978 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002979#endif
2980 continue;
2981 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002982 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002983 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002984 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002985 continue;
2986
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002987 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002988 break;
2989#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002990 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002991#endif
2992 continue;
2993
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002994 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002995 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002996 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 continue;
2998 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002999 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003000 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003001 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 continue;
3003 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003004 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003005 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003006 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 continue;
3008 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003009 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003011 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003012 continue;
3013
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003014 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003015 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003016 char_u *reg_pat;
3017 char_u *nulp = NULL;
3018 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003019
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003020 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003021 || *p == NUL
3022 || (ends_excmd(*p)
3023#ifdef FEAT_EVAL
3024 // in ":filter #pat# cmd" # does not
3025 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003026 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003027#endif
3028 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003029 break;
3030 if (*p == '!')
3031 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003032 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003033 p = skipwhite(p + 1);
3034 if (*p == NUL || ends_excmd(*p))
3035 break;
3036 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003037#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003038 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003039 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003040 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003041#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003042 if (skip_only)
3043 p = skip_vimgrep_pat(p, NULL, NULL);
3044 else
3045 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003046 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3047 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003048 if (p == NULL || *p == NUL)
3049 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003050 if (!skip_only)
3051 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003052 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003053 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003054 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003055 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003056 // restore the character overwritten by NUL
3057 if (nulp != NULL)
3058 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003059 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003060 eap->cmd = p;
3061 continue;
3062 }
3063
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003064 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3065 {
3066 cmod->cmod_split |= WSP_HOR;
3067 continue;
3068 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003069 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003070 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003071 || *p == NUL || ends_excmd(*p))
3072 break;
3073 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003074 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003075 continue;
3076
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003077 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003078 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003079 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003080 continue;
3081 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003082 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3083 {
3084 if (ends_excmd2(p, eap->cmd))
3085 {
3086 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003087 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003088 return FAIL;
3089 }
3090 cmod->cmod_flags |= CMOD_LEGACY;
3091 continue;
3092 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003093
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003094 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003095 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003096 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003097 continue;
3098
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003099 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003100 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003101 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003102 continue;
3103 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003104 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003105 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003106 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003107 continue;
3108
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003109 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003110 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003111 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003112 continue;
3113
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003114 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003115 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003116 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003117 continue;
3118 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003119 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003120 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003121 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003122 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3123 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003124 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003125 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003126 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003127 }
3128 continue;
3129
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003130 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003131 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003132 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003133 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003134 long tabnr = get_address(eap, &eap->cmd,
3135 ADDR_TABS, eap->skip,
3136 skip_only, FALSE, 1);
3137 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003138 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003139 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003140 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003141 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3142 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003143 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003144 return FAIL;
3145 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003146 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003147 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003148 }
3149 eap->cmd = p;
3150 continue;
3151 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003152 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003153 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003154 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003155 continue;
3156
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003157 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003158 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003159 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003160 continue;
3161
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003162 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003163 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003164 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003165 continue;
3166 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003167 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003168 {
3169 if (ends_excmd2(p, eap->cmd))
3170 {
3171 *errormsg =
3172 _(e_vim9cmd_must_be_followed_by_command);
3173 return FAIL;
3174 }
3175 cmod->cmod_flags |= CMOD_VIM9CMD;
3176 continue;
3177 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003178 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003179 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003180 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003181 {
zeertzjqcd749632022-06-14 13:30:35 +01003182 // zero means not set, one is verbose == 0, etc.
3183 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003184 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003185 else
zeertzjqcd749632022-06-14 13:30:35 +01003186 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003187 eap->cmd = p;
3188 continue;
3189 }
3190 break;
3191 }
3192
Bram Moolenaar1501b632022-03-27 16:56:21 +01003193 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003194 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003195 if (eap->cmd > cmd_start)
3196 {
3197 // Move the '<,'> range to after the modifiers and insert a colon.
3198 // Since the modifiers have been parsed put the colon on top of the
3199 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3200 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003201 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003202 {
3203 size_t len = STRLEN(cmd_start);
3204
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003205 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003206 // "'<,'>mods" -> "mods *+
3207 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003208 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003209 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003210 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003211 }
3212 else
3213 {
3214 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3215 eap->cmd -= 5;
3216 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3217 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003218 }
3219 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003220 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003221 // Special case: change empty command to "+".
3222 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003223 eap->cmd = (char_u *)"'<,'>+";
3224 else
3225 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003226 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003227 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003228 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003229
Bram Moolenaareffed932018-08-14 13:38:17 +02003230 return OK;
3231}
3232
3233/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003234 * Return TRUE if "cmod" has anything set.
3235 */
3236 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003237has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003238{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003239 return (cmod->cmod_flags != 0 && (!ignore_silent
3240 || (cmod->cmod_flags
3241 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003242 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003243 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003244 || cmod->cmod_tab != 0
3245 || cmod->cmod_filter_regmatch.regprog != NULL;
3246}
3247
Bram Moolenaar8991be22022-02-14 21:51:46 +00003248#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003249/*
3250 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3251 */
3252 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003253cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003254{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003255 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003256 {
3257 emsg(_(e_misplaced_command_modifier));
3258 return TRUE;
3259 }
3260 return FALSE;
3261}
Dominique Pelle748b3082022-01-08 12:41:16 +00003262#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003263
3264/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003265 * Apply the command modifiers. Saves current state in "cmdmod", call
3266 * undo_cmdmod() later.
3267 */
3268 void
3269apply_cmdmod(cmdmod_T *cmod)
3270{
3271#ifdef HAVE_SANDBOX
3272 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3273 {
3274 ++sandbox;
3275 cmod->cmod_did_sandbox = TRUE;
3276 }
3277#endif
zeertzjqcd749632022-06-14 13:30:35 +01003278 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003279 {
3280 if (cmod->cmod_verbose_save == 0)
3281 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003282 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003283 }
3284
3285 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3286 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003287 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003288 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003289 cmod->cmod_save_msg_scroll = msg_scroll;
3290 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003291 if (cmod->cmod_flags & CMOD_SILENT)
3292 ++msg_silent;
3293 if (cmod->cmod_flags & CMOD_UNSILENT)
3294 msg_silent = 0;
3295
3296 if (cmod->cmod_flags & CMOD_ERRSILENT)
3297 {
3298 ++emsg_silent;
3299 ++cmod->cmod_did_esilent;
3300 }
3301
Bram Moolenaare1004402020-10-24 20:49:43 +02003302 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003303 {
3304 // Set 'eventignore' to "all".
3305 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003306 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003307 set_string_option_direct((char_u *)"ei", -1,
3308 (char_u *)"all", OPT_FREE, SID_NONE);
3309 }
3310}
3311
3312/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003313 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003314 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003315 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003316undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003317{
Bram Moolenaare1004402020-10-24 20:49:43 +02003318 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003319 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003320 p_verbose = cmod->cmod_verbose_save - 1;
3321 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003322 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003323
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003324#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003325 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003326 {
3327 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003328 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003329 }
3330#endif
3331
Bram Moolenaare1004402020-10-24 20:49:43 +02003332 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003334 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003335 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3336 OPT_FREE, SID_NONE);
3337 free_string_option(cmod->cmod_save_ei);
3338 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003339 }
3340
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003341 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003342
Bram Moolenaare1004402020-10-24 20:49:43 +02003343 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003344 {
3345 // messages could be enabled for a serious error, need to check if the
3346 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003347 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3348 msg_silent = cmod->cmod_save_msg_silent - 1;
3349 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003350 if (emsg_silent < 0)
3351 emsg_silent = 0;
3352 // Restore msg_scroll, it's set by file I/O commands, even when no
3353 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003354 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003355
3356 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3357 // somewhere in the line. Put it back in the first column.
3358 if (redirecting())
3359 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003360
Bram Moolenaare1004402020-10-24 20:49:43 +02003361 cmod->cmod_save_msg_silent = 0;
3362 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003363 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003364}
3365
3366/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003367 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003368 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003369 * Return FAIL and set "errormsg" or return OK.
3370 */
3371 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003372parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003373{
3374 int address_count = 1;
3375 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003376 int need_check_cursor = FALSE;
3377 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003378
3379 // Repeat for all ',' or ';' separated addresses.
3380 for (;;)
3381 {
3382 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003383 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003384 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003385 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003386 eap->addr_count == 0, address_count++);
3387 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003388 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003389 if (lnum == MAXLNUM)
3390 {
3391 if (*eap->cmd == '%') // '%' - all lines
3392 {
3393 ++eap->cmd;
3394 switch (eap->addr_type)
3395 {
3396 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003397 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003398 eap->line1 = 1;
3399 eap->line2 = curbuf->b_ml.ml_line_count;
3400 break;
3401 case ADDR_LOADED_BUFFERS:
3402 {
3403 buf_T *buf = firstbuf;
3404
3405 while (buf->b_next != NULL
3406 && buf->b_ml.ml_mfp == NULL)
3407 buf = buf->b_next;
3408 eap->line1 = buf->b_fnum;
3409 buf = lastbuf;
3410 while (buf->b_prev != NULL
3411 && buf->b_ml.ml_mfp == NULL)
3412 buf = buf->b_prev;
3413 eap->line2 = buf->b_fnum;
3414 break;
3415 }
3416 case ADDR_BUFFERS:
3417 eap->line1 = firstbuf->b_fnum;
3418 eap->line2 = lastbuf->b_fnum;
3419 break;
3420 case ADDR_WINDOWS:
3421 case ADDR_TABS:
3422 if (IS_USER_CMDIDX(eap->cmdidx))
3423 {
3424 eap->line1 = 1;
3425 eap->line2 = eap->addr_type == ADDR_WINDOWS
3426 ? LAST_WIN_NR : LAST_TAB_NR;
3427 }
3428 else
3429 {
3430 // there is no Vim command which uses '%' and
3431 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003432 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003433 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003434 }
3435 break;
3436 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003437 case ADDR_UNSIGNED:
3438 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003439 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003440 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003441 case ADDR_ARGUMENTS:
3442 if (ARGCOUNT == 0)
3443 eap->line1 = eap->line2 = 0;
3444 else
3445 {
3446 eap->line1 = 1;
3447 eap->line2 = ARGCOUNT;
3448 }
3449 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003450 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003451#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003452 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003453 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003454 if (eap->line2 == 0)
3455 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003456#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003457 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003458 case ADDR_NONE:
3459 // Will give an error later if a range is found.
3460 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003461 }
3462 ++eap->addr_count;
3463 }
3464 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3465 {
3466 pos_T *fp;
3467
3468 // '*' - visual area
3469 if (eap->addr_type != ADDR_LINES)
3470 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003471 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003472 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003473 }
3474
3475 ++eap->cmd;
3476 if (!eap->skip)
3477 {
3478 fp = getmark('<', FALSE);
3479 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003480 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003481 eap->line1 = fp->lnum;
3482 fp = getmark('>', FALSE);
3483 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003484 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003485 eap->line2 = fp->lnum;
3486 ++eap->addr_count;
3487 }
3488 }
3489 }
3490 else
3491 eap->line2 = lnum;
3492 eap->addr_count++;
3493
3494 if (*eap->cmd == ';')
3495 {
3496 if (!eap->skip)
3497 {
3498 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003499
Bram Moolenaar0e717042020-04-27 19:29:01 +02003500 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003501 // accept zero as address, so 0;/PATTERN/ works correctly
3502 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003503 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003504 if (eap->line2 > 0)
3505 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003506 else
3507 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003508 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003509 }
3510 }
3511 else if (*eap->cmd != ',')
3512 break;
3513 ++eap->cmd;
3514 }
3515
3516 // One address given: set start and end lines.
3517 if (eap->addr_count == 1)
3518 {
3519 eap->line1 = eap->line2;
3520 // ... but only implicit: really no address given
3521 if (lnum == MAXLNUM)
3522 eap->addr_count = 0;
3523 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003524 ret = OK;
3525
3526theend:
3527 if (need_check_cursor)
3528 check_cursor();
3529 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003530}
3531
3532/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003533 * Append "cmd" to the error message in IObuff.
3534 * Takes care of limiting the length and handling 0xa0, which would be
3535 * invisible otherwise.
3536 */
3537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003538append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003539{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003540 size_t len = STRLEN(IObuff);
3541 char_u *s = cmd;
3542 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003543
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003544 if (len > IOSIZE - 100)
3545 {
3546 // Not enough space, truncate and put in "...".
3547 d = IObuff + IOSIZE - 100;
3548 d -= mb_head_off(IObuff, d);
3549 STRCPY(d, "...");
3550 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003551 STRCAT(IObuff, ": ");
3552 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003553 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003554 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003555 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003556 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003557 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003558 STRCPY(d, "<a0>");
3559 d += 4;
3560 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003561 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3562 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003563 else
3564 MB_COPY_CHAR(s, d);
3565 }
3566 *d = NUL;
3567}
3568
Dominique Pelle748b3082022-01-08 12:41:16 +00003569#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003570/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003571 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3572 * the name. Otherwise just return "start".
3573 */
3574 char_u *
3575skip_option_env_lead(char_u *start)
3576{
3577 char_u *name = start;
3578
3579 if (*start == '&')
3580 {
3581 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3582 name += 3;
3583 else
3584 name += 1;
3585 }
3586 else if (*start == '$')
3587 name += 1;
3588 return name;
3589}
Dominique Pelle748b3082022-01-08 12:41:16 +00003590#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003591
3592/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003593 * Return TRUE and set "*idx" if "p" points to a one letter command.
3594 * If not in Vim9 script:
Doug Kearnsea842022024-09-29 17:17:41 +02003595 * - The 'k' command can directly be followed by any character
3596 * but :keepa[lt] is another command, as are :keepj[umps],
3597 * :kee[pmarks] and :keepp[atterns].
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003598 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3599 * but :sre[wind] is another command, as are :scr[iptnames],
3600 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3601 */
3602 static int
3603one_letter_cmd(char_u *p, cmdidx_T *idx)
3604{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003605 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003606 return FALSE;
Doug Kearnsea842022024-09-29 17:17:41 +02003607 if (p[0] == 'k'
3608 && (p[1] != 'e' || (p[1] == 'e' && p[2] != 'e')))
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003609 {
3610 *idx = CMD_k;
3611 return TRUE;
3612 }
3613 if (p[0] == 's'
3614 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3615 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3616 || p[1] == 'g'
3617 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3618 || p[1] == 'I'
3619 || (p[1] == 'r' && p[2] != 'e')))
3620 {
3621 *idx = CMD_substitute;
3622 return TRUE;
3623 }
3624 return FALSE;
3625}
3626
Dominique Pellee764d1b2023-03-12 21:20:59 +00003627#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003628/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003629 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3630 * call.
3631 */
3632 int
3633number_method(char_u *cmd)
3634{
3635 char_u *p = skipdigits(cmd);
3636
3637 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3638}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003639#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003640
3641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003644 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003645 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 *
3647 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3648 * assignment without "let". Sets eap->cmdidx to the command while returning
3649 * "eap->cmd".
3650 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 * Returns NULL for an ambiguous user command.
3652 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 char_u *
3654find_ex_command(
3655 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003656 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003657 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003658 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
3660 int len;
3661 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003662 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003663#ifndef FEAT_EVAL
3664 int vim9 = FALSE;
3665#else
3666 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 /*
3669 * Recognize a Vim9 script function/method call and assignment:
3670 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3671 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003672 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003673 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003675 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003677 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003678 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3679 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003681 int oplen;
3682 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003683 char_u *swp;
3684
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003685 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003686 || (eap->cmd[0] == '$'
3687 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003688 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003689 && (valid_yank_reg(eap->cmd[1], FALSE)
3690 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003691 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003692 if (*eap->cmd == '&')
3693 {
3694 p = eap->cmd + 1;
3695 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3696 p += 2;
3697 p = to_name_end(p, FALSE);
3698 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003699 else if (*eap->cmd == '$')
3700 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003701 else
3702 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003703 if (ends_excmd(*skipwhite(p)))
3704 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003705 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3706 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003707 eap->cmdidx = CMD_eval;
3708 return eap->cmd;
3709 }
3710 // "&option" can be followed by "->" or "=", check below
3711 }
3712
3713 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003715 if (
3716 // "(..." is an expression.
3717 // "funcname(" is always a function call.
3718 *p == '('
3719 || (p == eap->cmd
3720 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003721 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003722 *eap->cmd == '{'
3723 // "'string'->func()" is an expression.
3724 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003725 // '"string"->func()' is an expression.
3726 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003727 // '$"string"->func()' is an expression.
3728 // "$'string'->func()" is an expression.
3729 || (eap->cmd[0] == '$'
3730 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3731 // '0z1234->func()' is an expression.
3732 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003733 // "g:varname" is an expression.
3734 || eap->cmd[1] == ':'
3735 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003736 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003737 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003738 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003739 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3740 {
3741 // "{" by itself is the start of a block.
3742 eap->cmdidx = CMD_block;
3743 return eap->cmd + 1;
3744 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003745 eap->cmdidx = CMD_eval;
3746 return eap->cmd;
3747 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003748
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003749 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003750 // "varname[]" is an expression.
3751 *p == '['
3752 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003753 || (*p == '.'
3754 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3755 // g:[key] is an expression
3756 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003757 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003758 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003759
3760 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003761 // Skip over the whole thing, it can be:
3762 // name.member = val
3763 // name[a : b] = val
3764 // name[idx] = val
3765 // name[idx].member = val
3766 // etc.
3767 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003768 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003769 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003770 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003771 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003772 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003773 || (after[0] == '.' && after[1] == '.'
3774 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003775 eap->cmdidx = CMD_var;
3776 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003777 --emsg_silent;
3778 return eap->cmd;
3779 }
3780
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003781 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3782 // an assignment.
3783 // If there is no line break inside the "[...]" then "p" is
3784 // advanced to after the "]" by to_name_const_end(): check if a "="
3785 // follows.
3786 // If "[...]" has a line break "p" still points at the "[" and it
3787 // can't be an assignment.
3788 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003789 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003790 char_u *eq;
3791
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003792 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003793 if (p == eap->cmd && *p == '[')
3794 {
3795 int count = 0;
3796 int semicolon = FALSE;
3797
3798 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3799 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003800 eq = p;
3801 if (eq != NULL)
3802 {
3803 eq = skipwhite(eq);
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003804 if (vim_strchr((char_u *)"+-*/%.", *eq) != NULL)
3805 {
3806 if (eq[0] == '.' && eq[1] == '.')
3807 ++eq;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003808 ++eq;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003809 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003810 }
3811 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003812 {
3813 eap->cmdidx = CMD_eval;
3814 return eap->cmd;
3815 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003816 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003817 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003818 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003819 return eap->cmd;
3820 }
3821 }
3822
3823 // Recognize an assignment if we recognize the variable name:
3824 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003825 // "@r = expr"
3826 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003827 // "var = expr" where "var" is a variable name or we are skipping
3828 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003829 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003830 oplen = assignment_len(skipwhite(p), &heredoc);
3831 if (oplen > 0)
3832 {
3833 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3834 || *eap->cmd == '&'
3835 || *eap->cmd == '$'
3836 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003837 || (eap->skip && IS_WHITE_OR_NUL(
3838 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003839 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003840 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003841 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003842 return eap->cmd;
3843 }
3844 }
3845
Bram Moolenaar9510d222022-09-11 15:14:05 +01003846 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003847 // "w:varname: number = 123".
3848 if (eap->cmd[1] == ':' && *p == ':')
3849 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003850 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003851 return eap->cmd;
3852 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003853 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003854
Bram Moolenaar31d99482022-05-26 22:24:43 +01003855 // 1234->func() is a method call
3856 if (number_method(eap->cmd))
3857 {
3858 eap->cmdidx = CMD_eval;
3859 return eap->cmd;
3860 }
3861
Bram Moolenaar7b829262021-10-13 15:04:34 +01003862 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3863 // an expression. A global/substitute/list command needs to use a
3864 // longer name.
3865 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3866 {
3867 eap->cmdidx = CMD_eval;
3868 return eap->cmd;
3869 }
3870
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003871 // If it is an ID it might be a variable with an operator on the next
3872 // line, if the variable exists it can't be an Ex command.
3873 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003874 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003875 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3876 {
3877 eap->cmdidx = CMD_eval;
3878 return eap->cmd;
3879 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003880
3881 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003882 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3883 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003884 {
3885 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3886 return eap->cmd + 2;
3887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 }
3889#endif
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 /*
3892 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 */
3894 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003895 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 ++p;
Doug Kearnsea842022024-09-29 17:17:41 +02003898 if (full != NULL)
3899 *full = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901 else
3902 {
3903 while (ASCII_ISALPHA(*p))
3904 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003905 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003906 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003908 while (ASCII_ISALNUM(*p))
3909 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 }
3911 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3912 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003913 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 ++p;
3915 while (ASCII_ISALPHA(*p))
3916 ++p;
3917 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003918
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003919 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003920 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 ++p;
3922 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003923 // The "d" command can directly be followed by 'l' or 'p' flag, when
3924 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003925 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003927 // Check for ":dl", ":dell", etc. to ":deletel": that's
3928 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003929 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003930 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003931 break;
3932 if (i == len - 1)
3933 {
3934 --len;
3935 if (p[-1] == 'l')
3936 eap->flags |= EXFLAG_LIST;
3937 else
3938 eap->flags |= EXFLAG_PRINT;
3939 }
3940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003942 if (ASCII_ISLOWER(eap->cmd[0]))
3943 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003944 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003945 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003946
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003947 if (command_count != (int)CMD_SIZE)
3948 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003949 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003950 getout(1);
3951 }
3952
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003953 // Use a precomputed index for fast look-up in cmdnames[]
3954 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003955 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3956 if (ASCII_ISLOWER(c2))
3957 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3958 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003959 else if (ASCII_ISUPPER(eap->cmd[0]))
3960 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003962 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3965 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3966 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3967 (size_t)len) == 0)
3968 {
3969#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003970 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 *full = TRUE;
3972#endif
3973 break;
3974 }
3975
Bram Moolenaar204852a2022-03-05 12:56:44 +00003976 // :Print and :mode are not supported in Vim9 script.
3977 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003978 if (vim9 && eap->cmdidx != CMD_SIZE)
3979 {
3980 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3981 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003982 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003983 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003984 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003985 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003986 eap->cmdidx = CMD_SIZE;
3987 }
3988 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003989
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003990 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003991 // 'cpoptions'.
3992 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3993 p = eap->cmd;
3994
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003995 // Look for a user defined command as a last resort. Let ":Print" be
3996 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003997 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3998 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004000 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 while (ASCII_ISALNUM(*p))
4002 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004003 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01004005 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 eap->cmdidx = CMD_SIZE;
4007 }
4008
Bram Moolenaar204852a2022-03-05 12:56:44 +00004009 // ":fina" means ":finally" in legacy script, for backwards compatibility.
4010 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02004011 eap->cmdidx = CMD_finally;
4012
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004013#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004014 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004015 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004016 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004017 && (eap->cmdidx < 0 ||
4018 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004019 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004020 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4021
4022 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004023 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004024 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004025 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004026#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return p;
4029}
4030
4031#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004032typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004033{
4034 char *name;
4035 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004036 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004037} cmdmod_info_T;
4038
4039static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004040 {"aboveleft", 3, FALSE},
4041 {"belowright", 3, FALSE},
4042 {"botright", 2, FALSE},
4043 {"browse", 3, FALSE},
4044 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004045 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004046 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004047 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004048 {"keepalt", 5, FALSE},
4049 {"keepjumps", 5, FALSE},
4050 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004051 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004052 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004053 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004054 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004055 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004056 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004057 {"rightbelow", 6, FALSE},
4058 {"sandbox", 3, FALSE},
4059 {"silent", 3, FALSE},
4060 {"tab", 3, TRUE},
4061 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004062 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004063 {"verbose", 4, TRUE},
4064 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004065 {"vim9cmd", 4, FALSE}
4066}; // cmdmod_info_tab
4067
Bram Moolenaared53fb92007-11-24 20:50:24 +00004068/*
4069 * Return length of a command modifier (including optional count).
4070 * Return zero when it's not a modifier.
4071 */
4072 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004073modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004074{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004075 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004076 char_u *p = cmd;
4077
4078 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004079 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004080 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004081 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004082 for (j = 0; p[j] != NUL; ++j)
4083 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004084 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004085 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4086 && (p == cmd || cmdmod_info_tab[i].has_count))
4087 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004088 }
4089 return 0;
4090}
4091
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092/*
4093 * Return > 0 if an Ex command "name" exists.
4094 * Return 2 if there is an exact match.
4095 * Return 3 if there is an ambiguous match.
4096 */
4097 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004098cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 exarg_T ea;
4101 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004102 int i;
4103 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004104 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105
Christian Brabandt70a11a62024-07-26 19:13:55 +02004106 // Check command modifiers.
4107 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004109 for (j = 0; name[j] != NUL; ++j)
4110 if (name[j] != cmdmod_info_tab[i].name[j])
4111 break;
4112 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4113 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004116 // Check built-in commands and user defined commands.
4117 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004118 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004120 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004122 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004124 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4125 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004126 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004127 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4129}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004130
4131/*
4132 * "fullcommand" function
4133 */
4134 void
4135f_fullcommand(typval_T *argvars, typval_T *rettv)
4136{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004137 exarg_T ea;
4138 char_u *name;
4139 char_u *p;
4140 int vim9script = in_vim9script();
4141 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004142
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004143 rettv->v_type = VAR_STRING;
4144 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004145
Bram Moolenaaraa534142022-09-15 21:46:02 +01004146 if (in_vim9script()
4147 && (check_for_string_arg(argvars, 0) == FAIL
4148 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004149 return;
4150
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004151 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004152 if (name == NULL)
4153 return;
4154
Bram Moolenaaraa534142022-09-15 21:46:02 +01004155 if (argvars[1].v_type != VAR_UNKNOWN)
4156 {
4157 vim9script = tv_get_bool(&argvars[1]);
4158 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4159 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4160 }
4161
zeertzjqc024ed92022-01-04 16:22:52 +00004162 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004163 name++;
4164 name = skip_range(name, TRUE, NULL);
4165
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004166 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4167 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004168 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004169 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004170 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004171 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004172 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004173 goto theend;
4174
4175 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004176 {
4177 int res;
4178
4179 ++emsg_silent;
4180 res = not_in_vim9(&ea);
4181 --emsg_silent;
4182
4183 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004184 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004185 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004186
4187 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004188 ? get_user_command_name(ea.useridx, ea.cmdidx)
4189 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004190theend:
4191 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#endif
4194
Bram Moolenaard0190392019-08-23 21:17:35 +02004195 cmdidx_T
4196excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
Bram Moolenaard0190392019-08-23 21:17:35 +02004198 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004200 if (!one_letter_cmd(cmd, &idx))
4201 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4202 idx = (cmdidx_T)((int)idx + 1))
4203 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4204 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
Bram Moolenaard0190392019-08-23 21:17:35 +02004206 return idx;
4207}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
Bram Moolenaard0190392019-08-23 21:17:35 +02004209 long
4210excmd_get_argt(cmdidx_T idx)
4211{
4212 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213}
4214
4215/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004216 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 *
4218 * Backslashed delimiters after / or ? will be skipped, and commands will
4219 * not be expanded between /'s and ?'s or after "'".
4220 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004221 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 * Returns the "cmd" pointer advanced to beyond the range.
4223 */
4224 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004225skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004226 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004227 int skip_star, // skip "*" used for Visual range
4228 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004230 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004231 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004233 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004235 if (*cmd == '\\')
4236 {
4237 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4238 ++cmd;
4239 else
4240 break;
4241 }
4242 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004244 char_u *p = cmd;
4245
4246 // a quote is only valid at the start or after a separator
4247 while (p > cmd_start)
4248 {
4249 --p;
4250 if (!VIM_ISWHITE(*p))
4251 break;
4252 }
4253 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4254 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (*++cmd == NUL && ctx != NULL)
4256 *ctx = EXPAND_NOTHING;
4257 }
4258 else if (*cmd == '/' || *cmd == '?')
4259 {
4260 delim = *cmd++;
4261 while (*cmd != NUL && *cmd != delim)
4262 if (*cmd++ == '\\' && *cmd != NUL)
4263 ++cmd;
4264 if (*cmd == NUL && ctx != NULL)
4265 *ctx = EXPAND_NOTHING;
4266 }
4267 if (*cmd != NUL)
4268 ++cmd;
4269 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004270
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004271 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004272 while (*cmd == ':')
4273 cmd = skipwhite(cmd + 1);
4274
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004275 // Skip "*" used for Visual range.
4276 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4277 cmd = skipwhite(cmd + 1);
4278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 return cmd;
4280}
4281
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004282 static void
4283addr_error(cmd_addr_T addr_type)
4284{
4285 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004286 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004287 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004288 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004289}
4290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004292 * Return the default address for an address type.
4293 */
4294 static linenr_T
4295default_address(exarg_T *eap)
4296{
4297 linenr_T lnum = 0;
4298
4299 switch (eap->addr_type)
4300 {
4301 case ADDR_LINES:
4302 case ADDR_OTHER:
4303 // Default is the cursor line number. Avoid using an invalid
4304 // line number though.
4305 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4306 lnum = curbuf->b_ml.ml_line_count;
4307 else
4308 lnum = curwin->w_cursor.lnum;
4309 break;
4310 case ADDR_WINDOWS:
4311 lnum = CURRENT_WIN_NR;
4312 break;
4313 case ADDR_ARGUMENTS:
4314 lnum = curwin->w_arg_idx + 1;
4315 if (lnum > ARGCOUNT)
4316 lnum = ARGCOUNT;
4317 break;
4318 case ADDR_LOADED_BUFFERS:
4319 case ADDR_BUFFERS:
4320 lnum = curbuf->b_fnum;
4321 break;
4322 case ADDR_TABS:
4323 lnum = CURRENT_TAB_NR;
4324 break;
4325 case ADDR_TABS_RELATIVE:
4326 case ADDR_UNSIGNED:
4327 lnum = 1;
4328 break;
4329 case ADDR_QUICKFIX:
4330#ifdef FEAT_QUICKFIX
4331 lnum = qf_get_cur_idx(eap);
4332#endif
4333 break;
4334 case ADDR_QUICKFIX_VALID:
4335#ifdef FEAT_QUICKFIX
4336 lnum = qf_get_cur_valid_idx(eap);
4337#endif
4338 break;
4339 case ADDR_NONE:
4340 // Will give an error later if a range is found.
4341 break;
4342 }
4343 return lnum;
4344}
4345
4346/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004347 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 *
4349 * Set ptr to the next character after the part that was interpreted.
4350 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004351 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 *
4353 * Return MAXLNUM when no Ex address was found.
4354 */
4355 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004356get_address(
4357 exarg_T *eap UNUSED,
4358 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004359 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004360 int skip, // only skip the address, don't use it
4361 int silent, // no errors or side effects
4362 int to_other_file, // flag: may jump to other file
4363 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
4365 int c;
4366 int i;
4367 long n;
4368 char_u *cmd;
4369 pos_T pos;
4370 pos_T *fp;
4371 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004372 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 cmd = skipwhite(*ptr);
4375 lnum = MAXLNUM;
4376 do
4377 {
4378 switch (*cmd)
4379 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004380 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004381 ++cmd;
4382 switch (addr_type)
4383 {
4384 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004385 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 lnum = curwin->w_cursor.lnum;
4387 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004389 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004390 break;
4391 case ADDR_ARGUMENTS:
4392 lnum = curwin->w_arg_idx + 1;
4393 break;
4394 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004395 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 lnum = curbuf->b_fnum;
4397 break;
4398 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004399 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004401 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004402 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004403 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004404 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004405 cmd = NULL;
4406 goto error;
4407 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004408 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004409#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004410 lnum = qf_get_cur_idx(eap);
4411#endif
4412 break;
4413 case ADDR_QUICKFIX_VALID:
4414#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004415 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004416#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004417 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004418 }
4419 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 ++cmd;
4423 switch (addr_type)
4424 {
4425 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004426 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 lnum = curbuf->b_ml.ml_line_count;
4428 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004430 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004431 break;
4432 case ADDR_ARGUMENTS:
4433 lnum = ARGCOUNT;
4434 break;
4435 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004436 buf = lastbuf;
4437 while (buf->b_ml.ml_mfp == NULL)
4438 {
4439 if (buf->b_prev == NULL)
4440 break;
4441 buf = buf->b_prev;
4442 }
4443 lnum = buf->b_fnum;
4444 break;
4445 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 lnum = lastbuf->b_fnum;
4447 break;
4448 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004449 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004451 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004452 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004453 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004454 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004455 cmd = NULL;
4456 goto error;
4457 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004458 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004459#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004460 lnum = qf_get_size(eap);
4461 if (lnum == 0)
4462 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004463#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004464 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004465 case ADDR_QUICKFIX_VALID:
4466#ifdef FEAT_QUICKFIX
4467 lnum = qf_get_valid_size(eap);
4468 if (lnum == 0)
4469 lnum = 1;
4470#endif
4471 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 }
4473 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004475 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 if (*++cmd == NUL)
4477 {
4478 cmd = NULL;
4479 goto error;
4480 }
4481 if (addr_type != ADDR_LINES)
4482 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004483 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004484 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 goto error;
4486 }
4487 if (skip)
4488 ++cmd;
4489 else
4490 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 // Only accept a mark in another file when it is
4492 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4494 ++cmd;
4495 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004497 lnum = curwin->w_cursor.lnum;
4498 else
4499 {
4500 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
4502 cmd = NULL;
4503 goto error;
4504 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 lnum = fp->lnum;
4506 }
4507 }
4508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004511 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004512 c = *cmd++;
4513 if (addr_type != ADDR_LINES)
4514 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004515 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004516 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 goto error;
4518 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004519 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004521 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004522 if (*cmd == c)
4523 ++cmd;
4524 }
4525 else
4526 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004527 int flags;
4528
4529 pos = curwin->w_cursor; // save curwin->w_cursor
4530
4531 // When '/' or '?' follows another address, start from
4532 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004533 if (lnum > 0 && lnum != MAXLNUM)
4534 curwin->w_cursor.lnum =
4535 lnum > curbuf->b_ml.ml_line_count
4536 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004537
4538 // Start a forward search at the end of the line (unless
4539 // before the first line).
4540 // Start a backward search at the start of the line.
4541 // This makes sure we never match in the current
4542 // line, and can match anywhere in the
4543 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004544 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 curwin->w_cursor.col = MAXCOL;
4546 else
4547 curwin->w_cursor.col = 0;
4548 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004549 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004550 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004551 {
4552 curwin->w_cursor = pos;
4553 cmd = NULL;
4554 goto error;
4555 }
4556 lnum = curwin->w_cursor.lnum;
4557 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004558 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004559 cmd += searchcmdlen;
4560 }
4561 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004563 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004564 ++cmd;
4565 if (addr_type != ADDR_LINES)
4566 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004567 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004568 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004569 goto error;
4570 }
4571 if (*cmd == '&')
4572 i = RE_SUBST;
4573 else if (*cmd == '?' || *cmd == '/')
4574 i = RE_SEARCH;
4575 else
4576 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004577 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004578 cmd = NULL;
4579 goto error;
4580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004582 if (!skip)
4583 {
4584 /*
4585 * When search follows another address, start from
4586 * there.
4587 */
4588 if (lnum != MAXLNUM)
4589 pos.lnum = lnum;
4590 else
4591 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004593 /*
4594 * Start the search just like for the above
4595 * do_search().
4596 */
4597 if (*cmd != '?')
4598 pos.col = MAXCOL;
4599 else
4600 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004601 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004602 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004603 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004604 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004605 lnum = pos.lnum;
4606 else
4607 {
4608 cmd = NULL;
4609 goto error;
4610 }
4611 }
4612 ++cmd;
4613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004616 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004617 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619
4620 for (;;)
4621 {
4622 cmd = skipwhite(cmd);
4623 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4624 break;
4625
4626 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004627 {
4628 switch (addr_type)
4629 {
4630 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004631 case ADDR_OTHER:
4632 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004633 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004634 break;
4635 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004636 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 break;
4638 case ADDR_ARGUMENTS:
4639 lnum = curwin->w_arg_idx + 1;
4640 break;
4641 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004642 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004643 lnum = curbuf->b_fnum;
4644 break;
4645 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004646 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004647 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004648 case ADDR_TABS_RELATIVE:
4649 lnum = 1;
4650 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004651 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004652#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004653 lnum = qf_get_cur_idx(eap);
4654#endif
4655 break;
4656 case ADDR_QUICKFIX_VALID:
4657#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004658 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004659#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004660 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004661 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004662 case ADDR_UNSIGNED:
4663 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004664 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004665 }
4666 }
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004669 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 else
4671 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004672 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 n = 1;
4674 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004675 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004676 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004678 if (n == MAXLNUM)
4679 {
4680 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004681 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004682 goto error;
4683 }
4684 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004685
4686 if (addr_type == ADDR_TABS_RELATIVE)
4687 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004688 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004689 cmd = NULL;
4690 goto error;
4691 }
4692 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004693 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004694 lnum = compute_buffer_local_count(
4695 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004697 {
4698#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004699 // Relative line addressing: need to adjust for lines in a
4700 // closed fold after the first address.
4701 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4702 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004703 (void)hasFolding(lnum, NULL, &lnum);
4704#endif
4705 if (i == '-')
4706 lnum -= n;
4707 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004708 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004709 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004710 {
4711 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004712 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004713 goto error;
4714 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004715 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004716 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 }
4719 } while (*cmd == '/' || *cmd == '?');
4720
4721error:
4722 *ptr = cmd;
4723 return lnum;
4724}
4725
4726/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004727 * Set eap->line1 and eap->line2 to the whole range.
4728 * Used for commands with the EX_DFLALL flag and no range given.
4729 */
4730 static void
4731address_default_all(exarg_T *eap)
4732{
4733 eap->line1 = 1;
4734 switch (eap->addr_type)
4735 {
4736 case ADDR_LINES:
4737 case ADDR_OTHER:
4738 eap->line2 = curbuf->b_ml.ml_line_count;
4739 break;
4740 case ADDR_LOADED_BUFFERS:
4741 {
4742 buf_T *buf = firstbuf;
4743
4744 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4745 buf = buf->b_next;
4746 eap->line1 = buf->b_fnum;
4747 buf = lastbuf;
4748 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4749 buf = buf->b_prev;
4750 eap->line2 = buf->b_fnum;
4751 }
4752 break;
4753 case ADDR_BUFFERS:
4754 eap->line1 = firstbuf->b_fnum;
4755 eap->line2 = lastbuf->b_fnum;
4756 break;
4757 case ADDR_WINDOWS:
4758 eap->line2 = LAST_WIN_NR;
4759 break;
4760 case ADDR_TABS:
4761 eap->line2 = LAST_TAB_NR;
4762 break;
4763 case ADDR_TABS_RELATIVE:
4764 eap->line2 = 1;
4765 break;
4766 case ADDR_ARGUMENTS:
4767 if (ARGCOUNT == 0)
4768 eap->line1 = eap->line2 = 0;
4769 else
4770 eap->line2 = ARGCOUNT;
4771 break;
4772 case ADDR_QUICKFIX_VALID:
4773#ifdef FEAT_QUICKFIX
4774 eap->line2 = qf_get_valid_size(eap);
4775 if (eap->line2 == 0)
4776 eap->line2 = 1;
4777#endif
4778 break;
4779 case ADDR_NONE:
4780 case ADDR_UNSIGNED:
4781 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004782 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004783 break;
4784 }
4785}
4786
4787
4788/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004789 * Get flags from an Ex command argument.
4790 */
4791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004792get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004793{
4794 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4795 {
4796 if (*eap->arg == 'l')
4797 eap->flags |= EXFLAG_LIST;
4798 else if (*eap->arg == 'p')
4799 eap->flags |= EXFLAG_PRINT;
4800 else
4801 eap->flags |= EXFLAG_NR;
4802 eap->arg = skipwhite(eap->arg + 1);
4803 }
4804}
4805
4806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 * Function called for command which is Not Implemented. NI!
4808 */
4809 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004810ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004813 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004814 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815}
4816
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004817#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818/*
4819 * Function called for script command which is Not Implemented. NI!
4820 * Skips over ":perl <<EOF" constructs.
4821 */
4822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004823ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824{
4825 if (!eap->skip)
4826 ex_ni(eap);
4827 else
4828 vim_free(script_get(eap, eap->arg));
4829}
4830#endif
4831
4832/*
4833 * Check range in Ex command for validity.
4834 * Return NULL when valid, error message when invalid.
4835 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004836 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004837invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004839 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 if ( eap->line1 < 0
4842 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004844 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004845
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004846 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004847 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004848 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004849 {
4850 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004851 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004852#ifdef FEAT_DIFF
Yee Cheng Chind75ab0c2025-06-20 18:44:18 +02004853 + (eap->cmdidx == CMD_diffget ||
4854 eap->cmdidx == CMD_diffput)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004855#endif
4856 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004857 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004858 break;
4859 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004860 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004861 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004862 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004863 break;
4864 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004865 // Only a boundary check, not whether the buffers actually
4866 // exist.
4867 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004868 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004869 break;
4870 case ADDR_LOADED_BUFFERS:
4871 buf = firstbuf;
4872 while (buf->b_ml.ml_mfp == NULL)
4873 {
4874 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004875 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004876 buf = buf->b_next;
4877 }
4878 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004879 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004880 buf = lastbuf;
4881 while (buf->b_ml.ml_mfp == NULL)
4882 {
4883 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004884 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004885 buf = buf->b_prev;
4886 }
4887 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004888 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004889 break;
4890 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004891 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004892 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004893 break;
4894 case ADDR_TABS:
4895 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004896 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004897 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004898 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004899 case ADDR_OTHER:
4900 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004901 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004902 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004903#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004904 // No error for value that is too big, will use the last entry.
4905 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004906 {
4907 if (eap->addr_count == 0)
4908 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004909 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004910 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004911#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004912 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004913 case ADDR_QUICKFIX_VALID:
4914#ifdef FEAT_QUICKFIX
4915 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4916 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004917 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004918#endif
4919 break;
4920 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004921 case ADDR_NONE:
4922 // Will give an error elsewhere.
4923 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004924 }
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return NULL;
4927}
4928
4929/*
4930 * Correct the range for zero line number, if required.
4931 */
4932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004933correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004935 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
4937 if (eap->line1 == 0)
4938 eap->line1 = 1;
4939 if (eap->line2 == 0)
4940 eap->line2 = 1;
4941 }
4942}
4943
Bram Moolenaar748bf032005-02-02 23:04:36 +00004944#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004945/*
4946 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4947 * pattern. Otherwise return eap->arg.
4948 */
4949 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004950skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004951{
4952 char_u *p = eap->arg;
4953
Bram Moolenaara37420f2006-02-04 22:37:47 +00004954 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4955 || eap->cmdidx == CMD_vimgrepadd
4956 || eap->cmdidx == CMD_lvimgrepadd
4957 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004958 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004959 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004960 if (p == NULL)
4961 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004962 }
4963 return p;
4964}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004965
4966/*
4967 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4968 * in the command line, so that things like % get expanded.
4969 */
4970 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004971replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004972{
4973 char_u *new_cmdline;
4974 char_u *program;
4975 char_u *pos;
4976 char_u *ptr;
4977 int len;
4978 int i;
4979
4980 /*
4981 * Don't do it when ":vimgrep" is used for ":grep".
4982 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004983 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4984 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4985 || eap->cmdidx == CMD_grepadd
4986 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004987 && !grep_internal(eap->cmdidx))
4988 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004989 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4990 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004991 {
4992 if (*curbuf->b_p_gp == NUL)
4993 program = p_gp;
4994 else
4995 program = curbuf->b_p_gp;
4996 }
4997 else
4998 {
4999 if (*curbuf->b_p_mp == NUL)
5000 program = p_mp;
5001 else
5002 program = curbuf->b_p_mp;
5003 }
5004
5005 p = skipwhite(p);
5006
5007 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5008 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005009 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005010 i = 1;
5011 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5012 ++i;
5013 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005014 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005015 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005016 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005017 ptr = new_cmdline;
5018 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5019 {
5020 i = (int)(pos - program);
5021 STRNCPY(ptr, program, i);
5022 STRCPY(ptr += i, p);
5023 ptr += len;
5024 program = pos + 2;
5025 }
5026 STRCPY(ptr, program);
5027 }
5028 else
5029 {
John Marriotted908f72024-04-18 22:46:56 +02005030 size_t program_len = STRLEN(program);
5031
5032 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005033 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005034 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005035 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005036 STRCPY(new_cmdline + program_len, " ");
5037 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005038 }
5039 msg_make(p);
5040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005042 vim_free(*cmdlinep);
5043 *cmdlinep = new_cmdline;
5044 p = new_cmdline;
5045 }
5046 return p;
5047}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005048#endif
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050/*
5051 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005052 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 * Return FAIL for failure, OK otherwise.
5054 */
5055 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005056expand_filename(
5057 exarg_T *eap,
5058 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005059 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005061 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005063 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 char_u *p;
5065 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005066 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
Bram Moolenaar748bf032005-02-02 23:04:36 +00005068#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005069 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005070 p = skip_grep_pat(eap);
5071#else
5072 p = eap->arg;
5073#endif
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 /*
5076 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5077 * the file name contains a wildcard it should not cause expanding.
5078 * (it will be expanded anyway if there is a wildcard before replacing).
5079 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005080 has_wildcards = mch_has_wildcard(p);
5081 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005083#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005084 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005085 if (p[0] == '`' && p[1] == '=')
5086 {
5087 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005088 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005089 if (*p == '`')
5090 ++p;
5091 continue;
5092 }
5093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 /*
5095 * Quick check if this cannot be the start of a special string.
5096 * Also removes backslash before '%', '#' and '<'.
5097 */
5098 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5099 {
5100 ++p;
5101 continue;
5102 }
5103
5104 /*
5105 * Try to find a match at this position.
5106 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005107 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005108 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005111 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 p += srclen;
5114 continue;
5115 }
5116
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005117 // Wildcards won't be expanded below, the replacement is taken
5118 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005119 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5120 {
5121 char_u *l = repl;
5122
5123 repl = expand_env_save(repl);
5124 vim_free(l);
5125 }
5126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005127 // Need to escape white space et al. with a backslash.
5128 // Don't do this for:
5129 // - replacement that already has been escaped: "##"
5130 // - shell commands (may have to use quotes instead).
5131 // - non-unix systems when there is a single argument (spaces don't
5132 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005134 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 && eap->cmdidx != CMD_grep
5137 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005138 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005139 && eap->cmdidx != CMD_lgrep
5140 && eap->cmdidx != CMD_lgrepadd
5141 && eap->cmdidx != CMD_lmake
5142 && eap->cmdidx != CMD_make
5143 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005145 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146#endif
5147 )
5148 {
5149 char_u *l;
5150#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005151 // Don't escape a backslash here, because rem_backslash() doesn't
5152 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 static char_u *nobslash = (char_u *)" \t\"|";
5154# define ESCAPE_CHARS nobslash
5155#else
5156# define ESCAPE_CHARS escape_chars
5157#endif
5158
5159 for (l = repl; *l; ++l)
5160 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5161 {
5162 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5163 if (l != NULL)
5164 {
5165 vim_free(repl);
5166 repl = l;
5167 }
5168 break;
5169 }
5170 }
5171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005172 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005173 if ((eap->usefilter || eap->cmdidx == CMD_bang
5174 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005175 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 char_u *l;
5178
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005179 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if (l != NULL)
5181 {
5182 vim_free(repl);
5183 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185 }
5186
Mike Williams51024bb2024-05-30 07:46:30 +02005187 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 vim_free(repl);
5189 if (p == NULL)
5190 return FAIL;
5191 }
5192
5193 /*
5194 * One file argument: Expand wildcards.
5195 * Don't do this with ":r !command" or ":w !command".
5196 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005197 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
5199 /*
5200 * May do this twice:
5201 * 1. Replace environment variables.
5202 * 2. Replace any other wildcards, remove backslashes.
5203 */
5204 for (n = 1; n <= 2; ++n)
5205 {
5206 if (n == 2)
5207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 /*
5209 * Halve the number of backslashes (this is Vi compatible).
5210 * For Unix and OS/2, when wildcards are expanded, this is
5211 * done by ExpandOne() below.
5212 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005213#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (!has_wildcards)
5215#endif
5216 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218
5219 if (has_wildcards)
5220 {
5221 if (n == 1)
5222 {
5223 /*
5224 * First loop: May expand environment variables. This
5225 * can be done much faster with expand_env() than with
5226 * something else (e.g., calling a shell).
5227 * After expanding environment variables, check again
5228 * if there are still wildcards present.
5229 */
5230 if (vim_strchr(eap->arg, '$') != NULL
5231 || vim_strchr(eap->arg, '~') != NULL)
5232 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005233 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005234 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 has_wildcards = mch_has_wildcard(NameBuff);
5236 p = NameBuff;
5237 }
5238 else
5239 p = NULL;
5240 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005241 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
5243 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005244 int options = WILD_LIST_NOTFOUND
5245 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
5247 ExpandInit(&xpc);
5248 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005249 if (p_wic)
5250 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005252 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (p == NULL)
5254 return FAIL;
5255 }
5256 if (p != NULL)
5257 {
John Marriotted908f72024-04-18 22:46:56 +02005258 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005260 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 vim_free(p);
5262 }
5263 }
5264 }
5265 }
5266 return OK;
5267}
5268
5269/*
5270 * Replace part of the command line, keeping eap->cmd, eap->arg and
5271 * eap->nextcmd correct.
5272 * "src" points to the part that is to be replaced, of length "srclen".
5273 * "repl" is the replacement string.
5274 * Returns a pointer to the character after the replaced string.
5275 * Returns NULL for failure.
5276 */
5277 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005278repl_cmdline(
5279 exarg_T *eap,
5280 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005281 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005282 char_u *repl,
5283 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284{
John Marriotted908f72024-04-18 22:46:56 +02005285 size_t repllen;
5286 size_t taillen;
5287 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005289 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 /*
5292 * The new command line is build in new_cmdline[].
5293 * First allocate it.
5294 * Careful: a "+cmd" argument may have been NUL terminated.
5295 */
John Marriotted908f72024-04-18 22:46:56 +02005296 repllen = STRLEN(repl);
5297 taillen = STRLEN(src + srclen);
5298 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005299 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005300 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005301 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005302 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 /*
5305 * Copy the stuff before the expanded part.
5306 * Copy the expanded stuff.
5307 * Copy what came after the expanded part.
5308 * Copy the next commands, if there are any.
5309 */
John Marriotted908f72024-04-18 22:46:56 +02005310 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5311 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005312
John Marriotted908f72024-04-18 22:46:56 +02005313 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5314 new_cmdlinelen += repllen; // remember the end of the string
5315 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5316 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005318 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
John Marriotted908f72024-04-18 22:46:56 +02005320 new_cmdlinelen += taillen + 1;
5321 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5322 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5325 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5326 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5327 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5328 vim_free(*cmdlinep);
5329 *cmdlinep = new_cmdline;
5330
5331 return src;
5332}
5333
5334/*
5335 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005336 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 */
5338 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005339separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
5341 char_u *p;
5342
Bram Moolenaar86b68352004-12-27 21:59:20 +00005343#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005344 p = skip_grep_pat(eap);
5345#else
5346 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005347#endif
5348
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005349 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
5351 if (*p == Ctrl_V)
5352 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005353 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005354 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005356 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005357 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005358 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 break;
5360 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005361
5362#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005363 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005364 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005365 {
5366 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005367 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005368 if (*p == NUL) // stop at NUL after CTRL-V
5369 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005370 }
5371#endif
5372
Bram Moolenaareda29c92022-10-02 12:59:00 +01005373 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005374 // :@" and :*" do not start a comment!
5375 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005376 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005377 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005378 && !(eap->argt & EX_NOTRLCOM)
5379 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5380 || p != eap->arg)
5381 && (eap->cmdidx != CMD_redir
5382 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005383 || (*p == '#'
5384 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005385 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005386 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005387 || (*p == '|'
5388 && eap->cmdidx != CMD_append
5389 && eap->cmdidx != CMD_change
5390 && eap->cmdidx != CMD_insert)
5391 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
5393 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005394 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 * AND 'b' is present in 'cpoptions'.
5396 */
5397 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005398 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005400 if (!keep_backslash)
5401 {
5402 STRMOVE(p - 1, p); // remove the '\'
5403 --p;
5404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406 else
5407 {
5408 eap->nextcmd = check_nextcmd(p);
5409 *p = NUL;
5410 break;
5411 }
5412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005415 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 del_trailing_spaces(eap->arg);
5417}
5418
5419/*
5420 * get + command from ex argument
5421 */
5422 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005423getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 char_u *arg = *argp;
5426 char_u *command = NULL;
5427
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005428 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005431 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 command = dollar_command;
5433 else
5434 {
5435 command = arg;
5436 arg = skip_cmd_arg(command, TRUE);
5437 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005438 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
5440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005441 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 *argp = arg;
5443 }
5444 return command;
5445}
5446
5447/*
5448 * Find end of "+command" argument. Skip over "\ " and "\\".
5449 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005450 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005451skip_cmd_arg(
5452 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005453 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
5455 while (*p && !vim_isspace(*p))
5456 {
5457 if (*p == '\\' && p[1] != NUL)
5458 {
5459 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005460 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 else
5462 ++p;
5463 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005464 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 }
5466 return p;
5467}
5468
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005469 int
5470get_bad_opt(char_u *p, exarg_T *eap)
5471{
5472 if (STRICMP(p, "keep") == 0)
5473 eap->bad_char = BAD_KEEP;
5474 else if (STRICMP(p, "drop") == 0)
5475 eap->bad_char = BAD_DROP;
5476 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5477 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005478 else
5479 return FAIL;
5480 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005481}
5482
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005484 * Function given to ExpandGeneric() to obtain the list of bad= names.
5485 */
5486 static char_u *
5487get_bad_name(expand_T *xp UNUSED, int idx)
5488{
5489 // Note: Keep this in sync with getargopt.
5490 static char *(p_bad_values[]) =
5491 {
5492 "?",
5493 "keep",
5494 "drop",
5495 };
5496
John Marriotted908f72024-04-18 22:46:56 +02005497 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5498 return NULL;
5499
5500 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005501}
5502
5503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 * Get "++opt=arg" argument.
5505 * Return FAIL or OK.
5506 */
5507 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005508getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
5510 char_u *arg = eap->arg + 2;
5511 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005512 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005515 // Note: Keep this in sync with get_argopt_name.
5516
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005517 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5519 {
5520 if (*arg == 'n')
5521 {
5522 arg += 2;
5523 eap->force_bin = FORCE_NOBIN;
5524 }
5525 else
5526 eap->force_bin = FORCE_BIN;
5527 if (!checkforcmd(&arg, "binary", 3))
5528 return FAIL;
5529 eap->arg = skipwhite(arg);
5530 return OK;
5531 }
5532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005533 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005534 if (STRNCMP(arg, "edit", 4) == 0)
5535 {
5536 eap->read_edit = TRUE;
5537 eap->arg = skipwhite(arg + 4);
5538 return OK;
5539 }
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 if (STRNCMP(arg, "ff", 2) == 0)
5542 {
5543 arg += 2;
5544 pp = &eap->force_ff;
5545 }
5546 else if (STRNCMP(arg, "fileformat", 10) == 0)
5547 {
5548 arg += 10;
5549 pp = &eap->force_ff;
5550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 else if (STRNCMP(arg, "enc", 3) == 0)
5552 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005553 if (STRNCMP(arg, "encoding", 8) == 0)
5554 arg += 8;
5555 else
5556 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 pp = &eap->force_enc;
5558 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005559 else if (STRNCMP(arg, "bad", 3) == 0)
5560 {
5561 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005562 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
5565 if (pp == NULL || *arg != '=')
5566 return FAIL;
5567
5568 ++arg;
5569 *pp = (int)(arg - eap->cmd);
5570 arg = skip_cmd_arg(arg, FALSE);
5571 eap->arg = skipwhite(arg);
5572 *arg = NUL;
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 if (pp == &eap->force_ff)
5575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5577 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005578 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005580 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005582 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5584 *p = TOLOWER_ASC(*p);
5585 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005586 else
5587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005588 // Check ++bad= argument. Must be a single-byte character, "keep" or
5589 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005590 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005591 return FAIL;
5592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
5594 return OK;
5595}
5596
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005597/*
5598 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5599 */
5600 static char_u *
5601get_argopt_name(expand_T *xp UNUSED, int idx)
5602{
5603 // Note: Keep this in sync with getargopt.
5604 static char *(p_opt_values[]) =
5605 {
5606 "fileformat=",
5607 "encoding=",
5608 "binary",
5609 "nobinary",
5610 "bad=",
5611 "edit",
5612 };
5613
John Marriotted908f72024-04-18 22:46:56 +02005614 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5615 return NULL;
5616
5617 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005618}
5619
5620/*
5621 * Command-line expansion for ++opt=name.
5622 */
5623 int
5624expand_argopt(
5625 char_u *pat,
5626 expand_T *xp,
5627 regmatch_T *rmp,
5628 char_u ***matches,
5629 int *numMatches)
5630{
5631 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5632 {
5633 char_u *(*cb)(expand_T *, int) = NULL;
5634
5635 char_u *name_end = xp->xp_pattern - 1;
5636 if (name_end - xp->xp_line >= 2
5637 && STRNCMP(name_end - 2, "ff", 2) == 0)
5638 cb = get_fileformat_name;
5639 else if (name_end - xp->xp_line >= 10
5640 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5641 cb = get_fileformat_name;
5642 else if (name_end - xp->xp_line >= 3
5643 && STRNCMP(name_end - 3, "enc", 3) == 0)
5644 cb = get_encoding_name;
5645 else if (name_end - xp->xp_line >= 8
5646 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5647 cb = get_encoding_name;
5648 else if (name_end - xp->xp_line >= 3
5649 && STRNCMP(name_end - 3, "bad", 3) == 0)
5650 cb = get_bad_name;
5651
5652 if (cb != NULL)
5653 {
5654 return ExpandGeneric(
5655 pat,
5656 xp,
5657 rmp,
5658 matches,
5659 numMatches,
5660 cb,
5661 FALSE);
5662 }
5663 return FAIL;
5664 }
5665
5666 // Special handling of "ff" which acts as a short form of
5667 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005668 if (xp->xp_pattern_len == 2
5669 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005670 {
5671 *matches = ALLOC_MULT(char_u *, 1);
5672 if (*matches == NULL)
5673 return FAIL;
5674 *numMatches = 1;
5675 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5676 return OK;
5677 }
5678
5679 return ExpandGeneric(
5680 pat,
5681 xp,
5682 rmp,
5683 matches,
5684 numMatches,
5685 get_argopt_name,
5686 FALSE);
5687}
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005690ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005693 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 * directory for security reasons.
5695 */
5696 if (secure)
5697 {
5698 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005699 eap->errmsg =
5700 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005703 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 else
5705 do_augroup(eap->arg, eap->forceit);
5706}
5707
5708/*
5709 * ":doautocmd": Apply the automatic commands to the current buffer.
5710 */
5711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005712ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005714 char_u *arg = eap->arg;
5715 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005716 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005717
Bram Moolenaar1610d052016-06-09 22:53:01 +02005718 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005719 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005720 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005721 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724/*
5725 * :[N]bunload[!] [N] [bufname] unload buffer
5726 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5727 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5728 */
5729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005730ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005732 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005733 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 eap->errmsg = do_bufdel(
5735 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5736 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5737 : DOBUF_UNLOAD, eap->arg,
5738 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5739}
5740
5741/*
5742 * :[N]buffer [N] to buffer N
5743 * :[N]sbuffer [N] to buffer N
5744 */
5745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005746ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005748 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005749 return;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01005750 do_exbuffer(eap);
5751}
5752
5753/*
5754 * ":buffer" command and alike.
5755 */
5756 static void
5757do_exbuffer(exarg_T *eap)
5758{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005760 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 else
5762 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005763 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5765 else
5766 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005767 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005768 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
5770}
5771
5772/*
5773 * :[N]bmodified [N] to next mod. buffer
5774 * :[N]sbmodified [N] to next mod. buffer
5775 */
5776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005777ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005780 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005781 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782}
5783
5784/*
5785 * :[N]bnext [N] to next buffer
5786 * :[N]sbnext [N] split and to next buffer
5787 */
5788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005789ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005791 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005792 return;
5793
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005795 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005796 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797}
5798
5799/*
5800 * :[N]bNext [N] to previous buffer
5801 * :[N]bprevious [N] to previous buffer
5802 * :[N]sbNext [N] split and to previous buffer
5803 * :[N]sbprevious [N] split and to previous buffer
5804 */
5805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005806ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005808 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005809 return;
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005812 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005813 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814}
5815
5816/*
5817 * :brewind to first buffer
5818 * :bfirst to first buffer
5819 * :sbrewind split and to first buffer
5820 * :sbfirst split and to first buffer
5821 */
5822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005823ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005825 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005826 return;
5827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005829 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005830 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831}
5832
5833/*
5834 * :blast to last buffer
5835 * :sblast split and to last buffer
5836 */
5837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005838ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005840 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005841 return;
5842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005844 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005845 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
Bram Moolenaar7a092242020-04-16 22:10:49 +02005848/*
5849 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005850 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005853ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005855 int comment_char = '"';
5856
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005857 if (in_vim9script())
5858 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005859 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005860}
5861
5862/*
5863 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5864 * to "cmd_start" or has a white space character before it.
5865 */
5866 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005867ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005868{
5869 int c = *cmd;
5870
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005871 if (c == NUL || c == '|' || c == '\n')
5872 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005873 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005874 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5875 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005876 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005877 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878}
5879
5880#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5881 || defined(PROTO)
5882/*
5883 * Return the next command, after the first '|' or '\n'.
5884 * Return NULL if not found.
5885 */
5886 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005887find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888{
5889 while (*p != '|' && *p != '\n')
5890 {
5891 if (*p == NUL)
5892 return NULL;
5893 ++p;
5894 }
5895 return (p + 1);
5896}
5897#endif
5898
5899/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005900 * Check if *p is a separator between Ex commands, skipping over white space.
5901 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 */
5903 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005904check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005906 char_u *s = skipwhite(p);
5907
5908 if (*s == '|' || *s == '\n')
5909 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 else
5911 return NULL;
5912}
5913
5914/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005915 * If "eap->nextcmd" is not set, check for a next command at "p".
5916 */
5917 void
5918set_nextcmd(exarg_T *eap, char_u *arg)
5919{
5920 char_u *p = check_nextcmd(arg);
5921
5922 if (eap->nextcmd == NULL)
5923 eap->nextcmd = p;
5924 else if (p != NULL)
5925 // cannot use "| command" inside a {} block
5926 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5927}
5928
5929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 * - if there are more files to edit
5931 * - and this is the last window
5932 * - and forceit not used
5933 * - and not repeated twice on a row
5934 * return FAIL and give error message if 'message' TRUE
5935 * return OK otherwise
5936 */
5937 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005938check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005939 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005940 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5943
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005944 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005945 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 {
5947 if (message)
5948 {
5949#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005950 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5951 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005953 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005955 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5956 NGETTEXT("%d more file to edit. Quit anyway?",
5957 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5959 return OK;
5960 return FAIL;
5961 }
5962#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005963 semsg(NGETTEXT(e_nr_more_file_to_edit,
5964 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005965 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
5967 return FAIL;
5968 }
5969 return OK;
5970}
5971
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972/*
5973 * Function given to ExpandGeneric() to obtain the list of command names.
5974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005976get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005979 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005980 // the following are no real commands
5981 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02005982 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
Christian Brabandt70a11a62024-07-26 19:13:55 +02005983 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 return cmdnames[idx].cmd_name;
5985}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005988ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
Bram Moolenaare6850792010-05-14 15:28:44 +02005990 if (*eap->arg == NUL)
5991 {
5992#ifdef FEAT_EVAL
5993 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5994 char_u *p = NULL;
5995
5996 if (expr != NULL)
5997 {
5998 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005999 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02006000 --emsg_off;
6001 vim_free(expr);
6002 }
6003 if (p != NULL)
6004 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006005 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02006006 vim_free(p);
6007 }
6008 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006009 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006010#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006011 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006012#endif
6013 }
6014 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006015 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006016
6017#ifdef FEAT_VTP
6018 else if (has_vtp_working())
6019 {
6020 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006021 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006022 redrawcmd();
6023 }
6024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025}
6026
6027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006028ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006031 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 do_highlight(eap->arg, eap->forceit, FALSE);
6033}
6034
6035
6036/*
6037 * Call this function if we thought we were going to exit, but we won't
6038 * (because of an error). May need to restore the terminal mode.
6039 */
6040 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006041not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042{
6043 exiting = FALSE;
6044 settmode(TMODE_RAW);
6045}
6046
Bram Moolenaar3552e742021-05-29 12:21:58 +02006047 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006048before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6049{
6050 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6051
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006052 // Bail out when autocommands closed the window.
6053 // Refuse to quit when the buffer in the last window is being closed (can
6054 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006055 if (!win_valid(wp)
6056 || curbuf_locked()
6057 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6058 return TRUE;
6059
6060 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6061 {
6062 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006063 // Refuse to quit when locked or when the window was closed or the
6064 // buffer in the last window is being closed (can only happen in
6065 // autocommands).
6066 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006067 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6068 return TRUE;
6069 }
6070
6071 return FALSE;
6072}
6073
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006075 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006076 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006077 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006079 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006080ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006082 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (cmdwin_type != 0)
6085 {
6086 cmdwin_result = Ctrl_C;
6087 return;
6088 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006089 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006090 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006091 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006092 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006093 return;
6094 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006095 if (eap->addr_count > 0)
6096 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006097 int wnr = eap->line2;
6098
6099 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6100 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006101 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006102 }
6103 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006104 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006106 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006107 if (curbuf_locked())
6108 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006110 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006111 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006112 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113
6114#ifdef FEAT_NETBEANS_INTG
6115 netbeansForcedQuit = eap->forceit;
6116#endif
6117
6118 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006119 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 */
6121 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6122 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006123 if ((!buf_hide(wp->w_buffer)
6124 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006125 | (eap->forceit ? CCGD_FORCEIT : 0)
6126 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006128 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {
6130 not_exiting();
6131 }
6132 else
6133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006134 // quit last window
6135 // Note: only_one_window() returns true, even so a help window is
6136 // still open. In that case only quit, if no address has been
6137 // specified. Example:
6138 // :h|wincmd w|1q - don't quit
6139 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006140 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006142 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006143#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006145#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006146 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006147 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
6149}
6150
6151/*
6152 * ":cquit".
6153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006155ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006157 // this does not always pass on the exit code to the Manx compiler. why?
6158 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159}
6160
6161/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006162 * Do preparations for "qall" and "wqall".
6163 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006165 int
6166before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (cmdwin_type != 0)
6169 {
6170 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006171 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 else
6173 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006174 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006176
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006177 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006178 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006179 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006180 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006181 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006182 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006183
6184 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006185 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006186
Bram Moolenaar411da642023-05-10 16:53:27 +01006187 return OK;
6188}
6189
6190/*
6191 * ":qall": try to quit all windows
6192 */
6193 static void
6194ex_quit_all(exarg_T *eap)
6195{
6196 if (before_quit_all(eap) == FAIL)
6197 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006199 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 getout(0);
6201 not_exiting();
6202}
6203
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204/*
6205 * ":close": close current window, unless it is the last one
6206 */
6207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006208ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006210 win_T *win;
6211 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006213 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006215 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006216 {
6217 if (eap->addr_count == 0)
6218 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006219 else
6220 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006221 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006222 {
6223 winnr++;
6224 if (winnr == eap->line2)
6225 break;
6226 }
6227 if (win == NULL)
6228 win = lastwin;
6229 ex_win_close(eap->forceit, win, NULL);
6230 }
6231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232}
6233
Bram Moolenaar4033c552017-09-16 20:54:51 +02006234#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006235/*
6236 * ":pclose": Close any preview window.
6237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006239ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240{
6241 win_T *win;
6242
Bram Moolenaar79648732019-07-18 21:43:07 +02006243 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006244 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006245 if (win->w_p_pvw)
6246 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006247 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006248 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006249 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006250# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006251 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006252 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006253# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006255#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006256
Bram Moolenaarf740b292006-02-16 22:11:02 +00006257/*
6258 * Close window "win" and take care of handling closing the last window for a
6259 * modified buffer.
6260 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006261 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006262ex_win_close(
6263 int forceit,
6264 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006265 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266{
6267 int need_hide;
6268 buf_T *buf = win->w_buffer;
6269
Bram Moolenaarcf844172020-06-26 19:44:06 +02006270 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006271 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006272 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006273 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006274 return;
6275 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006276 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006277 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006280 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006282#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006283 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006285# ifdef FEAT_TERMINAL
6286 if (term_job_running(buf->b_term))
6287 {
6288 if (term_confirm_stop(buf) == FAIL)
6289 return;
6290 // Manually kill the terminal here because this command will
6291 // hide it otherwise.
6292 free_terminal(buf);
6293 need_hide = FALSE;
6294 }
6295 else
6296# endif
6297 {
6298 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006299
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006300 set_bufref(&bufref, buf);
6301 dialog_changed(buf, FALSE);
6302 if (bufref_valid(&bufref) && bufIsChanged(buf))
6303 return;
6304 need_hide = FALSE;
6305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 }
6307 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006310 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 return;
6312 }
6313 }
6314
Bram Moolenaar4033c552017-09-16 20:54:51 +02006315#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006317#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006318
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006319 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006320 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006321 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006322 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006323 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324}
6325
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006327 * Handle the argument for a tabpage related ex command.
6328 * Returns a tabpage number.
6329 * When an error is encountered then eap->errmsg is set.
6330 */
6331 static int
6332get_tabpage_arg(exarg_T *eap)
6333{
6334 int tab_number;
6335 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6336
6337 if (eap->arg && *eap->arg != NUL)
6338 {
6339 char_u *p = eap->arg;
6340 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006341 int relative = 0; // argument +N/-N means: go to N places to the
6342 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006343
6344 if (*p == '-')
6345 {
6346 relative = -1;
6347 p++;
6348 }
6349 else if (*p == '+')
6350 {
6351 relative = 1;
6352 p++;
6353 }
6354
6355 p_save = p;
6356 tab_number = getdigits(&p);
6357
6358 if (relative == 0)
6359 {
6360 if (STRCMP(p, "$") == 0)
6361 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006362 else if (STRCMP(p, "#") == 0)
6363 if (valid_tabpage(lastused_tabpage))
6364 tab_number = tabpage_index(lastused_tabpage);
6365 else
6366 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006367 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6368 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006369 tab_number = 0;
6370 goto theend;
6371 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006372 else if (p == p_save || *p_save == '-' || *p != NUL
6373 || tab_number > LAST_TAB_NR)
6374 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006375 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006376 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006377 goto theend;
6378 }
6379 }
6380 else
6381 {
6382 if (*p_save == NUL)
6383 tab_number = 1;
6384 else if (p == p_save || *p_save == '-' || *p != NUL
6385 || tab_number == 0)
6386 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006387 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006388 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006389 goto theend;
6390 }
6391 tab_number = tab_number * relative + tabpage_index(curtab);
6392 if (!unaccept_arg0 && relative == -1)
6393 --tab_number;
6394 }
6395 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006396 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006397 }
6398 else if (eap->addr_count > 0)
6399 {
6400 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006401 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006402 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006403 tab_number = 0;
6404 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006405 else
6406 {
6407 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006408 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006409 {
zeertzjq076faac2024-03-25 16:41:06 +01006410 char_u *cmdp = eap->cmd;
6411
6412 while (--cmdp > *eap->cmdlinep
6413 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6414 ;
6415 if (*cmdp == '-')
6416 {
6417 --tab_number;
6418 if (tab_number < unaccept_arg0)
6419 eap->errmsg = _(e_invalid_range);
6420 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006421 }
6422 }
6423 }
6424 else
6425 {
6426 switch (eap->cmdidx)
6427 {
6428 case CMD_tabnext:
6429 tab_number = tabpage_index(curtab) + 1;
6430 if (tab_number > LAST_TAB_NR)
6431 tab_number = 1;
6432 break;
6433 case CMD_tabmove:
6434 tab_number = LAST_TAB_NR;
6435 break;
6436 default:
6437 tab_number = tabpage_index(curtab);
6438 }
6439 }
6440
6441theend:
6442 return tab_number;
6443}
6444
6445/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006446 * ":tabclose": close current tab page, unless it is the last one.
6447 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 */
6449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006450ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006452 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006453 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006454
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006455 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006456 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006457 cmdwin_result = K_IGNORE;
6458 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006459 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006460
6461 if (first_tabpage->tp_next == NULL)
6462 {
6463 emsg(_(e_cannot_close_last_tab_page));
6464 return;
6465 }
6466
6467 if (window_layout_locked(CMD_tabclose))
6468 return;
6469
6470 tab_number = get_tabpage_arg(eap);
6471 if (eap->errmsg != NULL)
6472 return;
6473
6474 tp = find_tabpage(tab_number);
6475 if (tp == NULL)
6476 {
6477 beep_flush();
6478 return;
6479 }
6480 if (tp != curtab)
6481 {
6482 tabpage_close_other(tp, eap->forceit);
6483 return;
6484 }
6485 else if (!text_locked() && !curbuf_locked())
6486 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006487}
6488
6489/*
6490 * ":tabonly": close all tab pages except the current one
6491 */
6492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006493ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006494{
6495 tabpage_T *tp;
6496 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006497 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006498
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006499 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006500 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006501 cmdwin_result = K_IGNORE;
6502 return;
6503 }
6504
6505 if (first_tabpage->tp_next == NULL)
6506 {
6507 msg(_("Already only one tab page"));
6508 return;
6509 }
6510
6511 if (window_layout_locked(CMD_tabonly))
6512 return;
6513
6514 tab_number = get_tabpage_arg(eap);
6515 if (eap->errmsg != NULL)
6516 return;
6517
6518 goto_tabpage(tab_number);
6519 // Repeat this up to a 1000 times, because autocommands may
6520 // mess up the lists.
6521 for (done = 0; done < 1000; ++done)
6522 {
6523 FOR_ALL_TABPAGES(tp)
6524 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006525 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006526 tabpage_close_other(tp, eap->forceit);
6527 // if we failed to close it quit
6528 if (valid_tabpage(tp))
6529 done = 1000;
6530 // start over, "tp" is now invalid
6531 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006532 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006533 if (first_tabpage->tp_next == NULL)
6534 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537
6538/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006539 * Close the current tab page.
6540 */
6541 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006542tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006543{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006544 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006545 return;
6546
Jim Zhoubcf66e02025-03-16 20:24:57 +01006547 trigger_tabclosedpre(curtab, TRUE);
6548
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006549 // First close all the windows but the current one. If that worked then
6550 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006551 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006552 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006553 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006554 ex_win_close(forceit, curwin, NULL);
6555# ifdef FEAT_GUI
6556 need_mouse_correct = TRUE;
6557# endif
6558}
6559
6560/*
6561 * Close tab page "tp", which is not the current tab page.
6562 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006563 * Also takes care of the tab pages line disappearing when closing the
6564 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006565 */
6566 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006567tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006568{
6569 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006570 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006571
Jim Zhoubcf66e02025-03-16 20:24:57 +01006572 trigger_tabclosedpre(tp, TRUE);
6573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006574 // Limit to 1000 windows, autocommands may add a window while we close
6575 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006576 while (++done < 1000)
6577 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006578 wp = tp->tp_firstwin;
6579 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006581 // Autocommands may delete the tab page under our fingers and we may
6582 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006583 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006584 break;
6585 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006586
Bram Moolenaar29323592016-07-24 22:04:11 +02006587 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006588}
6589
6590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 * ":only".
6592 */
6593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006594ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006596 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006597 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598# ifdef FEAT_GUI
6599 need_mouse_correct = TRUE;
6600# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006601 if (eap->addr_count > 0)
6602 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006603 win_T *wp;
6604 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006605 for (wp = firstwin; --wnr > 0; )
6606 {
6607 if (wp->w_next == NULL)
6608 break;
6609 else
6610 wp = wp->w_next;
6611 }
6612 win_goto(wp);
6613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 close_others(TRUE, eap->forceit);
6615}
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006618ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006620 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006621 if (eap->skip)
6622 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006623
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006624 if (window_layout_locked(CMD_hide))
6625 return;
6626#ifdef FEAT_GUI
6627 need_mouse_correct = TRUE;
6628#endif
6629 if (eap->addr_count == 0)
6630 win_close(curwin, FALSE); // don't free buffer
6631 else
6632 {
6633 int winnr = 0;
6634 win_T *win;
6635
6636 FOR_ALL_WINDOWS(win)
6637 {
6638 winnr++;
6639 if (winnr == eap->line2)
6640 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006642 if (win == NULL)
6643 win = lastwin;
6644 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
6646}
6647
6648/*
6649 * ":stop" and ":suspend": Suspend Vim.
6650 */
dbivolaruab16ad32021-12-29 19:41:47 +00006651 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006652ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
6654 /*
6655 * Disallow suspending for "rvim".
6656 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006657 if (check_restricted())
6658 return;
6659
6660 if (!eap->forceit)
6661 autowrite_all();
6662 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6663 windgoto((int)Rows - 1, 0);
6664 out_char('\n');
6665 out_flush();
6666 stoptermcap();
6667 out_flush(); // needed for SUN to restore xterm buffer
6668 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6669 ui_suspend(); // call machine specific function
6670 maketitle();
6671 resettitle(); // force updating the title
6672 starttermcap();
6673 scroll_start(); // scroll screen before redrawing
6674 redraw_later_clear();
6675 shell_resized(); // may have resized window
6676 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677}
6678
6679/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006680 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 */
6682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006683ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006685#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006686 if (not_in_vim9(eap) == FAIL)
6687 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 if (cmdwin_type != 0)
6690 {
6691 cmdwin_result = Ctrl_C;
6692 return;
6693 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006694 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006695 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006696 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006697 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006698 return;
6699 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006702 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 */
6704 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6705 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006706
6707 // Write the buffer for ":wq" or when it was changed.
6708 // Trigger QuitPre and ExitPre.
6709 // Check if we can exit now, after autocommands have changed things.
6710 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6711 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006713 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
6715 not_exiting();
6716 }
6717 else
6718 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006719 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006721 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722# ifdef FEAT_GUI
6723 need_mouse_correct = TRUE;
6724# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006725 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006726 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 }
6728}
6729
6730/*
6731 * ":print", ":list", ":number".
6732 */
6733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006734ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006736 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006737 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006740 for ( ;!got_int; ui_breakcheck())
6741 {
6742 print_line(eap->line1,
6743 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6744 || (eap->flags & EXFLAG_NR)),
6745 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6746 if (++eap->line1 > eap->line2)
6747 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006748 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006749 }
6750 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006751 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006752 curwin->w_cursor.lnum = eap->line2;
6753 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 }
6755
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757}
6758
6759#ifdef FEAT_BYTEOFF
6760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006761ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 goto_byte(eap->line2);
6764}
6765#endif
6766
6767/*
6768 * ":shell".
6769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006771ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 do_shell(NULL, 0);
6774}
6775
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006776#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006778static int drop_busy = FALSE;
6779static int drop_filec;
6780static char_u **drop_filev = NULL;
6781static int drop_split;
6782static void (*drop_callback)(void *);
6783static void *drop_cookie;
6784
6785 static void
6786handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787{
6788 exarg_T ea;
6789 int save_msg_scroll = msg_scroll;
6790
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006791 // Setting the argument list may cause screen updates and being called
6792 // recursively. Avoid that by setting drop_busy.
6793 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006795 // Check whether the current buffer is changed. If so, we will need
6796 // to split the current window or data could be lost.
6797 // We don't need to check if the 'hidden' option is set, as in this
6798 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006799 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {
6801 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006802 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 --emsg_off;
6804 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006805 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (win_split(0, 0) == FAIL)
6808 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006809 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006811 // When splitting the window, create a new alist. Otherwise the
6812 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 alist_unlink(curwin->w_alist);
6814 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816
6817 /*
6818 * Set up the new argument list.
6819 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006820 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
6822 /*
6823 * Move to the first file.
6824 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006825 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006826 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 ea.cmd = (char_u *)"next";
6828 do_argfile(&ea, 0);
6829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006830 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6831 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 need_start_insertmode = FALSE;
6833
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006834 // Restore msg_scroll, otherwise a following command may cause scrolling
6835 // unexpectedly. The screen will be redrawn by the caller, thus
6836 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006838
6839 if (drop_callback != NULL)
6840 drop_callback(drop_cookie);
6841
6842 drop_filev = NULL;
6843 drop_busy = FALSE;
6844}
6845
6846/*
6847 * Handle a file drop. The code is here because a drop is *nearly* like an
6848 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006849 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006850 * code is very similar to :args and hence needs access to a lot of the static
6851 * functions in this file.
6852 *
6853 * The "filev" list must have been allocated using alloc(), as should each item
6854 * in the list. This function takes over responsibility for freeing the "filev"
6855 * list.
6856 */
6857 void
6858handle_drop(
6859 int filec, // the number of files dropped
6860 char_u **filev, // the list of files dropped
6861 int split, // force splitting the window
6862 void (*callback)(void *), // to be called after setting the argument
6863 // list
6864 void *cookie) // argument for "callback" (allocated)
6865{
6866 // Cannot handle recursive drops, finish the pending one.
6867 if (drop_busy)
6868 {
6869 FreeWild(filec, filev);
6870 vim_free(cookie);
6871 return;
6872 }
6873
6874 // When calling handle_drop() more than once in a row we only use the last
6875 // one.
6876 if (drop_filev != NULL)
6877 {
6878 FreeWild(drop_filec, drop_filev);
6879 vim_free(drop_cookie);
6880 }
6881
6882 drop_filec = filec;
6883 drop_filev = filev;
6884 drop_split = split;
6885 drop_callback = callback;
6886 drop_cookie = cookie;
6887
6888 // Postpone this when:
6889 // - editing the command line
6890 // - not possible to change the current buffer
6891 // - updating the screen
6892 // As it may change buffers and window structures that are in use and cause
6893 // freed memory to be used.
6894 if (text_locked() || curbuf_locked() || updating_screen)
6895 return;
6896
6897 handle_drop_internal();
6898}
6899
6900/*
6901 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6902 * reset: Handle a postponed drop.
6903 */
6904 void
6905handle_any_postponed_drop(void)
6906{
6907 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006908 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006909 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910}
6911#endif
6912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 * ":preserve".
6915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006917ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006919 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 ml_preserve(curbuf, TRUE);
6921}
6922
6923/*
6924 * ":recover".
6925 */
6926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006927ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006929 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006931 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6932 | CCGD_MULTWIN
6933 | (eap->forceit ? CCGD_FORCEIT : 0)
6934 | CCGD_EXCMD)
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 && (*eap->arg == NUL
6937 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006938 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 recoverymode = FALSE;
6940}
6941
6942/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006943 * Command modifier used in a wrong way. Also for other commands that can't
6944 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 */
6946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006947ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006949 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950}
6951
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006952#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006953
6954// callback function for 'findfunc'
6955static callback_T ffu_cb;
6956
6957 static callback_T *
6958get_findfunc_callback(void)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006959{
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006960 return *curbuf->b_p_ffu != NUL ? &curbuf->b_ffu_cb : &ffu_cb;
6961}
6962
zeertzjq6eda2692024-11-03 09:23:33 +01006963/*
6964 * Call 'findfunc' to obtain a list of file names.
6965 */
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006966 static list_T *
6967call_findfunc(char_u *pat, int cmdcomplete)
6968{
6969 typval_T args[3];
6970 callback_T *cb;
6971 typval_T rettv;
6972 int retval;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006973 sctx_T saved_sctx = current_sctx;
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006974 sctx_T *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006975
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006976 args[0].v_type = VAR_STRING;
6977 args[0].vval.v_string = pat;
6978 args[1].v_type = VAR_BOOL;
6979 args[1].vval.v_number = cmdcomplete;
6980 args[2].v_type = VAR_UNKNOWN;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006981
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006982 // Lock the text to prevent weird things from happening. Also disallow
6983 // switching to another window, it should not be needed and may end up in
6984 // Insert mode in another buffer.
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006985 ++textlock;
6986
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006987 ctx = get_option_sctx("findfunc");
6988 if (ctx != NULL)
6989 current_sctx = *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006990
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006991 cb = get_findfunc_callback();
6992 retval = call_callback(cb, -1, &rettv, 2, args);
6993
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006994 current_sctx = saved_sctx;
6995
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006996 --textlock;
6997
6998 list_T *retlist = NULL;
6999
7000 if (retval == OK)
7001 {
7002 if (rettv.v_type == VAR_LIST)
7003 retlist = list_copy(rettv.vval.v_list, FALSE, FALSE, get_copyID());
7004 else
7005 emsg(_(e_invalid_return_type_from_findfunc));
7006
7007 clear_tv(&rettv);
7008 }
7009
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007010 return retlist;
7011}
7012
7013/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007014 * Find file names matching "pat" using 'findfunc' and return it in "files".
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007015 * Used for expanding the :find, :sfind and :tabfind command argument.
7016 * Returns OK on success and FAIL otherwise.
7017 */
7018 int
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007019expand_findfunc(char_u *pat, char_u ***files, int *numMatches)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007020{
7021 list_T *l;
7022 int len;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007023
7024 *numMatches = 0;
7025 *files = NULL;
7026
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007027 l = call_findfunc(pat, VVAL_TRUE);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007028
7029 if (l == NULL)
7030 return FAIL;
7031
7032 len = list_len(l);
7033 if (len == 0) // empty List
7034 return FAIL;
7035
7036 *files = ALLOC_MULT(char_u *, len);
7037 if (*files == NULL)
7038 return FAIL;
7039
7040 // Copy all the List items
7041 listitem_T *li;
7042 int idx = 0;
7043 FOR_ALL_LIST_ITEMS(l, li)
7044 {
7045 if (li->li_tv.v_type == VAR_STRING)
7046 {
7047 (*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
7048 idx++;
7049 }
7050 }
7051
7052 *numMatches = idx;
7053 list_free(l);
7054
7055 return OK;
7056}
7057
7058/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007059 * Use 'findfunc' to find file 'findarg'. The 'count' argument is used to find
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007060 * the n'th matching file.
7061 */
7062 static char_u *
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007063findfunc_find_file(char_u *findarg, int findarg_len, int count)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007064{
7065 list_T *fname_list;
7066 char_u *ret_fname = NULL;
7067 char_u cc;
7068 int fname_count;
7069
7070 cc = findarg[findarg_len];
7071 findarg[findarg_len] = NUL;
7072
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007073 fname_list = call_findfunc(findarg, VVAL_FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007074 fname_count = list_len(fname_list);
7075
7076 if (fname_count == 0)
7077 semsg(_(e_cant_find_file_str_in_path), findarg);
7078 else
7079 {
7080 if (count > fname_count)
7081 semsg(_(e_no_more_file_str_found_in_path), findarg);
7082 else
7083 {
7084 listitem_T *li = list_find(fname_list, count - 1);
7085 if (li != NULL && li->li_tv.v_type == VAR_STRING)
7086 ret_fname = vim_strsave(li->li_tv.vval.v_string);
7087 }
7088 }
7089
7090 if (fname_list != NULL)
7091 list_free(fname_list);
7092
7093 findarg[findarg_len] = cc;
7094
7095 return ret_fname;
7096}
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007097
7098/*
7099 * Process the 'findfunc' option value.
7100 * Returns NULL on success and an error message on failure.
7101 */
7102 char *
7103did_set_findfunc(optset_T *args UNUSED)
7104{
7105 int retval;
7106
zeertzjq6eda2692024-11-03 09:23:33 +01007107 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007108 // buffer-local option set
7109 retval = option_set_callback_func(curbuf->b_p_ffu, &curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007110 else
7111 {
7112 // global option set
7113 retval = option_set_callback_func(p_ffu, &ffu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01007114 // when using :set, free the local callback
7115 if (!(args->os_flags & OPT_GLOBAL))
7116 free_callback(&curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007117 }
7118
7119 if (retval == FAIL)
7120 return e_invalid_argument;
7121
7122 // If the option value starts with <SID> or s:, then replace that with
7123 // the script identifier.
7124 char_u **varp = (char_u **)args->os_varp;
7125 char_u *name = get_scriptlocal_funcname(*varp);
7126 if (name != NULL)
7127 {
7128 free_string_option(*varp);
7129 *varp = name;
7130 }
7131
7132 return NULL;
7133}
7134
7135# if defined(EXITFREE) || defined(PROTO)
7136 void
7137free_findfunc_option(void)
7138{
7139 free_callback(&ffu_cb);
7140}
7141# endif
7142
7143/*
7144 * Mark the global 'findfunc' callback with "copyID" so that it is not
7145 * garbage collected.
7146 */
7147 int
7148set_ref_in_findfunc(int copyID UNUSED)
7149{
7150 int abort = FALSE;
7151 abort = set_ref_in_callback(&ffu_cb, copyID);
7152 return abort;
7153}
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007154#endif
7155
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156/*
7157 * :sview [+command] file split window with new file, read-only
7158 * :split [[+command] file] split window with current or new file
7159 * :vsplit [[+command] file] split window vertically with current or new file
7160 * :new [[+command] file] split window with no or new file
7161 * :vnew [[+command] file] split vertically window with no or new file
7162 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007163 *
7164 * :tabedit open new Tab page with empty window
7165 * :tabedit [+command] file open new Tab page and edit "file"
7166 * :tabnew [[+command] file] just like :tabedit
7167 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 */
7169 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007170ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007172 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007174#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007175 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02007176 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007177#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02007178 int use_tab = eap->cmdidx == CMD_tabedit
7179 || eap->cmdidx == CMD_tabfind
7180 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007182 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007183 return;
7184
Bram Moolenaar4033c552017-09-16 20:54:51 +02007185#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar4033c552017-09-16 20:54:51 +02007189#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007190 // A ":split" in the quickfix window works like ":new". Don't want two
7191 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02007192 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
7194 if (eap->cmdidx == CMD_split)
7195 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 if (eap->cmdidx == CMD_vsplit)
7197 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007201 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007203 char_u *file_to_find = NULL;
7204 char *search_ctx = NULL;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007205
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007206 if (*get_findfunc() != NUL)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007207 {
7208#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007209 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007210 eap->addr_count > 0 ? eap->line2 : 1);
7211#endif
7212 }
7213 else
7214 {
7215 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7216 FNAME_MESS, TRUE, curbuf->b_ffname,
7217 &file_to_find, &search_ctx);
7218 vim_free(file_to_find);
7219 vim_findfile_cleanup(search_ctx);
7220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (fname == NULL)
7222 goto theend;
7223 eap->arg = fname;
7224 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007225# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007226 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 && eap->cmdidx != CMD_new)
7229 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007230 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007231# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007232 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007233# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007234 au_has_group((char_u *)"FileExplorer"))
7235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007236 // No browsing supported but we do have the file explorer:
7237 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007238 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007239 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007240 }
7241 else
7242 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007243 fname = do_browse(0, (char_u *)(use_tab
7244 ? _("Edit File in new tab page")
7245 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007247 if (fname == NULL)
7248 goto theend;
7249 eap->arg = fname;
7250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007252 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007255 /*
7256 * Either open new tab page or split the window.
7257 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007258 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007259 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007260 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007261 : eap->addr_count == 0 ? 0
7262 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007263 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007264 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007265
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007266 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007267 if (curwin != old_curwin
7268 && win_valid(old_curwin)
7269 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007270 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007271 old_curwin->w_alt_fnum = curbuf->b_fnum;
7272 }
7273 }
7274 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7276 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007277 // Reset 'scrollbind' when editing another file, but keep it when
7278 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007279 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007280 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 else
7282 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 do_exedit(eap, old_curwin);
7284 }
7285
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007286# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007287 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290theend:
7291 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007293
7294/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007295 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007296 */
7297 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007298tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007299{
7300 exarg_T ea;
7301
Bram Moolenaara80faa82020-04-12 19:37:17 +02007302 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007303 ea.cmdidx = CMD_tabnew;
7304 ea.cmd = (char_u *)"tabn";
7305 ea.arg = (char_u *)"";
7306 ex_splitview(&ea);
7307}
7308
7309/*
7310 * :tabnext command
7311 */
7312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007313ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007314{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007315 int tab_number;
7316
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007317 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007318 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007319 switch (eap->cmdidx)
7320 {
7321 case CMD_tabfirst:
7322 case CMD_tabrewind:
7323 goto_tabpage(1);
7324 break;
7325 case CMD_tablast:
7326 goto_tabpage(9999);
7327 break;
7328 case CMD_tabprevious:
7329 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007330 if (eap->arg && *eap->arg != NUL)
7331 {
7332 char_u *p = eap->arg;
7333 char_u *p_save = p;
7334
7335 tab_number = getdigits(&p);
7336 if (p == p_save || *p_save == '-' || *p != NUL
7337 || tab_number == 0)
7338 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007339 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007340 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007341 return;
7342 }
7343 }
7344 else
7345 {
7346 if (eap->addr_count == 0)
7347 tab_number = 1;
7348 else
7349 {
7350 tab_number = eap->line2;
7351 if (tab_number < 1)
7352 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007353 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007354 return;
7355 }
7356 }
7357 }
7358 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007359 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007360 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007361 tab_number = get_tabpage_arg(eap);
7362 if (eap->errmsg == NULL)
7363 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007364 break;
7365 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007366}
7367
7368/*
7369 * :tabmove command
7370 */
7371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007372ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007373{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007374 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007375
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007376 tab_number = get_tabpage_arg(eap);
7377 if (eap->errmsg == NULL)
7378 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007379}
7380
7381/*
7382 * :tabs command: List tabs and their contents.
7383 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007385ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007386{
7387 tabpage_T *tp;
7388 win_T *wp;
7389 int tabcount = 1;
7390
7391 msg_start();
7392 msg_scroll = TRUE;
7393 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7394 {
7395 msg_putchar('\n');
7396 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007397 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007398 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007399 ui_breakcheck();
7400
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007401 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007402 wp = firstwin;
7403 else
7404 wp = tp->tp_firstwin;
7405 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7406 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007407 msg_putchar('\n');
7408 msg_putchar(wp == curwin ? '>' : ' ');
7409 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007410 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7411 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007412 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007413 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007414 else
7415 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7416 IObuff, IOSIZE, TRUE);
7417 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007418 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007419 ui_breakcheck();
7420 }
7421 }
7422}
7423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424/*
7425 * ":mode": Set screen mode.
7426 * If no argument given, just get the screen size and redraw.
7427 */
7428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007429ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
7431 if (*eap->arg == NUL)
7432 shell_resized();
7433 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007434 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435}
7436
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437/*
7438 * ":resize".
7439 * set, increment or decrement current window height
7440 */
7441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007442ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
7444 int n;
7445 win_T *wp = curwin;
7446
7447 if (eap->addr_count > 0)
7448 {
7449 n = eap->line2;
7450 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7451 ;
7452 }
7453
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007454# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007458 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 {
7460 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007461 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007462 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007464 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 }
7466 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
7468 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007469 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007470 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007472 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
7474}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476/*
7477 * ":find [+command] <file>" command.
7478 */
7479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007480ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
Colin Kennedy21570352024-03-03 16:16:47 +01007482 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007483 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007484
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007485 char_u *fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007487 char_u *file_to_find = NULL;
7488 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007490 if (*get_findfunc() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 {
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007492#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007493 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007494 eap->addr_count > 0 ? eap->line2 : 1);
7495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 }
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007497 else
7498 {
7499 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7500 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
7501 if (eap->addr_count > 0)
7502 {
7503 // Repeat finding the file "count" times. This matters when it appears
7504 // several times in the path.
7505 count = eap->line2;
7506 while (fname != NULL && --count > 0)
7507 {
7508 vim_free(fname);
7509 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7510 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
7511 }
7512 }
7513 VIM_CLEAR(file_to_find);
7514 vim_findfile_cleanup(search_ctx);
7515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007517 if (fname == NULL)
7518 return;
7519
7520 eap->arg = fname;
7521 do_exedit(eap, NULL);
7522 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523}
7524
7525/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007526 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007527 */
7528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007529ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007530{
7531 regmatch_T regmatch;
7532 char_u *p;
7533
Bram Moolenaar65088802021-03-13 21:07:21 +01007534#ifdef FEAT_EVAL
7535 if (not_in_vim9(eap) == FAIL)
7536 return;
7537#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007538 curwin->w_cursor.lnum = eap->line2;
7539 beginline(BL_SOL | BL_FIX);
7540 if (*eap->arg == '/')
7541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007542 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007543 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007544 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007545 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007546 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007547 if (regmatch.regprog != NULL)
7548 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007549 // make a copy of the line, when searching for a mark it might be
7550 // flushed
7551 char_u *line = vim_strsave(ml_get_curline());
7552
Bram Moolenaardf177f62005-02-22 08:39:57 +00007553 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007554 if (vim_regexec(&regmatch, line, (colnr_T)0))
7555 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007556 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007557 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007558 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007559 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007560 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007561 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007562 eap->arg += STRLEN(eap->arg);
7563 }
7564 check_cursor();
7565
7566 eap->cmdidx = CMD_visual;
7567 do_exedit(eap, NULL);
7568}
7569
7570/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007571 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 */
7573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007574ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007576 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7577
Colin Kennedy21570352024-03-03 16:16:47 +01007578 // Exclude commands which keep the window's current buffer
7579 if (
7580 eap->cmdidx != CMD_badd
7581 && eap->cmdidx != CMD_balt
7582 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007583 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7584 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007585 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007586
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 do_exedit(eap, NULL);
7588}
7589
7590/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007591 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007594do_exedit(
7595 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007596 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007600 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007602 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7603 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007604 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 /*
7606 * ":vi" command ends Ex mode.
7607 */
7608 if (exmode_active && (eap->cmdidx == CMD_visual
7609 || eap->cmdidx == CMD_view))
7610 {
7611 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007612 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007614 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007615 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007616 if (global_busy)
7617 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007618 if (eap->nextcmd != NULL)
7619 {
7620 stuffReadbuff(eap->nextcmd);
7621 eap->nextcmd = NULL;
7622 }
7623
7624 if (exmode_was != EXMODE_VIM)
7625 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007626 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007627 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007628 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007629 no_wait_return = 0;
7630 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007631 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007632 msg_scroll = 0;
7633#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007634 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007635 hold_gui_events = 0;
7636#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007637 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007638 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007639
7640 main_loop(FALSE, TRUE);
7641
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007642 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007643 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007644 no_wait_return = save_nwr;
7645 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007646#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007647 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007648#endif
7649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 }
7653
7654 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007655 || eap->cmdidx == CMD_tabnew
7656 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007657 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007659 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 setpcmark();
7661 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007662 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7663 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007665 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 || *eap->arg != NUL
7667#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007668 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669#endif
7670 )
7671 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007672 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7673 // Only ":edit" or ":script" can bring us here, others are stopped
7674 // earlier.
7675 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007676 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007677
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 n = readonlymode;
7679 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7680 readonlymode = TRUE;
7681 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007682 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7683 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007684 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7685 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7687 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007688 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7690 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7691 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007692 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007694 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007695 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007696 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7697 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007698 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007700 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (old_curwin != NULL)
7702 {
7703 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007704 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007706#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007707 cleanup_T cs;
7708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007709 // Reset the error/interrupt/exception state here so that
7710 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007711 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007712#endif
7713#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007715#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007716 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007717
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007718#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007719 // Restore the error/interrupt/exception state if not
7720 // discarded by a new aborting error, interrupt, or
7721 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007722 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 }
7725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
7727 else if (readonlymode && curbuf->b_nwindows == 1)
7728 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007729 // When editing an already visited buffer, 'readonly' won't be set
7730 // but the previous value is kept. With ":view" and ":sview" we
7731 // want the file to be readonly, except when another window is
7732 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 curbuf->b_p_ro = TRUE;
7734 }
7735 readonlymode = n;
7736 }
7737 else
7738 {
7739 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007740 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 if (n != curwin->w_arg_idx_invalid)
7744 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 }
7746
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 /*
7748 * if ":split file" worked, set alternate file name in old window to new
7749 * file
7750 */
7751 if (old_curwin != NULL
7752 && *eap->arg != NUL
7753 && curwin != old_curwin
7754 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007755 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007756 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
7759 ex_no_reprint = TRUE;
7760}
7761
7762#ifndef FEAT_GUI
7763/*
7764 * ":gui" and ":gvim" when there is no GUI.
7765 */
7766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007767ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007769 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770}
7771#endif
7772
Bram Moolenaar4f974752019-02-17 17:44:42 +01007773#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007775ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 gui_make_tearoff(eap->arg);
7778}
7779#endif
7780
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007781#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7782 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007786# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7787 if (gui.in_use)
7788 gui_make_popup(eap->arg, eap->forceit);
7789# ifdef FEAT_TERM_POPUP_MENU
7790 else
7791# endif
7792# endif
7793# ifdef FEAT_TERM_POPUP_MENU
7794 pum_make_popup(eap->arg, eap->forceit);
7795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797#endif
7798
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007800ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007803 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007805 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806}
7807
7808/*
7809 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7810 * offset.
7811 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007814ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007817 win_T *save_curwin = curwin;
7818 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 long topline;
7820 long y;
7821 linenr_T old_linenr = curwin->w_cursor.lnum;
7822
7823 setpcmark();
7824
7825 /*
7826 * determine max topline
7827 */
7828 if (curwin->w_p_scb)
7829 {
7830 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007831 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {
7833 if (wp->w_p_scb && wp->w_buffer)
7834 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007835 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (topline > y)
7837 topline = y;
7838 }
7839 }
7840 if (topline < 1)
7841 topline = 1;
7842 }
7843 else
7844 {
7845 topline = 1;
7846 }
7847
7848
7849 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007850 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007852 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
7854 if (curwin->w_p_scb)
7855 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007856 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 y = topline - curwin->w_topline;
7858 if (y > 0)
7859 scrollup(y, TRUE);
7860 else
7861 scrolldown(-y, TRUE);
7862 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007863 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 }
7867 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007868 curwin = save_curwin;
7869 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 if (curwin->w_p_scb)
7871 {
7872 did_syncbind = TRUE;
7873 checkpcmark();
7874 if (old_linenr != curwin->w_cursor.lnum)
7875 {
7876 char_u ctrl_o[2];
7877
7878 ctrl_o[0] = Ctrl_O;
7879 ctrl_o[1] = 0;
7880 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7881 }
7882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883}
7884
7885
7886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007887ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007889 int i;
7890 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7891 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007893 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007895 do_bang(1, eap, FALSE, FALSE, TRUE);
7896 return;
7897 }
7898
7899 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7900 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901
7902#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007903 if (cmdmod.cmod_flags & CMOD_BROWSE)
7904 {
7905 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007907 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7908 NULL, NULL, NULL, curbuf);
7909 if (browseFile != NULL)
7910 {
7911 i = readfile(browseFile, NULL,
7912 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7913 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 }
7915 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007916 i = OK;
7917 }
7918 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007920 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007922 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 return;
7924 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007925 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927 else
7928 {
7929 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7930 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7931 i = readfile(eap->arg, NULL,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007932 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933
7934 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007935 if (i != OK)
7936 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007937#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007938 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007940 semsg(_(e_cant_open_file_str), eap->arg);
7941 }
7942 else
7943 {
7944 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007945 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007946 // Delete the empty line that remains. Historically ex does
7947 // this but vi doesn't.
7948 if (eap->line2 == 0)
7949 lnum = curbuf->b_ml.ml_line_count;
7950 else
7951 lnum = 1;
7952 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007953 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007954 ml_delete(lnum);
7955 if (curwin->w_cursor.lnum > 1
7956 && curwin->w_cursor.lnum >= lnum)
7957 --curwin->w_cursor.lnum;
7958 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007959 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007960 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007961 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
7963}
7964
Bram Moolenaarea408852005-06-25 22:49:46 +00007965static char_u *prev_dir = NULL;
7966
7967#if defined(EXITFREE) || defined(PROTO)
7968 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007969free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007970{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007971 VIM_CLEAR(prev_dir);
7972 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007973}
7974#endif
7975
Bram Moolenaarf4258302013-06-02 18:20:17 +02007976/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007977 * Get the previous directory for the given chdir scope.
7978 */
7979 static char_u *
7980get_prevdir(cdscope_T scope)
7981{
7982 if (scope == CDSCOPE_WINDOW)
7983 return curwin->w_prevdir;
7984 else if (scope == CDSCOPE_TABPAGE)
7985 return curtab->tp_prevdir;
7986 return prev_dir;
7987}
7988
7989/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007990 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007991 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7992 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007993 */
7994 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007995post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007996{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007997 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007998 // Clear tab local directory for both :cd and :tcd
7999 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008000 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008001 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02008002 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008003 char_u *pdir = get_prevdir(scope);
8004
8005 // If still in the global directory, need to remember current
8006 // directory as the global directory.
8007 if (globaldir == NULL && pdir != NULL)
8008 globaldir = vim_strsave(pdir);
8009
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008010 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02008011 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008012 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008013 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008014 curtab->tp_localdir = vim_strsave(NameBuff);
8015 else
8016 curwin->w_localdir = vim_strsave(NameBuff);
8017 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02008018 }
8019 else
8020 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008021 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01008022 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008023 }
8024
zeertzjq64be6aa2021-11-19 11:59:08 +00008025 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008026 shorten_fnames(TRUE);
8027}
8028
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008029/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008030 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
8031 */
8032 void
8033trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
8034{
8035#ifdef FEAT_EVAL
8036 dict_T *v_event;
8037 save_v_event_T save_v_event;
8038
8039 v_event = get_v_event(&save_v_event);
8040 (void)dict_add_string(v_event, "directory", new_dir);
8041 dict_set_items_ro(v_event);
8042#endif
8043 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
8044#ifdef FEAT_EVAL
8045 restore_v_event(v_event, &save_v_event);
8046#endif
8047}
8048
8049/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008050 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02008051 * chdir() function.
8052 * scope == CDSCOPE_WINDOW: changes the window-local directory
8053 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
8054 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008055 * Returns TRUE if the directory is successfully changed.
8056 */
8057 int
8058changedir_func(
8059 char_u *new_dir,
8060 int forceit,
8061 cdscope_T scope)
8062{
Bram Moolenaar002bc792020-06-05 22:33:42 +02008063 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008064 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008065 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00008066 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008067 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008068
Bram Moolenaar7cc96922020-01-31 22:41:38 +01008069 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008070 return FALSE;
8071
8072 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
8073 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00008074 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008075 return FALSE;
8076 }
8077
8078 // ":cd -": Change to previous directory
8079 if (STRCMP(new_dir, "-") == 0)
8080 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008081 pdir = get_prevdir(scope);
8082 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008083 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008084 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008085 return FALSE;
8086 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02008087 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008088 }
8089
8090 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008091 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02008092 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008093 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02008094 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008095
Bakudankun29f3a452021-12-11 12:28:08 +00008096 // For UNIX ":cd" means: go to home directory.
8097 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008098#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008099 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00008100#else
8101 if (*new_dir == NUL && p_cdh)
8102#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008103 {
8104 // use NameBuff for home directory name
8105# ifdef VMS
8106 char_u *p;
8107
8108 p = mch_getenv((char_u *)"SYS$LOGIN");
8109 if (p == NULL || *p == NUL) // empty is the same as not set
8110 NameBuff[0] = NUL;
8111 else
8112 vim_strncpy(NameBuff, p, MAXPATHL - 1);
8113# else
8114 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8115# endif
8116 new_dir = NameBuff;
8117 }
zeertzjqf38aad82021-12-30 13:45:57 +00008118 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008119 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
8120 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00008121 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008122 if (scope == CDSCOPE_WINDOW)
8123 acmd_fname = (char_u *)"window";
8124 else if (scope == CDSCOPE_TABPAGE)
8125 acmd_fname = (char_u *)"tabpage";
8126 else
8127 acmd_fname = (char_u *)"global";
8128 trigger_DirChangedPre(acmd_fname, new_dir);
8129
8130 if (vim_chdir(new_dir))
8131 {
8132 emsg(_(e_command_failed));
8133 vim_free(pdir);
8134 return FALSE;
8135 }
Richard Doty3d0abad2021-12-29 14:39:08 +00008136 }
zeertzjq73257142022-02-02 13:16:37 +00008137
8138 if (scope == CDSCOPE_WINDOW)
8139 pp = &curwin->w_prevdir;
8140 else if (scope == CDSCOPE_TABPAGE)
8141 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008142 else
zeertzjq73257142022-02-02 13:16:37 +00008143 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008144 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00008145 *pp = pdir;
8146
8147 post_chdir(scope);
8148
8149 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008150 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008151 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00008152 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008153}
Bram Moolenaarea408852005-06-25 22:49:46 +00008154
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008156 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008158 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008159ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01008161 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162
8163 new_dir = eap->arg;
8164#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00008165 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
8166 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008168 ex_pwd(NULL);
8169 return;
8170 }
8171#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008172
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008173 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008174
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008175 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8176 scope = CDSCOPE_WINDOW;
8177 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
8178 scope = CDSCOPE_TABPAGE;
8179
8180 if (changedir_func(new_dir, eap->forceit, scope))
8181 {
8182 // Echo the new current directory if the command was typed.
8183 if (KeyTyped || p_verbose >= 5)
8184 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 }
8186}
8187
8188/*
8189 * ":pwd".
8190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008192ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193{
8194 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8195 {
8196#ifdef BACKSLASH_IN_FILENAME
8197 slash_adjust(NameBuff);
8198#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02008199 if (p_verbose > 0)
8200 {
8201 char *context = "global";
8202
Bram Moolenaar05268152021-11-18 18:53:45 +00008203 if (last_chdir_reason != NULL)
8204 context = last_chdir_reason;
8205 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02008206 context = "window";
8207 else if (curtab->tp_localdir != NULL)
8208 context = "tabpage";
8209 smsg("[%s] %s", context, (char *)NameBuff);
8210 }
8211 else
8212 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 }
8214 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00008215 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216}
8217
8218/*
8219 * ":=".
8220 */
8221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008222ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008224 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008225 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226}
8227
8228 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008229ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008231 int n;
8232 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
8234 if (cursor_valid())
8235 {
8236 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8237 if (n >= 0)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02008238 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008240
8241 len = eap->line2;
8242 switch (*eap->arg)
8243 {
8244 case 'm': break;
8245 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008246 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008247 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008248
8249 // Hide the cursor if invoked with !
8250 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251}
8252
8253/*
8254 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008255 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 */
8257 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008258do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259{
Bram Moolenaar52953192019-08-16 10:27:13 +02008260 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008261 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008262# ifdef ELAPSED_FUNC
8263 elapsed_T start_tv;
8264
8265 // Remember at what time we started, so that we know how much longer we
8266 // should wait after waiting for a bit.
8267 ELAPSED_INIT(start_tv);
8268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008270 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008271 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008272 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008273 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008274
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008275 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008276 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008278 wait_now = msec - done > 1000L ? 1000L : msec - done;
8279#ifdef FEAT_TIMERS
8280 {
8281 long due_time = check_due_timer();
8282
8283 if (due_time > 0 && due_time < wait_now)
8284 wait_now = due_time;
8285 }
8286#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008287#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008288 if (has_any_channel() && wait_now > 20L)
8289 wait_now = 20L;
8290#endif
8291#ifdef FEAT_SOUND
8292 if (has_any_sound_callback() && wait_now > 20L)
8293 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008294#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008295 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008296
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008297#ifdef FEAT_JOB_CHANNEL
8298 if (has_any_channel())
8299 ui_breakcheck_force(TRUE);
8300 else
8301#endif
8302 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008303#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008304 // Process the netbeans and clientserver messages that may have been
8305 // received in the call to ui_breakcheck() when the GUI is in use. This
8306 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008307 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008308#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008309
8310# ifdef ELAPSED_FUNC
8311 // actual time passed
8312 done = ELAPSED_FUNC(start_tv);
8313# else
8314 // guestimate time passed (will actually be more)
8315 done += wait_now;
8316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008318
8319 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8320 // input buffer, otherwise a following call to input() fails.
8321 if (got_int)
8322 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008323
8324 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008325 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326}
8327
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328/*
8329 * ":winsize" command (obsolete).
8330 */
8331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008332ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
8334 int w, h;
8335 char_u *arg = eap->arg;
8336 char_u *p;
8337
Keith Thompson184f71c2024-01-04 21:19:04 +01008338 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008339 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008340 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008341 return;
8342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 w = getdigits(&arg);
8344 arg = skipwhite(arg);
8345 p = arg;
8346 h = getdigits(&arg);
8347 if (*p != NUL && *arg == NUL)
8348 set_shellsize(w, h, TRUE);
8349 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008350 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351}
8352
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008354ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355{
8356 int xchar = NUL;
8357 char_u *p;
8358
8359 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 if (eap->arg[1] == NUL)
8363 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008364 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 return;
8366 }
8367 xchar = eap->arg[1];
8368 p = eap->arg + 2;
8369 }
8370 else
8371 p = eap->arg + 1;
8372
Bram Moolenaar63b91732021-08-05 20:40:03 +02008373 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008375 if (*p != NUL && *p != (
8376#ifdef FEAT_EVAL
8377 in_vim9script() ? '#' :
8378#endif
8379 '"')
8380 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008381 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008382 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008384 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008385 postponed_split_flags = cmdmod.cmod_split;
8386 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8388 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008389 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 }
8391}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392
Bram Moolenaar843ee412004-06-30 16:16:41 +00008393#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394/*
8395 * ":winpos".
8396 */
8397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008398ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
8400 int x, y;
8401 char_u *arg = eap->arg;
8402 char_u *p;
8403
8404 if (*arg == NUL)
8405 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008406# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008407# ifdef VIMDLL
8408 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8409 mch_get_winpos(&x, &y) != FAIL)
8410# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008412# else
8413 if (mch_get_winpos(&x, &y) != FAIL)
8414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {
8416 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008417 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 }
8419 else
8420# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008421 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 }
8423 else
8424 {
8425 x = getdigits(&arg);
8426 arg = skipwhite(arg);
8427 p = arg;
8428 y = getdigits(&arg);
8429 if (*p == NUL || *arg != NUL)
8430 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008431 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 return;
8433 }
8434# ifdef FEAT_GUI
8435 if (gui.in_use)
8436 gui_mch_set_winpos(x, y);
8437 else if (gui.starting)
8438 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008439 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 gui_win_x = x;
8441 gui_win_y = y;
8442 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008443# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 else
8445# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008446# endif
8447# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008448 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449# endif
8450# ifdef HAVE_TGETENT
8451 if (*T_CWP)
8452 term_set_winpos(x, y);
8453# endif
8454 }
8455}
8456#endif
8457
8458/*
8459 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8460 */
8461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008462ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
8464 oparg_T oa;
8465
8466 clear_oparg(&oa);
8467 oa.regname = eap->regname;
8468 oa.start.lnum = eap->line1;
8469 oa.end.lnum = eap->line2;
8470 oa.line_count = eap->line2 - eap->line1 + 1;
8471 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008473 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 {
8475 setpcmark();
8476 curwin->w_cursor.lnum = eap->line1;
8477 beginline(BL_SOL | BL_FIX);
8478 }
8479
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008480 if (VIsual_active)
8481 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 switch (eap->cmdidx)
8484 {
8485 case CMD_delete:
8486 oa.op_type = OP_DELETE;
8487 op_delete(&oa);
8488 break;
8489
8490 case CMD_yank:
8491 oa.op_type = OP_YANK;
8492 (void)op_yank(&oa, FALSE, TRUE);
8493 break;
8494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008496 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008498 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8499#else
8500 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008502 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 oa.op_type = OP_RSHIFT;
8504 else
8505 oa.op_type = OP_LSHIFT;
8506 op_shift(&oa, FALSE, eap->amount);
8507 break;
8508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008510 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511}
8512
8513/*
8514 * ":put".
8515 */
8516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008517ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008519 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 if (eap->line2 == 0)
8521 {
8522 eap->line2 = 1;
8523 eap->forceit = TRUE;
8524 }
8525 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008526 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008527 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008528 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529}
8530
8531/*
64-bitmane08f10a2025-03-18 22:14:34 +01008532 * ":iput".
8533 */
8534 static void
8535ex_iput(exarg_T *eap)
8536{
8537 // ":0iput" works like ":1iput!".
8538 if (eap->line2 == 0)
8539 {
8540 eap->line2 = 1;
8541 eap->forceit = TRUE;
8542 }
8543 curwin->w_cursor.lnum = eap->line2;
8544 check_cursor_col();
8545 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
8546 PUT_LINE|PUT_CURSLINE
8547 |PUT_FIXINDENT);
8548}
8549
8550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 * Handle ":copy" and ":move".
8552 */
8553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008554ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 long n;
8557
Bram Moolenaar491799b2020-08-01 19:23:43 +02008558#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008559 if (not_in_vim9(eap) == FAIL)
8560 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008561#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008562 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008563 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
8565 eap->nextcmd = NULL;
8566 return;
8567 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008568 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 /*
8571 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8572 */
8573 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8574 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008575 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 return;
8577 }
8578
8579 if (eap->cmdidx == CMD_move)
8580 {
8581 if (do_move(eap->line1, eap->line2, n) == FAIL)
8582 return;
8583 }
8584 else
8585 ex_copy(eap->line1, eap->line2, n);
8586 u_clearline();
8587 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008588 ex_may_print(eap);
8589}
8590
8591/*
8592 * Print the current line if flags were given to the Ex command.
8593 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008594 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008595ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008596{
8597 if (eap->flags != 0)
8598 {
8599 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8600 (eap->flags & EXFLAG_LIST));
8601 ex_no_reprint = TRUE;
8602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603}
8604
8605/*
8606 * ":smagic" and ":snomagic".
8607 */
8608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008609ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008611 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008613 magic_overruled = eap->cmdidx == CMD_smagic
8614 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008615 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008616 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617}
8618
8619/*
8620 * ":join".
8621 */
8622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008623ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624{
8625 curwin->w_cursor.lnum = eap->line1;
8626 if (eap->line1 == eap->line2)
8627 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008628 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 return;
8630 if (eap->line2 == curbuf->b_ml.ml_line_count)
8631 {
8632 beep_flush();
8633 return;
8634 }
8635 ++eap->line2;
8636 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008637 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008639 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640}
8641
8642/*
8643 * ":[addr]@r" or ":[addr]*r": execute register
8644 */
8645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008646ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008649 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
8651 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008652 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008655 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656#endif
8657
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008658 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 c = *eap->arg;
8660 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8661 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008662 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008663 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8664 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008667 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008670 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008672 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008674 /*
8675 * Execute from the typeahead buffer.
8676 * Continue until the stuff buffer is empty and all added characters
8677 * have been consumed.
8678 */
8679 while (!stuff_empty() || typebuf.tb_len > prev_len)
8680 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8681
8682 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683}
8684
8685/*
8686 * ":!".
8687 */
8688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008689ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
8691 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8692}
8693
8694/*
8695 * ":undo".
8696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008698ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008700 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008701 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008702 else
8703 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008706#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008708ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008709{
8710 char_u hash[UNDO_HASH_SIZE];
8711
8712 u_compute_hash(hash);
8713 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8714}
8715
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008717ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008718{
8719 char_u hash[UNDO_HASH_SIZE];
8720
8721 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008722 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008723}
8724#endif
8725
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726/*
8727 * ":redo".
8728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008730ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731{
8732 u_redo(1);
8733}
8734
8735/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008736 * ":earlier" and ":later".
8737 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008739ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008740{
8741 long count = 0;
8742 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008743 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008744 char_u *p = eap->arg;
8745
8746 if (*p == NUL)
8747 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008748 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008749 {
8750 count = getdigits(&p);
8751 switch (*p)
8752 {
8753 case 's': ++p; sec = TRUE; break;
8754 case 'm': ++p; sec = TRUE; count *= 60; break;
8755 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008756 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8757 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008758 }
8759 }
8760
8761 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008762 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008763 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008764 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8765 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008766}
8767
8768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 * ":redir": start/stop redirection.
8770 */
8771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008772ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 char *mode;
8775 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008776 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777
Bram Moolenaarba768492016-07-08 15:32:54 +02008778#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008779 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008780 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008781 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008782 return;
8783 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008784#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008785
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 if (STRICMP(eap->arg, "END") == 0)
8787 close_redir();
8788 else
8789 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008790 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008792 ++arg;
8793 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008795 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 mode = "a";
8797 }
8798 else
8799 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008800 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
8802 close_redir();
8803
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008804 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008805 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 if (fname == NULL)
8807 return;
8808#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008809 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {
8811 char_u *browseFile;
8812
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008813 browseFile = do_browse(BROWSE_SAVE,
8814 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008815 fname, NULL, NULL,
8816 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008818 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 vim_free(fname);
8820 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008821 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 }
8823#endif
8824
8825 redir_fd = open_exfile(fname, eap->forceit, mode);
8826 vim_free(fname);
8827 }
8828#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008829 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008831 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008833 ++arg;
8834 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008836 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008837 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008839 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008841 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008842 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008843 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008846 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008847 if (*arg == '>')
8848 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008849 // Make register empty when not using @A-@Z and the
8850 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008851 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008852 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008854 }
8855 if (*arg != NUL)
8856 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008857 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008858 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008859 }
8860 }
8861 else if (*arg == '=' && arg[1] == '>')
8862 {
8863 int append;
8864
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008865 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008866 close_redir();
8867 arg += 2;
8868
8869 if (*arg == '>')
8870 {
8871 ++arg;
8872 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 }
8874 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008875 append = FALSE;
8876
8877 if (var_redir_start(skipwhite(arg), append) == OK)
8878 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 }
8880#endif
8881
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008882 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008885 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008888 // Make sure redirection is not off. Can happen for cmdline completion
8889 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008890 if (redir_fd != NULL
8891#ifdef FEAT_EVAL
8892 || redir_reg || redir_vname
8893#endif
8894 )
8895 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896}
8897
8898/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008899 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008901 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008902ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008904 redraw_cmd(eap->forceit);
8905}
8906
8907/*
8908 * ":redraw": force redraw, with clear if "clear" is TRUE.
8909 */
8910 void
8911redraw_cmd(int clear)
8912{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008913 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008915
8916 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008918
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008919 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008921 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 if (need_maketitle)
8923 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008924#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8925# ifdef VIMDLL
8926 if (!gui.in_use)
8927# endif
8928 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008929#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008930 RedrawingDisabled = save_RedrawingDisabled;
8931 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932
Bram Moolenaar5017c662022-04-07 18:06:08 +01008933 // After drawing the statusline screen_attr may still be set.
8934 screen_stop_highlight();
8935
Bram Moolenaara2a89732022-08-31 14:46:18 +01008936 // Reset msg_didout, so that a message that's there is overwritten.
8937 msg_didout = FALSE;
8938 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008940 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008941 need_wait_return = FALSE;
8942
Bram Moolenaara653e532022-04-19 11:38:24 +01008943 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008944 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008945 redrawcmdline();
8946
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 out_flush();
8948}
8949
8950/*
8951 * ":redrawstatus": force redraw of status line(s)
8952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008954ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 if (eap->forceit)
8957 status_redraw_all();
8958 else
8959 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008960 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008961 return; // redraw later
8962
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008963 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008964 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008965
8966 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008967 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008968
zeertzjq320d9102022-09-20 17:12:13 +01008969 if (State & MODE_CMDLINE)
8970 redraw_statuslines();
8971 else
8972 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008973 RedrawingDisabled = save_RedrawingDisabled;
8974 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
8977
Bram Moolenaare12bab32019-01-08 22:02:56 +01008978/*
8979 * ":redrawtabline": force redraw of the tabline
8980 */
8981 static void
8982ex_redrawtabline(exarg_T *eap UNUSED)
8983{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008984 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008985 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008986
8987 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008988 p_lz = FALSE;
8989
8990 draw_tabline();
8991
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008992 RedrawingDisabled = save_RedrawingDisabled;
8993 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008994 out_flush();
8995}
8996
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02008997/*
8998 * ":redrawtabpanel": force redraw of the tabpanel
8999 */
9000 static void
9001ex_redrawtabpanel(exarg_T *eap UNUSED)
9002{
9003 int save_RedrawingDisabled = RedrawingDisabled;
9004 RedrawingDisabled = 0;
9005
9006 int save_p_lz = p_lz;
9007 p_lz = FALSE;
9008
9009#if defined(FEAT_TABPANEL)
9010 draw_tabpanel();
9011#else
9012 emsg(_(e_cannot_not_support_redrawtabpanel));
9013#endif
9014
9015 RedrawingDisabled = save_RedrawingDisabled;
9016 p_lz = save_p_lz;
9017 out_flush();
9018}
9019
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009021close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023 if (redir_fd != NULL)
9024 {
9025 fclose(redir_fd);
9026 redir_fd = NULL;
9027 }
9028#ifdef FEAT_EVAL
9029 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009030 if (redir_vname)
9031 {
9032 var_redir_stop();
9033 redir_vname = 0;
9034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035#endif
9036}
9037
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02009038#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009039 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02009040vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009041{
9042 if (vim_mkdir(name, prot) != 0)
9043 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00009044 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009045 return FAIL;
9046 }
9047 return OK;
9048}
9049#endif
9050
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051/*
9052 * Open a file for writing for an Ex command, with some checks.
9053 * Return file descriptor, or NULL on failure.
9054 */
9055 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009056open_exfile(
9057 char_u *fname,
9058 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009059 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
9061 FILE *fd;
9062
9063#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009064 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 if (mch_isdir(fname))
9066 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01009067 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 return NULL;
9069 }
9070#endif
9071 if (!forceit && *mode != 'a' && vim_fexists(fname))
9072 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00009073 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 return NULL;
9075 }
9076
9077 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00009078 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
9080 return fd;
9081}
9082
9083/*
9084 * ":mark" and ":k".
9085 */
9086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009087ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
9089 pos_T pos;
9090
Bram Moolenaar10b94212021-02-19 21:42:57 +01009091#ifdef FEAT_EVAL
9092 if (not_in_vim9(eap) == FAIL)
9093 return;
9094#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009095 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009097 emsg(_(e_argument_required));
9098 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009100
9101 if (eap->arg[1] != NUL) // more than one character?
9102 {
9103 semsg(_(e_trailing_characters_str), eap->arg);
9104 return;
9105 }
9106
9107 pos = curwin->w_cursor; // save curwin->w_cursor
9108 curwin->w_cursor.lnum = eap->line2;
9109 beginline(BL_WHITE | BL_FIX);
9110 if (setmark(*eap->arg) == FAIL) // set mark
9111 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
9112 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113}
9114
9115/*
9116 * Update w_topline, w_leftcol and the cursor position.
9117 */
9118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009119update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009121 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 update_topline();
9123 if (!curwin->w_p_wrap)
9124 validate_cursor();
9125 update_curswant();
9126}
9127
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009129 * Save the current State and go to Normal mode.
9130 * Return TRUE if the typeahead could be saved.
9131 */
9132 int
9133save_current_state(save_state_T *sst)
9134{
9135 sst->save_msg_scroll = msg_scroll;
9136 sst->save_restart_edit = restart_edit;
9137 sst->save_msg_didout = msg_didout;
9138 sst->save_State = State;
9139 sst->save_insertmode = p_im;
9140 sst->save_finish_op = finish_op;
9141 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009142 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009143 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009144
Bram Moolenaar4324d872020-12-01 20:12:24 +01009145 msg_scroll = FALSE; // no msg scrolling in Normal mode
9146 restart_edit = 0; // don't go to Insert mode
9147 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01009148
Bram Moolenaara452b802020-12-01 21:47:59 +01009149 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01009150 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009151
9152 /*
9153 * Save the current typeahead. This is required to allow using ":normal"
9154 * from an event handler and makes sure we don't hang when the argument
9155 * ends with half a command.
9156 */
9157 save_typeahead(&sst->tabuf);
9158 return sst->tabuf.typebuf_valid;
9159}
9160
9161 void
9162restore_current_state(save_state_T *sst)
9163{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009164 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02009165 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009166
9167 msg_scroll = sst->save_msg_scroll;
9168 restart_edit = sst->save_restart_edit;
9169 p_im = sst->save_insertmode;
9170 finish_op = sst->save_finish_op;
9171 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009172 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009173 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009174 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01009175 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009176
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009177 // Restore the state (needed when called from a function executed for
9178 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009179 State = sst->save_State;
9180#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009181 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009182#endif
9183}
9184
9185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 * ":normal[!] {commands}": Execute normal mode commands.
9187 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009189ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009191 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 char_u *arg = NULL;
9193 int l;
9194 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009196 if (ex_normal_lock > 0)
9197 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00009198 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009199 return;
9200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 if (ex_normal_busy >= p_mmd)
9202 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009203 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 return;
9205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 /*
9208 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9209 * this for the K_SPECIAL leading byte, otherwise special keys will not
9210 * work.
9211 */
9212 if (has_mbyte)
9213 {
9214 int len = 0;
9215
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009216 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 for (p = eap->arg; *p != NUL; ++p)
9218 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009219#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009220 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01009222#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009223 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009224 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009225#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 )
9229 len += 2;
9230 }
9231 if (len > 0)
9232 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009233 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 if (arg != NULL)
9235 {
9236 len = 0;
9237 for (p = eap->arg; *p != NUL; ++p)
9238 {
9239 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01009240#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 if (*p == CSI)
9242 {
9243 arg[len++] = KS_EXTRA;
9244 arg[len++] = (int)KE_CSI;
9245 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009246#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009247 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 {
9249 arg[len++] = *++p;
9250 if (*p == K_SPECIAL)
9251 {
9252 arg[len++] = KS_SPECIAL;
9253 arg[len++] = KE_FILLER;
9254 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009255#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 else if (*p == CSI)
9257 {
9258 arg[len++] = KS_EXTRA;
9259 arg[len++] = (int)KE_CSI;
9260 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 }
9263 arg[len] = NUL;
9264 }
9265 }
9266 }
9267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009269 ++ex_normal_busy;
9270 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 {
9272 /*
9273 * Repeat the :normal command for each line in the range. When no
9274 * range given, execute it just once, without positioning the cursor
9275 * first.
9276 */
9277 do
9278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 if (eap->addr_count != 0)
9280 {
9281 curwin->w_cursor.lnum = eap->line1++;
9282 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009283 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 }
9285
Bram Moolenaar13505972019-01-24 15:04:48 +01009286 exec_normal_cmd(arg != NULL
9287 ? arg
9288 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 }
9290 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9291 }
9292
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009293 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 update_topline_cursor();
9295
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009296 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009298 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009299#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009300 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009301#endif
9302
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304}
9305
9306/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009307 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 */
9309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009310ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009312 if (eap->forceit)
9313 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009314 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009315 if (!curwin->w_cursor.lnum)
9316 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009317 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009318 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009319#ifdef FEAT_TERMINAL
9320 // Ignore this when running in an active terminal.
9321 if (term_job_running(curbuf->b_term))
9322 return;
9323#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009324
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009325 // Ignore the command when already in Insert mode. Inserting an
9326 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009327 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009328 return;
9329
Bram Moolenaar64969662005-12-14 21:59:55 +00009330 if (eap->cmdidx == CMD_startinsert)
9331 restart_edit = 'a';
9332 else if (eap->cmdidx == CMD_startreplace)
9333 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009335 restart_edit = 'V';
9336
9337 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009339 if (eap->cmdidx == CMD_startinsert)
9340 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009341 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009343
9344 if (VIsual_active)
9345 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346}
9347
9348/*
9349 * ":stopinsert"
9350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009352ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
9354 restart_edit = 0;
9355 stop_insert_mode = TRUE;
Christian Brabandtcf665cc2025-04-12 18:09:28 +02009356#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
9357 // when called from remote_expr in insert mode, make sure insert mode is
9358 // ended by adding K_NOP to the typeahead buffer
9359 if (vgetc_busy)
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02009360 ins_char_typebuf(K_NOP, 0);
Christian Brabandtcf665cc2025-04-12 18:09:28 +02009361#endif
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009362 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009365/*
9366 * Execute normal mode command "cmd".
9367 * "remap" can be REMAP_NONE or REMAP_YES.
9368 */
9369 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009370exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009371{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009372 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009373 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009374 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009375}
Bram Moolenaar25281632016-01-21 23:32:32 +01009376
Bram Moolenaar25281632016-01-21 23:32:32 +01009377/*
9378 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009379 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009380 */
9381 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009382exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009383{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009384 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009385 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009386
Bram Moolenaar905dd902019-04-07 14:21:47 +02009387 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9388 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009389 clear_oparg(&oa);
9390 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009391 while ((!stuff_empty()
9392 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009393 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009394 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009395 {
9396 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009397#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009398 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009399 && oa.op_type == OP_NOP && oa.regname == NUL
9400 && !VIsual_active)
9401 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009402 // If terminal_loop() returns OK we got a key that is handled
9403 // in Normal model. With FAIL we first need to position the
9404 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009405 if (terminal_loop(TRUE) == OK)
9406 normal_cmd(&oa, TRUE);
9407 }
9408 else
9409#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009410 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009411 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009412 }
9413}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009414
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415#ifdef FEAT_FIND_ID
9416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009417ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
9419 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9420 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009421 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422}
9423
Bram Moolenaar4033c552017-09-16 20:54:51 +02009424#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425/*
9426 * ":psearch"
9427 */
9428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009429ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 g_do_tagpreview = p_pvh;
9432 ex_findpat(eap);
9433 g_do_tagpreview = 0;
9434}
9435#endif
9436
9437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009438ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440 int whole = TRUE;
9441 long n;
9442 char_u *p;
9443 int action;
9444
9445 switch (cmdnames[eap->cmdidx].cmd_name[2])
9446 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009447 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9449 action = ACTION_GOTO;
9450 else
9451 action = ACTION_SHOW;
9452 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009453 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 action = ACTION_SHOW_ALL;
9455 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009456 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 action = ACTION_GOTO;
9458 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009459 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 action = ACTION_SPLIT;
9461 break;
9462 }
9463
9464 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009465 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
9467 n = getdigits(&eap->arg);
9468 eap->arg = skipwhite(eap->arg);
9469 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009470 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 {
9472 whole = FALSE;
9473 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009474 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 if (*p)
9476 {
9477 *p++ = NUL;
9478 p = skipwhite(p);
9479
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009480 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009481 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009482 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009484 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 }
9486 }
9487 if (!eap->skip)
9488 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9489 whole, !eap->forceit,
9490 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009491 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492}
9493#endif
9494
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495
Bram Moolenaar4033c552017-09-16 20:54:51 +02009496#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497/*
9498 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9499 */
9500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009501ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009503 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9505}
9506
9507/*
9508 * ":pedit"
9509 */
9510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009511ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512{
9513 win_T *curwin_save = curwin;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009514 prepare_preview_window();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009516 // Edit the file.
9517 do_exedit(eap, NULL);
9518
9519 back_to_current_window(curwin_save);
9520}
9521
9522/*
9523 * ":pbuffer"
9524 */
9525 static void
9526ex_pbuffer(exarg_T *eap)
9527{
9528 win_T *curwin_save = curwin;
9529 prepare_preview_window();
9530
9531 // Go to the buffer.
9532 do_exbuffer(eap);
9533
9534 back_to_current_window(curwin_save);
9535}
9536
9537 static void
9538prepare_preview_window(void)
9539{
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009540 if (ERROR_IF_ANY_POPUP_WINDOW)
9541 return;
9542
Bram Moolenaar026587b2019-08-17 15:08:00 +02009543 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009545 prepare_tagpreview(TRUE, TRUE, FALSE);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009546}
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009547
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009548 static void
9549back_to_current_window(win_T *curwin_save)
9550{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 if (curwin != curwin_save && win_valid(curwin_save))
9552 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009553 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009555 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 win_enter(curwin_save, TRUE);
9557 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009558# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009559 else if (WIN_IS_POPUP(curwin))
9560 {
9561 // can't keep focus in popup window
9562 win_enter(firstwin, TRUE);
9563 }
9564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 g_do_tagpreview = 0;
9566}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568
9569/*
9570 * ":stag", ":stselect" and ":stjump".
9571 */
9572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009573ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574{
9575 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009576 postponed_split_flags = cmdmod.cmod_split;
9577 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9579 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009580 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582
9583/*
9584 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9585 */
9586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009587ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9590}
9591
9592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009593ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 int cmd;
9596
9597 switch (name[1])
9598 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009599 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009601 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009603 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009605 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009607 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009609 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009611 case 'f': // ":tfirst"
9612 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009614 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009616 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009618 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009620 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 return;
9622 }
9623#endif
9624 cmd = DT_TAG;
9625 break;
9626 }
9627
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009628 if (name[0] == 'l')
9629 {
9630#ifndef FEAT_QUICKFIX
9631 ex_ni(eap);
9632 return;
9633#else
9634 cmd = DT_LTAG;
9635#endif
9636 }
9637
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9639 eap->forceit, TRUE);
9640}
9641
John Marriotted908f72024-04-18 22:46:56 +02009642enum {
9643 SPEC_PERC = 0,
9644 SPEC_HASH,
9645 SPEC_CWORD, // cursor word
9646 SPEC_CCWORD, // cursor WORD
9647 SPEC_CEXPR, // expr under cursor
9648 SPEC_CFILE, // cursor path name
9649 SPEC_SFILE, // ":so" file name
9650 SPEC_SLNUM, // ":so" file line number
9651 SPEC_STACK, // call stack
9652 SPEC_SCRIPT, // script file name
9653 SPEC_AFILE, // autocommand file name
9654 SPEC_ABUF, // autocommand buffer number
9655 SPEC_AMATCH, // autocommand match name
9656 SPEC_SFLNUM, // script file line number
9657 SPEC_SID // script ID: <SNR>123_
9658#ifdef FEAT_CLIENTSERVER
9659 , SPEC_CLIENT
9660#endif
9661};
9662
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009664 * Check "str" for starting with a special cmdline variable.
9665 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9666 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9667 */
9668 int
Mike Williams51024bb2024-05-30 07:46:30 +02009669find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009670{
John Marriotted908f72024-04-18 22:46:56 +02009671 // must be sorted by the 'value' field because it is used by bsearch()!
9672 static keyvalue_T spec_str_tab[] = {
9673 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9674 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9675 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9676 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9677 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9678 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9679 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009680#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009681 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009682#endif
John Marriotted908f72024-04-18 22:46:56 +02009683 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9684 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9685 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9686 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9687 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9688 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009689 };
John Marriotted908f72024-04-18 22:46:56 +02009690 keyvalue_T target;
9691 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009692
John Marriotted908f72024-04-18 22:46:56 +02009693 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009694 {
John Marriotted908f72024-04-18 22:46:56 +02009695 case '%':
9696 *usedlen = 1;
9697 return SPEC_PERC;
9698
9699 case '#':
9700 *usedlen = 1;
9701 return SPEC_HASH;
9702
9703 case '<':
9704 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01009705 target.value.string = src + 1; // skip over '<'
9706 target.value.length = 0; // not used, see cmp_keyvalue_value_n()
John Marriotted908f72024-04-18 22:46:56 +02009707
John Marriott8d4477e2024-11-02 15:59:01 +01009708 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab,
9709 ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]),
9710 cmp_keyvalue_value_n);
John Marriotted908f72024-04-18 22:46:56 +02009711 if (entry == NULL)
9712 return -1;
9713
John Marriott8d4477e2024-11-02 15:59:01 +01009714 *usedlen = entry->value.length + 1;
John Marriotted908f72024-04-18 22:46:56 +02009715 return entry->key;
9716
9717 default:
9718 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009719 }
John Marriotted908f72024-04-18 22:46:56 +02009720
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009721 return -1;
9722}
9723
9724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 * Evaluate cmdline variables.
9726 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009727 * change "%" to curbuf->b_ffname
9728 * "#" to curwin->w_alt_fnum
9729 * "%%" to curwin->w_alt_fnum in Vim9 script
9730 * "<cword>" to word under the cursor
9731 * "<cWORD>" to WORD under the cursor
9732 * "<cexpr>" to C-expression under the cursor
9733 * "<cfile>" to path name under the cursor
9734 * "<sfile>" to sourced file name
9735 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009736 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009737 * "<slnum>" to sourced file line number
9738 * "<afile>" to file name for autocommand
9739 * "<abuf>" to buffer number for autocommand
9740 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 *
9742 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9743 * "" for error without a message) and NULL is returned.
9744 * Returns an allocated string if a valid match was found.
9745 * Returns NULL if no match was found. "usedlen" then still contains the
9746 * number of characters to skip.
9747 */
9748 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009749eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009750 char_u *src, // pointer into commandline
9751 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009752 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009753 linenr_T *lnump, // line number for :e command, or NULL
9754 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009755 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009756 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009757 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758{
9759 int i;
9760 char_u *s;
9761 char_u *result;
9762 char_u *resultbuf = NULL;
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +01009763 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009765 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009767 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770
9771 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009772 if (escaped != NULL)
9773 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774
9775 /*
9776 * Check if there is something to do.
9777 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009778 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009779 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 {
9781 *usedlen = 1;
9782 return NULL;
9783 }
9784
9785 /*
9786 * Skip when preceded with a backslash "\%" and "\#".
9787 * Note: In "\\%" the % is also not recognized!
9788 */
9789 if (src > srcstart && src[-1] == '\\')
9790 {
9791 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009792 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 return NULL;
9794 }
9795
9796 /*
9797 * word or WORD under cursor
9798 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009799 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9800 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009802 resultlen = find_ident_under_cursor(&result,
9803 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9804 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9805 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (resultlen == 0)
9807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009808 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 return NULL;
9810 }
9811 }
9812
9813 /*
9814 * '#': Alternate file name
9815 * '%': Current file name
9816 * File name under the cursor
9817 * File name for autocommand
9818 * and following modifiers
9819 */
9820 else
9821 {
Mike Williams51024bb2024-05-30 07:46:30 +02009822 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009823
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 switch (spec_idx)
9825 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009826 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009827#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009828 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009831 // '%': current file
9832 if (curbuf->b_fname == NULL)
9833 {
9834 result = (char_u *)"";
9835 valid = 0; // Must have ":p:h" to be valid
9836 }
9837 else
9838 {
9839 result = curbuf->b_fname;
9840 tilde_file = STRCMP(result, "~") == 0;
9841 }
9842 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009844#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009845 // "%%" alternate file
9846 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009847#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009848 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009849 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009850 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009852 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 result = arg_all();
9854 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009855 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009856 if (escaped != NULL)
9857 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 break;
9860 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009861 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009862 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009863 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009865 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009866 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009867 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009868 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009870 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009872 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009874 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009875 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009876 return NULL;
9877 }
9878#ifdef FEAT_EVAL
9879 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9880 (long)i);
9881 if (result == NULL)
9882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009883 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009884 return NULL;
9885 }
9886#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009887 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 }
9891 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009892 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009893 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9894 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009895 buf = buflist_findnr(i);
9896 if (buf == NULL)
9897 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009898 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009899 return NULL;
9900 }
9901 if (lnump != NULL)
9902 *lnump = ECMD_LAST;
9903 if (buf->b_fname == NULL)
9904 {
9905 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009906 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009907 }
9908 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009909 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009910 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009911 tilde_file = STRCMP(result, "~") == 0;
9912 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 break;
9915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009916 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009917 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 if (result == NULL)
9919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009920 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 return NULL;
9922 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009923 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009926 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009928 if (result != NULL && !autocmd_fname_full)
9929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009930 // Still need to turn the fname into a full path. It is
9931 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009932 autocmd_fname_full = TRUE;
9933 result = FullName_save(autocmd_fname, FALSE);
9934 vim_free(autocmd_fname);
9935 autocmd_fname = result;
9936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 if (result == NULL)
9938 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009939 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 return NULL;
9941 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009942 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 break;
9944
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009945 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 if (autocmd_bufnr <= 0)
9947 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009948 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 return NULL;
9950 }
9951 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9952 result = strbuf;
9953 break;
9954
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009955 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 result = autocmd_match;
9957 if (result == NULL)
9958 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009959 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 return NULL;
9961 }
9962 break;
9963
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009964 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009965 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 if (result == NULL)
9967 {
LemonBoy6013d002022-04-09 21:42:10 +01009968 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9969 return NULL;
9970 }
9971 resultbuf = result; // remember allocated string
9972 break;
9973 case SPEC_STACK: // call stack
9974 result = estack_sfile(ESTACK_STACK);
9975 if (result == NULL)
9976 {
9977 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9978 return NULL;
9979 }
9980 resultbuf = result; // remember allocated string
9981 break;
9982 case SPEC_SCRIPT: // script file name
9983 result = estack_sfile(ESTACK_SCRIPT);
9984 if (result == NULL)
9985 {
9986 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 return NULL;
9988 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009989 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009992 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009993 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009994 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009995 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009996 return NULL;
9997 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009998 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009999 result = strbuf;
10000 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010001
10002#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010003 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010010004 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010005 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +000010006 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010007 return NULL;
10008 }
10009 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010010010 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010011 result = strbuf;
10012 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010013
Bram Moolenaar90944302020-08-01 20:45:11 +020010014 case SPEC_SID:
10015 if (current_sctx.sc_sid <= 0)
10016 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +000010017 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +020010018 return NULL;
10019 }
10020 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
10021 result = strbuf;
10022 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +020010023#endif
Bram Moolenaar90944302020-08-01 20:45:11 +020010024
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010025#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010026 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010027 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10028 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 result = strbuf;
10030 break;
10031#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020010032
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010033 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010034 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +020010035 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 }
10037
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010038 resultlen = STRLEN(result); // length of new string
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010039 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 {
10041 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010043 resultlen = s - result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 else if (!skip_mod)
10046 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010047 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 &resultlen);
10049 if (result == NULL)
10050 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010051 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 return NULL;
10053 }
10054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 }
10056
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010057 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010059 if (empty_is_error)
10060 {
10061 if (valid != VALID_HEAD + VALID_PATH)
10062 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
10063 else
10064 *errormsg = _(e_evaluates_to_an_empty_string);
10065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 result = NULL;
10067 }
10068 else
10069 result = vim_strnsave(result, resultlen);
10070 vim_free(resultbuf);
10071 return result;
10072}
10073
10074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 * Expand the <sfile> string in "arg".
10076 *
10077 * Returns an allocated string, or NULL for any error.
10078 */
10079 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010080expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010082 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +020010084 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +020010086 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +020010088 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +020010089 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 char_u *p;
10091
John Marriotted908f72024-04-18 22:46:56 +020010092 resultlen = STRLEN(arg);
10093 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 if (result == NULL)
10095 return NULL;
10096
10097 for (p = result; *p; )
10098 {
10099 if (STRNCMP(p, "<sfile>", 7) != 0)
10100 ++p;
10101 else
10102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010103 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +010010104 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 if (errormsg != NULL)
10106 {
10107 if (*errormsg)
10108 emsg(errormsg);
10109 vim_free(result);
10110 return NULL;
10111 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010112 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 {
10114 p += srclen;
10115 continue;
10116 }
John Marriotted908f72024-04-18 22:46:56 +020010117 repllen = STRLEN(repl);
10118 resultlen += repllen - srclen;
10119 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 if (newres == NULL)
10121 {
10122 vim_free(repl);
10123 vim_free(result);
10124 return NULL;
10125 }
John Marriotted908f72024-04-18 22:46:56 +020010126 len = p - result;
10127 mch_memmove(newres, result, len);
10128 STRCPY(newres + len, repl);
10129 len += repllen;
10130 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 vim_free(repl);
10132 vim_free(result);
10133 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010134 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 }
10136 }
10137
10138 return result;
10139}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010142/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010143 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010144 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010147dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 if (fname == NULL)
10150 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010151 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152}
10153#endif
10154
10155/*
10156 * ":behave {mswin,xterm}"
10157 */
10158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010159ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160{
10161 if (STRCMP(eap->arg, "mswin") == 0)
10162 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010163 set_option_value_give_err((char_u *)"selection",
10164 0L, (char_u *)"exclusive", 0);
10165 set_option_value_give_err((char_u *)"selectmode",
10166 0L, (char_u *)"mouse,key", 0);
10167 set_option_value_give_err((char_u *)"mousemodel",
10168 0L, (char_u *)"popup", 0);
10169 set_option_value_give_err((char_u *)"keymodel",
10170 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 }
10172 else if (STRCMP(eap->arg, "xterm") == 0)
10173 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010174 set_option_value_give_err((char_u *)"selection",
10175 0L, (char_u *)"inclusive", 0);
10176 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
10177 set_option_value_give_err((char_u *)"mousemodel",
10178 0L, (char_u *)"extend", 0);
10179 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 }
10181 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010182 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183}
10184
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185static int filetype_detect = FALSE;
10186static int filetype_plugin = FALSE;
10187static int filetype_indent = FALSE;
10188
10189/*
10190 * ":filetype [plugin] [indent] {on,off,detect}"
10191 * on: Load the filetype.vim file to install autocommands for file types.
10192 * off: Load the ftoff.vim file to remove all autocommands for file types.
10193 * plugin on: load filetype.vim and ftplugin.vim
10194 * plugin off: load ftplugof.vim
10195 * indent on: load filetype.vim and indent.vim
10196 * indent off: load indoff.vim
10197 */
10198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010199ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200{
10201 char_u *arg = eap->arg;
10202 int plugin = FALSE;
10203 int indent = FALSE;
10204
10205 if (*eap->arg == NUL)
10206 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010207 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010208 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 filetype_detect ? "ON" : "OFF",
10210 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10211 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10212 return;
10213 }
10214
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010215 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 for (;;)
10217 {
10218 if (STRNCMP(arg, "plugin", 6) == 0)
10219 {
10220 plugin = TRUE;
10221 arg = skipwhite(arg + 6);
10222 continue;
10223 }
10224 if (STRNCMP(arg, "indent", 6) == 0)
10225 {
10226 indent = TRUE;
10227 arg = skipwhite(arg + 6);
10228 continue;
10229 }
10230 break;
10231 }
10232 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10233 {
10234 if (*arg == 'o' || !filetype_detect)
10235 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010236 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 filetype_detect = TRUE;
10238 if (plugin)
10239 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010240 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 filetype_plugin = TRUE;
10242 }
10243 if (indent)
10244 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010245 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 filetype_indent = TRUE;
10247 }
10248 }
10249 if (*arg == 'd')
10250 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010251 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010252 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 }
10254 }
10255 else if (STRCMP(arg, "off") == 0)
10256 {
10257 if (plugin || indent)
10258 {
10259 if (plugin)
10260 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010261 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 filetype_plugin = FALSE;
10263 }
10264 if (indent)
10265 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010266 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 filetype_indent = FALSE;
10268 }
10269 }
10270 else
10271 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010272 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 filetype_detect = FALSE;
10274 }
10275 }
10276 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010277 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278}
10279
10280/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010281 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 */
10283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010284ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285{
zeertzjq5bf6c212024-03-31 18:41:27 +020010286 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010287 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010288
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010289 char_u *arg = eap->arg;
10290 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10291 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010292
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010293 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10294 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +020010295 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010299ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300{
10301#ifdef FEAT_DIGRAPHS
10302 if (*eap->arg != NUL)
10303 putdigraph(eap->arg);
10304 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010305 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306#else
mityu61065042021-07-19 20:07:21 +020010307 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308#endif
10309}
10310
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010311#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10312 void
10313set_no_hlsearch(int flag)
10314{
10315 no_hlsearch = flag;
10316# ifdef FEAT_EVAL
10317 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10318# endif
10319}
10320
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321/*
10322 * ":nohlsearch"
10323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010325ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010327 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010328 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330#endif
10331
10332#ifdef FEAT_CRYPT
10333/*
10334 * ":X": Get crypt key
10335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010337ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010339 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010340 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341}
10342#endif
10343
10344#ifdef FEAT_FOLDING
10345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010346ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347{
10348 if (foldManualAllowed(TRUE))
10349 foldCreate(eap->line1, eap->line2);
10350}
10351
10352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010353ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354{
10355 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10356 eap->forceit, FALSE);
10357}
10358
10359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010360ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361{
10362 linenr_T lnum;
10363
Bram Moolenaar4facea32019-10-12 20:17:40 +020010364# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010365 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010366# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010367
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010368 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10370 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10371 ml_setmarked(lnum);
10372
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010373 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010375 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010376# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010377 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379}
10380#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010382#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010383/*
10384 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10385 * instead of a quickfix command.
10386 */
10387 int
10388is_loclist_cmd(int cmdidx)
10389{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010390 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010391 return FALSE;
10392 return cmdnames[cmdidx].cmd_name[0] == 'l';
10393}
10394#endif
10395
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010396 int
10397get_pressedreturn(void)
10398{
10399 return ex_pressedreturn;
10400}
10401
10402 void
10403set_pressedreturn(int val)
10404{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +020010405 ex_pressedreturn = val;
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010406}