blob: bda20f54cfce582739d6771f795d6533b1a90c7a [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);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_startinsert(exarg_T *eap);
253static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100255static void ex_checkpath(exarg_T *eap);
256static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#else
258# define ex_findpat ex_ni
259# define ex_checkpath ex_ni
260#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200261#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#else
264# define ex_psearch ex_ni
265#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100266static void ex_tag(exarg_T *eap);
267static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200269# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200270# define ex_break ex_ni
271# define ex_breakadd ex_ni
272# define ex_breakdel ex_ni
273# define ex_breaklist ex_ni
274# define ex_call ex_ni
275# define ex_catch ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000276# define ex_class ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200277# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_continue ex_ni
279# define ex_debug ex_ni
280# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200281# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200282# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_echo ex_ni
285# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200287# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_endfunction ex_ni
289# define ex_endif ex_ni
290# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291# define ex_endwhile ex_ni
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000292# define ex_enum 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.
471 * fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid buffer ID.
472 * ffname is a full path to where a buffer lives on-disk or would live on-disk.
473 *
474 */
475 static int
476is_other_file(int fnum, char_u *ffname)
477{
478 if (fnum != 0)
479 {
480 if (fnum == curbuf->b_fnum)
481 return FALSE;
482
483 return TRUE;
484 }
485
486 if (ffname == NULL)
487 return TRUE;
488
489 if (*ffname == NUL)
490 return FALSE;
491
492 // TODO: Need a reliable way to know whether a buffer is meant to live on-disk
493 // !curbuf->b_dev_valid is not always available (example: missing on Windows)
494 if (curbuf->b_sfname != NULL
495 && *curbuf->b_sfname != NUL)
496 // This occurs with unsaved buffers. In which case `ffname`
497 // actually corresponds to curbuf->b_sfname
498 return fnamecmp(ffname, curbuf->b_sfname) != 0;
499
500 return otherfile(ffname);
501}
502
503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
505 * command is given.
506 */
507 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100508do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100509 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510{
511 int save_msg_scroll;
512 int prev_msg_row;
513 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100514 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000515
516 if (improved)
517 exmode_active = EXMODE_VIM;
518 else
519 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +0100520 State = MODE_NORMAL;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100521 may_trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000522
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 // When using ":global /pat/ visual" and then "Q" we return to continue
524 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if (global_busy)
526 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
528 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100529 ++RedrawingDisabled; // don't redisplay the window
530 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 ++hold_gui_events;
534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
Bram Moolenaar32526b32019-01-19 17:43:09 +0100536 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 while (exmode_active)
538 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100539 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000540 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
541 {
542 exmode_active = FALSE;
543 break;
544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 msg_scroll = TRUE;
546 need_wait_return = FALSE;
547 ex_pressedreturn = FALSE;
548 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100549 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 prev_msg_row = msg_row;
551 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (improved)
553 {
554 cmdline_row = msg_row;
555 do_cmdline(NULL, getexline, NULL, 0);
556 }
557 else
558 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
559 lines_left = Rows - 1;
560
Bram Moolenaardf177f62005-02-22 08:39:57 +0000561 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100562 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000564 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000565 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000568 if (ex_pressedreturn)
569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100570 // go up one line, to overwrite the ":<CR>" line, so the
571 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000572 msg_row = prev_msg_row;
573 if (prev_msg_row == Rows - 1)
574 msg_row--;
575 }
576 msg_col = 0;
577 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
578 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100581 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000582 {
583 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000584 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000585 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000586 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
589
590#ifdef FEAT_GUI
591 --hold_gui_events;
592#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100593 if (RedrawingDisabled > 0)
594 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100596 update_screen(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 need_wait_return = FALSE;
598 msg_scroll = save_msg_scroll;
599}
600
601/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200602 * Print the executed command for when 'verbose' is set.
603 * When "lnum" is 0 only print the command.
604 */
605 static void
606msg_verbose_cmd(linenr_T lnum, char_u *cmd)
607{
608 ++no_wait_return;
609 verbose_enter_scroll();
610
611 if (lnum == 0)
612 smsg(_("Executing: %s"), cmd);
613 else
614 smsg(_("line %ld: %s"), (long)lnum, cmd);
615 if (msg_silent == 0)
616 msg_puts("\n"); // don't overwrite this
617
618 verbose_leave_scroll();
619 --no_wait_return;
620}
621
622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 * Execute a simple command line. Used for translated commands like "*".
624 */
625 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100626do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
628 return do_cmdline(cmd, NULL, NULL,
629 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
630}
631
632/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100633 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
634 * This allows for using a range without ":" in Vim9 script.
635 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200636 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100637do_cmd_argument(char_u *cmd)
638{
639 return do_cmdline(cmd, NULL, NULL,
640 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
641}
642
643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 * do_cmdline(): execute one Ex command line
645 *
646 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100647 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 * 2. Split up in parts separated with '|'.
649 *
650 * This function can be called recursively!
651 *
652 * flags:
653 * DOCMD_VERBOSE - The command will be included in the error message.
654 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100655 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 * DOCMD_KEYTYPED - Don't reset KeyTyped.
657 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
658 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
659 *
660 * return FAIL if cmdline could not be executed, OK otherwise
661 */
662 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100663do_cmdline(
664 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200665 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100666 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100667 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100669 char_u *next_cmdline; // next cmd to execute
670 char_u *cmdline_copy = NULL; // copy of cmd line
671 int used_getline = FALSE; // used "fgetline" to obtain command
672 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100674 int count = 0; // line number count
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100675 int did_inc_RedrawingDisabled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 int retval = OK;
677#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100678 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 garray_T lines_ga; // keep lines for ":while"/":for"
680 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200681 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100682 char_u *fname = NULL; // function or script name
683 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
684 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
685 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200687 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200688 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100690 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200691 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000693 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000695 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698# define cmd_cookie cookie
699#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200701#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
703 // location for storing error messages to be converted to an exception.
704 // This ensures that the do_errthrow() call in do_one_cmd() does not
705 // combine the messages stored by an earlier invocation of do_one_cmd()
706 // with the command name of the later one. This would happen when
707 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 saved_msg_list = msg_list;
709 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#endif
711
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100712 // It's possible to create an endless loop with ":execute", catch that
713 // here. The value of 200 allows nested function calls, ":source", etc.
714 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100715 if (call_depth >= 200
716#ifdef FEAT_EVAL
717 && call_depth >= p_mfd
718#endif
719 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000721 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100723 // When converting to an exception, we do not include the command name
724 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100725 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 msg_list = saved_msg_list;
727#endif
728 return FAIL;
729 }
730 ++call_depth;
731
732#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100733 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000735 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100737 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100739 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100740 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000741 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 ++ex_nesting_level;
743
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100744 // Get the function or script name and the address where the next breakpoint
745 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000746 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
748 fname = func_name(real_cookie);
749 breakpoint = func_breakpoint(real_cookie);
750 dbg_tick = func_dbg_tick(real_cookie);
751 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100754 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 breakpoint = source_breakpoint(real_cookie);
756 dbg_tick = source_dbg_tick(real_cookie);
757 }
758
759 /*
760 * Initialize "force_abort" and "suppress_errthrow" at the top level.
761 */
762 if (!recursive)
763 {
764 force_abort = FALSE;
765 suppress_errthrow = FALSE;
766 }
767
768 /*
769 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000770 * exception handling (used when debugging). Otherwise clear it to avoid
771 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000773 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000774 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000775 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200776 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
778 initial_trylevel = trylevel;
779
780 /*
781 * "did_throw" will be set to TRUE when an exception is being thrown.
782 */
783 did_throw = FALSE;
784#endif
785 /*
786 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 * If force_abort is set, we cancel everything.
789 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100790#ifdef FEAT_EVAL
791 did_emsg_cumul += did_emsg;
792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 did_emsg = FALSE;
794
795 /*
796 * KeyTyped is only set when calling vgetc(). Reset it here when not
797 * calling vgetc() (sourced command lines).
798 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100799 if (!(flags & DOCMD_KEYTYPED)
800 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 KeyTyped = FALSE;
802
803 /*
804 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000805 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 * - for multiple commands on one line, separated with '|'
807 * - when repeating until there are no more lines (for ":source")
808 */
809 next_cmdline = cmdline;
810 do
811 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#endif
815
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100816 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (next_cmdline == NULL
818#ifdef FEAT_EVAL
819 && !force_abort
820 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822#endif
823 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100824 {
825#ifdef FEAT_EVAL
826 did_emsg_cumul += did_emsg;
827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000832 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100833 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * 3. If a line is given: Make a copy, so we can mess with it.
835 */
836
837#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000839 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 // Each '|' separated command is stored separately in lines_ga, to
842 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100843 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100845 // Check if a function has returned or, unless it has an unclosed
846 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000850 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 func_line_end(real_cookie);
852# endif
853 if (func_has_ended(real_cookie))
854 {
855 retval = FAIL;
856 break;
857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000859#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000860 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862 script_line_end();
863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100865 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100866 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 {
868 retval = FAIL;
869 break;
870 }
871
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100872 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (breakpoint != NULL && dbg_tick != NULL
874 && *dbg_tick != debug_tick)
875 {
876 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100877 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100878 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 *dbg_tick = debug_tick;
880 }
881
882 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100883 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100885 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100887 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100889 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100890 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100892 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100893 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 *dbg_tick = debug_tick;
895 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000897 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000898 {
899 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100900 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100901 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902 script_line_start();
903 }
904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100908 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (next_cmdline == NULL)
910 {
911 /*
912 * Need to set msg_didout for the first line after an ":if",
913 * otherwise the ":if" will be overwritten.
914 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100915 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100917 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918#ifdef FEAT_EVAL
919 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
920#else
921 0
922#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100923 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
924 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100926 // Don't call wait_return() for aborted command line. The NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100927 // returned for the end of a sourced file or executed function
928 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (KeyTyped && !(flags & DOCMD_REPEAT))
930 need_wait_return = FALSE;
931 retval = FAIL;
932 break;
933 }
934 used_getline = TRUE;
935
936 /*
937 * Keep the first typed line. Clear it when more lines are typed.
938 */
939 if (flags & DOCMD_KEEPLINE)
940 {
941 vim_free(repeat_cmdline);
942 if (count == 0)
943 repeat_cmdline = vim_strsave(next_cmdline);
944 else
945 repeat_cmdline = NULL;
946 }
947 }
948
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100949 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 else if (cmdline_copy == NULL)
951 {
952 next_cmdline = vim_strsave(next_cmdline);
953 if (next_cmdline == NULL)
954 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200955 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 retval = FAIL;
957 break;
958 }
959 }
960 cmdline_copy = next_cmdline;
961
962#ifdef FEAT_EVAL
963 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200964 * Inside a while/for loop, and when the command looks like a ":while"
965 * or ":for", the line is stored, because we may need it later when
966 * looping.
967 *
968 * When there is a '|' and another command, it is stored separately,
969 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000970 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200971 *
972 * Pass a different "fgetline" function to do_one_cmd() below,
973 * that it stores lines in or reads them from "lines_ga". Makes it
974 * possible to define a function inside a while/for loop and handles
975 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200977 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200979 cmd_getline = get_loop_line;
980 cmd_cookie = (void *)&cmd_loop_cookie;
981 cmd_loop_cookie.lines_gap = &lines_ga;
982 cmd_loop_cookie.current_line = current_line;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100983 cmd_loop_cookie.lc_getline = fgetline;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200984 cmd_loop_cookie.cookie = cookie;
985 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
986
987 // Save the current line when encountering it the first time.
988 if (current_line == lines_ga.ga_len
989 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
991 retval = FAIL;
992 break;
993 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200994 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200996 else
997 {
998 cmd_getline = fgetline;
999 cmd_cookie = cookie;
1000 }
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 did_endif = FALSE;
1003#endif
1004
1005 if (count++ == 0)
1006 {
1007 /*
1008 * All output from the commands is put below each other, without
1009 * waiting for a return. Don't do this when executing commands
1010 * from a script or when being called recursive (e.g. for ":e
1011 * +command file").
1012 */
1013 if (!(flags & DOCMD_NOWAIT) && !recursive)
1014 {
1015 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001016 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001018 msg_scroll = TRUE; // put messages below each other
1019 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001021 did_inc_RedrawingDisabled = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 }
1024
Bram Moolenaar823654b2020-05-29 23:03:09 +02001025 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001026 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
1028 /*
1029 * 2. Execute one '|' separated command.
1030 * do_one_cmd() will return NULL if there is no trailing '|'.
1031 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1032 */
1033 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001034 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#ifdef FEAT_EVAL
1036 &cstack,
1037#endif
1038 cmd_getline, cmd_cookie);
1039 --recursive;
1040
1041#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001043 // Use "current_line" from "cmd_loop_cookie", it may have been
1044 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001045 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046#endif
1047
1048 if (next_cmdline == NULL)
1049 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001050 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 /*
1053 * If the command was typed, remember it for the ':' register.
1054 * Do this AFTER executing the command to make :@: work.
1055 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001056 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 && new_last_cmdline != NULL)
1058 {
1059 vim_free(last_cmdline);
1060 last_cmdline = new_last_cmdline;
1061 new_last_cmdline = NULL;
1062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
1064 else
1065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001066 // need to copy the command after the '|' to cmdline_copy, for the
1067 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001068 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 next_cmdline = cmdline_copy;
1070 }
1071
1072
1073#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001076 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001078 {
1079 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
1085 ++current_line;
1086
1087 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001088 * An ":endwhile", ":endfor" and ":continue" is handled here.
1089 * If we were executing commands, jump back to the ":while" or
1090 * ":for".
1091 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001097 // Jump back to the matching ":while" or ":for". Be careful
1098 // not to use a cs_line[] from an entry that isn't a ":while"
1099 // or ":for": It would make "current_line" invalid and can
1100 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (!did_emsg && !got_int && !did_throw
1102 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 && (cstack.cs_flags[cstack.cs_idx]
1104 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 && cstack.cs_line[cstack.cs_idx] >= 0
1106 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1107 {
1108 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001109 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001111 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001113 // Check for the next breakpoint at or after the ":while"
1114 // or ":for".
Bram Moolenaar35d21c62022-09-02 16:47:16 +01001115 if (breakpoint != NULL && lines_ga.ga_len > current_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
1117 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001118 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 fname,
1120 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1121 *dbg_tick = debug_tick;
1122 }
1123 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001126 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001128 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1129 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 }
1132
1133 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001139 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 }
1141 }
1142
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001143 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001144 if (breakpoint != NULL && has_watchexpr())
1145 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001146 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001147 *dbg_tick = debug_tick;
1148 }
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 /*
1151 * When not inside any ":while" loop, clear remembered lines.
1152 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
1155 if (lines_ga.ga_len > 0)
1156 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001157 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1159 free_cmdlines(&lines_ga);
1160 }
1161 current_line = 0;
1162 }
1163
1164 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1166 * being restored at the ":endtry". Reset them here and set the
1167 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1168 * This includes the case where a missing ":endif", ":endwhile" or
1169 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 cstack.cs_lflags &= ~CSL_HAD_FINA;
1174 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1175 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 did_throw ? (void *)current_exception : NULL);
1177 did_emsg = got_int = did_throw = FALSE;
1178 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1179 }
1180
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001181 // Update global "trylevel" for recursive calls to do_cmdline() from
1182 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 trylevel = initial_trylevel + cstack.cs_trylevel;
1184
1185 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001186 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 * files) is aborted because of an error, an interrupt, or an uncaught
1188 * exception, cancel everything. If it is left normally, reset
1189 * force_abort to get the non-EH compatible abortion behavior for
1190 * the rest of the script.
1191 */
1192 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1193 force_abort = FALSE;
1194
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001195 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001197#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
1199 }
1200 /*
1201 * Continue executing command lines when:
1202 * - no CTRL-C typed, no aborting error, no exception thrown or try
1203 * conditionals need to be checked for executing finally clauses or
1204 * catching an interrupt exception
1205 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 * looping for ":source" command or function call.
1208 */
1209 while (!((got_int
1210#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001211 || (did_emsg && (force_abort || in_vim9script()))
1212 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#endif
1214 )
1215#ifdef FEAT_EVAL
1216 && cstack.cs_trylevel == 0
1217#endif
1218 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001219 && !(did_emsg
1220#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001221 // Keep going when inside try/catch, so that the error can be
1222 // deal with, except when it is a syntax error, it may cause
1223 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001224 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1225#endif
1226 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001227 && (getline_equal(fgetline, cookie, getexmodeline)
1228 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 && (next_cmdline != NULL
1230#ifdef FEAT_EVAL
1231 || cstack.cs_idx >= 0
1232#endif
1233 || (flags & DOCMD_REPEAT)));
1234
1235 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001236 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237#ifdef FEAT_EVAL
1238 free_cmdlines(&lines_ga);
1239 ga_clear(&lines_ga);
1240
1241 if (cstack.cs_idx >= 0)
1242 {
1243 /*
1244 * If a sourced file or executed function ran to its end, report the
1245 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001246 * In Vim9 script do not give a second error, executing aborts after
1247 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 */
Bram Moolenaarbf79a4e2022-05-27 13:52:08 +01001249 if (!got_int && !did_throw && !aborting()
1250 && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001251 && ((getline_equal(fgetline, cookie, getsourceline)
1252 && !source_finished(fgetline, cookie))
1253 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 && !func_has_ended(real_cookie))))
1255 {
1256 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001257 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001259 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001260 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001261 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001263 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265
1266 /*
1267 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1268 * ":endtry" in a sourced file or executed function. If the try
1269 * conditional is in its finally clause, ignore anything pending.
1270 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001271 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 */
1273 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001274 {
1275 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1276
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001277 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001278 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001279 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1280 &cstack.cs_looplevel);
1281 }
1282 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 trylevel = initial_trylevel;
1284 }
1285
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001286 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1287 // lack was reported above and the error message is to be converted to an
1288 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001289 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 ? (char_u *)"endfunction" : (char_u *)NULL);
1291
1292 if (trylevel == 0)
1293 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001294 // Just in case did_throw got set but current_exception wasn't.
1295 if (current_exception == NULL)
1296 did_throw = FALSE;
1297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 /*
1299 * When an exception is being thrown out of the outermost try
1300 * conditional, discard the uncaught exception, disable the conversion
1301 * of interrupts or errors to exceptions, and ensure that no more
1302 * commands are executed.
1303 */
1304 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001305 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
1307 /*
1308 * On an interrupt or an aborting error not converted to an exception,
1309 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001310 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 * when force_abort is set and did_emsg unset in case of an interrupt
1312 * from a finally clause after an error.
1313 */
1314 else if (got_int || (did_emsg && force_abort))
1315 suppress_errthrow = TRUE;
1316 }
1317
1318 /*
1319 * The current cstack will be freed when do_cmdline() returns. An uncaught
1320 * exception will have to be rethrown in the previous cstack. If a function
1321 * has just returned or a script file was just finished and the previous
1322 * cstack belongs to the same function or, respectively, script file, it
1323 * will have to be checked for finally clauses to be executed due to the
1324 * ":return" or ":finish". This is done in do_one_cmd().
1325 */
1326 if (did_throw)
1327 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001330 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && ex_nesting_level > func_level(real_cookie) + 1))
1332 {
1333 if (!did_throw)
1334 check_cstack = TRUE;
1335 }
1336 else
1337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001338 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 --ex_nesting_level;
1341 /*
1342 * Go to debug mode when returning from a function in which we are
1343 * single-stepping.
1344 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001345 if ((getline_equal(fgetline, cookie, getsourceline)
1346 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001348 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 ? (char_u *)_("End of sourced file")
1350 : (char_u *)_("End of function"));
1351 }
1352
1353 /*
1354 * Restore the exception environment (done after returning from the
1355 * debugger).
1356 */
1357 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001358 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001361
1362 // Cleanup if "cs_emsg_silent_list" remains.
1363 if (cstack.cs_emsg_silent_list != NULL)
1364 {
1365 eslist_T *elem, *temp;
1366
1367 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1368 {
1369 temp = elem->next;
1370 vim_free(elem);
1371 }
1372 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001373#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 /*
1376 * If there was too much output to fit on the command line, ask the user to
1377 * hit return before redrawing the screen. With the ":global" command we do
1378 * this only once after the command is finished.
1379 */
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001380 if (did_inc_RedrawingDisabled)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
Bram Moolenaar79cdf022023-05-20 14:07:00 +01001382 if (RedrawingDisabled > 0)
1383 --RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 --no_wait_return;
1385 msg_scroll = FALSE;
1386
1387 /*
1388 * When just finished an ":if"-":else" which was typed, no need to
1389 * wait for hit-return. Also for an error situation.
1390 */
1391 if (retval == FAIL
1392#ifdef FEAT_EVAL
1393 || (did_endif && KeyTyped && !did_emsg)
1394#endif
1395 )
1396 {
1397 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001398 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
1400 else if (need_wait_return)
1401 {
1402 /*
Bram Moolenaar13608d82022-08-29 15:06:50 +01001403 * The msg_start() above clears msg_didout. The wait_return() we do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 * here should not overwrite the command that may be shown before
1405 * doing that.
1406 */
1407 msg_didout |= msg_didout_before_start;
1408 wait_return(FALSE);
1409 }
1410 }
1411
Bram Moolenaar2a942252012-11-28 23:03:07 +01001412#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001413 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001414#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 /*
1416 * Reset if_level, in case a sourced script file contains more ":if" than
1417 * ":endif" (could be ":if x | foo | endif").
1418 */
1419 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001420#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 --call_depth;
1423 return retval;
1424}
1425
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001426#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001427/*
1428 * Handle when "did_throw" is set after executing commands.
1429 */
1430 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001431handle_did_throw(void)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001432{
1433 char *p = NULL;
1434 msglist_T *messages = NULL;
ichizok7e5fe382023-04-15 13:17:50 +01001435 ESTACK_CHECK_DECLARATION;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001436
1437 /*
1438 * If the uncaught exception is a user exception, report it as an
1439 * error. If it is an error exception, display the saved error
1440 * message now. For an interrupt exception, do nothing; the
1441 * interrupt message is given elsewhere.
1442 */
1443 switch (current_exception->type)
1444 {
1445 case ET_USER:
1446 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001447 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001448 current_exception->value);
1449 p = (char *)vim_strsave(IObuff);
1450 break;
1451 case ET_ERROR:
1452 messages = current_exception->messages;
1453 current_exception->messages = NULL;
1454 break;
1455 case ET_INTERRUPT:
1456 break;
1457 }
1458
1459 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1460 current_exception->throw_lnum);
ichizok7e5fe382023-04-15 13:17:50 +01001461 ESTACK_CHECK_SETUP;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001462 current_exception->throw_name = NULL;
1463
1464 discard_current_exception(); // uses IObuff if 'verbose'
LemonBoy749ba0f2024-07-04 19:23:16 +02001465
1466 // If "silent!" is active the uncaught exception is not fatal.
1467 if (emsg_silent == 0)
1468 {
1469 suppress_errthrow = TRUE;
1470 force_abort = TRUE;
1471 }
Bram Moolenaar620c9592021-07-31 21:32:31 +02001472
1473 if (messages != NULL)
1474 {
1475 do
1476 {
1477 msglist_T *next = messages->next;
1478 int save_compiling = estack_compiling;
1479
1480 estack_compiling = messages->msg_compiling;
1481 emsg(messages->msg);
1482 vim_free(messages->msg);
1483 vim_free(messages->sfile);
1484 vim_free(messages);
1485 messages = next;
1486 estack_compiling = save_compiling;
1487 }
1488 while (messages != NULL);
1489 }
1490 else if (p != NULL)
1491 {
1492 emsg(p);
1493 vim_free(p);
1494 }
1495 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01001496 ESTACK_CHECK_NOW;
Bram Moolenaar620c9592021-07-31 21:32:31 +02001497 estack_pop();
1498}
1499
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 */
1503 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001504get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001506 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 wcmd_T *wp;
1508 char_u *line;
1509
1510 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1511 {
1512 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001513 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001515 // First time inside the ":while"/":for": get line normally.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001516 if (cp->lc_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001517 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001519 line = cp->lc_getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001520 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 ++cp->current_line;
1522
1523 return line;
1524 }
1525
1526 KeyTyped = FALSE;
1527 ++cp->current_line;
1528 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001529 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 return vim_strsave(wp->line);
1531}
1532
1533/*
1534 * Store a line in "gap" so that a ":while" loop can execute it again.
1535 */
1536 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001537store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 if (ga_grow(gap, 1) == FAIL)
1540 return FAIL;
1541 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001542 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 return OK;
1545}
1546
1547/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001548 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 */
1550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001551free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 while (gap->ga_len > 0)
1554 {
1555 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1556 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 }
1558}
1559#endif
1560
1561/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001562 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1563 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001566getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001567 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001568 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001569 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001572 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001573 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001575 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1576 // function that's originally used to obtain the lines. This may be
1577 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001578 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001579 cp = (struct loop_cookie *)cookie;
1580 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001582 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 cp = cp->cookie;
1584 }
1585 return gp == func;
1586#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001587 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588#endif
1589}
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001592 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 * getline function. Otherwise return "cookie".
1594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001596getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001597 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001598 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
Bram Moolenaar13505972019-01-24 15:04:48 +01001600#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001601 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001602 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001604 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1605 // cookie that's originally used to obtain the lines. This may be nested
1606 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001607 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001608 cp = (struct loop_cookie *)cookie;
1609 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001611 gp = cp->lc_getline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 cp = cp->cookie;
1613 }
1614 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001615#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001618}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001620#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard5053d02020-06-28 15:51:16 +02001621/*
1622 * Get the next line source line without advancing.
1623 */
1624 char_u *
1625getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001626 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001627 void *cookie) // argument for fgetline()
1628{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001629 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001630 struct loop_cookie *cp;
1631 wcmd_T *wp;
1632
1633 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1634 // cookie that's originally used to obtain the lines. This may be nested
1635 // several levels.
1636 gp = fgetline;
1637 cp = (struct loop_cookie *)cookie;
1638 while (gp == get_loop_line)
1639 {
1640 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1641 {
1642 // executing lines a second time, use the stored copy
1643 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1644 return wp->line;
1645 }
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001646 gp = cp->lc_getline;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001647 cp = cp->cookie;
1648 }
1649 if (gp == getsourceline)
1650 return source_nextline(cp);
1651 return NULL;
1652}
1653#endif
1654
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001655
1656/*
1657 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1658 * ":bwipeout", etc.
1659 * Returns the buffer number.
1660 */
1661 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001662compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001663{
1664 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001665 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666 int count = offset;
1667
1668 buf = firstbuf;
1669 while (buf->b_next != NULL && buf->b_fnum < lnum)
1670 buf = buf->b_next;
1671 while (count != 0)
1672 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001673 count += (offset < 0) ? 1 : -1;
1674 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1675 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001676 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001677 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001678 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001679 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001680 while (buf->b_ml.ml_mfp == NULL)
1681 {
1682 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1683 if (nextbuf == NULL)
1684 break;
1685 buf = nextbuf;
1686 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001687 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001688 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001689 if (addr_type == ADDR_LOADED_BUFFERS)
1690 while (buf->b_ml.ml_mfp == NULL)
1691 {
1692 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1693 if (nextbuf == NULL)
1694 break;
1695 buf = nextbuf;
1696 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001697 return buf->b_fnum;
1698}
1699
Bram Moolenaarb7316892019-05-01 18:08:42 +02001700/*
1701 * Return the window number of "win".
1702 * When "win" is NULL return the number of windows.
1703 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001704 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001705current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001706{
1707 win_T *wp;
1708 int nr = 0;
1709
Bram Moolenaar29323592016-07-24 22:04:11 +02001710 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001711 {
1712 ++nr;
1713 if (wp == win)
1714 break;
1715 }
1716 return nr;
1717}
1718
1719 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001720current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001721{
1722 tabpage_T *tp;
1723 int nr = 0;
1724
Bram Moolenaar29323592016-07-24 22:04:11 +02001725 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001726 {
1727 ++nr;
1728 if (tp == tab)
1729 break;
1730 }
1731 return nr;
1732}
1733
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001734 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001735comment_start(char_u *p, int starts_with_colon UNUSED)
1736{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001737 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001738 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001739 return *p == '"';
1740}
1741
Bram Moolenaarf240e182014-11-27 18:33:02 +01001742# define CURRENT_WIN_NR current_win_nr(curwin)
1743# define LAST_WIN_NR current_win_nr(NULL)
1744# define CURRENT_TAB_NR current_tab_nr(curtab)
1745# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747/*
1748 * Execute one Ex command.
1749 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001750 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1751 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 *
1753 * 1. skip comment lines and leading space
1754 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001755 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001756 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001757 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758 * 6. parse arguments
1759 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001761 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 *
1763 * This function may be called recursively!
1764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001766do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001767 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001768 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001770 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001772 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001773 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001775 char_u *p;
1776 linenr_T lnum;
1777 long n;
1778 char *errormsg = NULL; // error message
1779 char_u *after_modifier = NULL;
1780 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001781 cmdmod_T save_cmdmod;
1782 int save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01001783 int save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaarddef1292019-12-16 17:10:33 +01001784 int ni; // set when Not Implemented
1785 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001786 int starts_with_colon = FALSE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001787 int may_have_range;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001788#ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001789 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001790#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001791 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01001792 int did_append_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
Bram Moolenaara80faa82020-04-12 19:37:17 +02001794 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 ea.line1 = 1;
1796 ea.line2 = 1;
1797#ifdef FEAT_EVAL
1798 ++ex_nesting_level;
1799#endif
1800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001801 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (quitmore
1803#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001804 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001805 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001806#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001807 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001808 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 --quitmore;
1810
1811 /*
1812 * Reset browse, confirm, etc.. They are restored when returning, for
1813 * recursive calls.
1814 */
1815 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001817 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001818 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1819 goto doend;
1820
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001821/*
1822 * 1. Skip comment lines and leading white space and colons.
1823 * 2. Handle command modifiers.
1824 */
1825 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001827 ea.cmdlinep = cmdlinep;
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001828 ea.ea_getline = fgetline;
Bram Moolenaareffed932018-08-14 13:38:17 +02001829 ea.cookie = cookie;
1830#ifdef FEAT_EVAL
1831 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001832 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001834 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001835 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001836 apply_cmdmod(&cmdmod);
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001837 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839#ifdef FEAT_EVAL
1840 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1841 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1842#else
1843 ea.skip = (if_level > 0);
1844#endif
1845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001847 * 3. Skip over the range to find the command. Let "p" point to after it.
1848 *
1849 * We need the command to know what kind of range it uses.
1850 */
1851 cmd = ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001852
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001853 // In Vim9 script a colon is required before the range. This may also be
1854 // after command modifiers.
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00001855 int vim9script = in_vim9script();
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001856 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001857 {
1858 may_have_range = FALSE;
1859 for (p = ea.cmd; p >= *cmdlinep; --p)
1860 {
1861 if (*p == ':')
1862 may_have_range = TRUE;
1863 if (p < ea.cmd && !VIM_ISWHITE(*p))
1864 break;
1865 }
1866 }
1867 else
1868 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001869 if (may_have_range)
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001870 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871
Bram Moolenaar5ab30012022-10-07 17:26:22 +01001872#ifdef FEAT_EVAL
1873 // Handle ":export" - it functions almost like a command modifier.
1874 // ":export var Name: type"
1875 // ":export def Name(..."
1876 // etc.
1877 if (vim9script && checkforcmd_noparen(&ea.cmd, "export", 6))
1878 is_export = TRUE;
1879#endif
1880
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001881 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001882 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001883 if (ea.cmd == cmd + 1 && *cmd == '$')
1884 // should be "$VAR = val"
1885 --ea.cmd;
Bram Moolenaareda29c92022-10-02 12:59:00 +01001886#ifdef FEAT_EVAL
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001887 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001888#else
1889 p = find_ex_command(&ea, NULL, NULL, NULL);
1890#endif
Bram Moolenaar36113e42020-11-02 21:08:47 +01001891 if (ea.cmdidx == CMD_SIZE)
1892 {
1893 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1894
1895 // If a ':' before the range is missing, give a clearer error
1896 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001897 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001898 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001899 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001900 goto doend;
1901 }
1902 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001906
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001907#ifdef FEAT_EVAL
1908# ifdef FEAT_PROFILE
1909 // Count this line for profiling if skip is TRUE.
1910 if (do_profiling == PROF_YES
1911 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1912 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1913 {
1914 int skip = did_emsg || got_int || did_throw;
1915
1916 if (ea.cmdidx == CMD_catch)
1917 skip = !skip && !(cstack->cs_idx >= 0
1918 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1919 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1920 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1921 skip = skip || !(cstack->cs_idx >= 0
1922 && !(cstack->cs_flags[cstack->cs_idx]
1923 & (CSF_ACTIVE | CSF_TRUE)));
1924 else if (ea.cmdidx == CMD_finally)
1925 skip = FALSE;
1926 else if (ea.cmdidx != CMD_endif
1927 && ea.cmdidx != CMD_endfor
1928 && ea.cmdidx != CMD_endtry
1929 && ea.cmdidx != CMD_endwhile)
1930 skip = ea.skip;
1931
1932 if (!skip)
1933 {
1934 if (getline_equal(fgetline, cookie, get_func_line))
1935 func_line_exec(getline_cookie(fgetline, cookie));
1936 else if (getline_equal(fgetline, cookie, getsourceline))
1937 script_line_exec();
1938 }
1939 }
1940# endif
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001941#endif
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001942
Bram Moolenaar33b55b52022-10-07 18:51:23 +01001943 ea.cmd = cmd;
1944
1945#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001946 // May go to debug mode. If this happens and the ">quit" debug command is
1947 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001948 dbg_check_breakpoint(&ea);
1949 if (!ea.skip && got_int)
1950 {
1951 ea.skip = TRUE;
1952 (void)do_intthrow(cstack);
1953 }
1954#endif
1955
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001956/*
1957 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 *
1959 * where 'addr' is:
1960 *
1961 * % (entire file)
1962 * $ [+-NUM]
1963 * 'x [+-NUM] (where x denotes a currently defined mark)
1964 * . [+-NUM]
1965 * [+-NUM]..
1966 * NUM
1967 *
1968 * The ea.cmd pointer is updated to point to the first character following the
1969 * range spec. If an initial address is found, but no second, the upper bound
1970 * is equal to the lower.
1971 */
1972
Bram Moolenaar25190db2019-05-04 15:05:28 +02001973 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001974 if (!IS_USER_CMDIDX(ea.cmdidx))
1975 {
1976 if (ea.cmdidx != CMD_SIZE)
1977 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1978 else
1979 ea.addr_type = ADDR_LINES;
1980
Bram Moolenaar25190db2019-05-04 15:05:28 +02001981 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001982 if (ea.cmdidx == CMD_wincmd && p != NULL)
1983 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001984#ifdef FEAT_QUICKFIX
1985 // :.cc in quickfix window uses line number
1986 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1987 ea.addr_type = ADDR_OTHER;
1988#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001989 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001990
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001991 if (!may_have_range)
1992 ea.line1 = ea.line2 = default_address(&ea);
Bram Moolenaareda29c92022-10-02 12:59:00 +01001993 else if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1994 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001997 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 */
1999
2000 /*
2001 * Skip ':' and any white space
2002 */
2003 ea.cmd = skipwhite(ea.cmd);
2004 while (*ea.cmd == ':')
2005 ea.cmd = skipwhite(ea.cmd + 1);
2006
2007 /*
2008 * If we got a line, but no command, then go to the line.
2009 * If we find a '|' or '\n' we set ea.nextcmd.
2010 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002011 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
2012 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
2014 /*
2015 * strange vi behaviour:
2016 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01002017 * ":3|..." prints line 3 (not in Vim9 script)
2018 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002022 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 goto doend;
2024 }
2025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002026 // If this looks like an undefined user command and there are CmdUndefined
2027 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002028 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2029 && ASCII_ISUPPER(*ea.cmd)
2030 && has_cmdundefined())
2031 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002032 int ret;
2033
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002034 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002035 while (ASCII_ISALNUM(*p))
2036 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002037 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002038 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2039 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002040 // If the autocommands did something and didn't cause an error, try
2041 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002042 p = (ret
2043#ifdef FEAT_EVAL
2044 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002045#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002047 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002048
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (p == NULL)
2050 {
2051 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002052 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 goto doend;
2054 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002055 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002056 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2057 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
2059 errormsg = uc_fun_cmd();
2060 goto doend;
2061 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (ea.cmdidx == CMD_SIZE)
2064 {
2065 if (!ea.skip)
2066 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002067 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // If the modifier was parsed OK the error must be in the
2071 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002072 if (after_modifier != NULL)
2073 append_command(after_modifier);
2074 else
2075 append_command(*cmdlinep);
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002076 did_append_cmd = TRUE;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002077 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002078 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002079 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081 goto doend;
2082 }
2083
Bram Moolenaar958636c2014-10-21 20:01:58 +02002084 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2085 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002086#ifdef HAVE_EX_SCRIPT_NI
2087 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2088#endif
2089 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091#ifndef FEAT_EVAL
2092 /*
2093 * When the expression evaluation is disabled, recognize the ":if" and
2094 * ":endif" commands and ignore everything in between it.
2095 */
2096 if (ea.cmdidx == CMD_if)
2097 ++if_level;
2098 if (if_level)
2099 {
2100 if (ea.cmdidx == CMD_endif)
2101 --if_level;
2102 goto doend;
2103 }
2104
2105#endif
2106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002108 if (*p == '!' && ea.cmdidx != CMD_substitute
2109 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
2111 ++p;
2112 ea.forceit = TRUE;
2113 }
2114 else
2115 ea.forceit = FALSE;
2116
2117/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002118 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002120 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002121 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 if (!ea.skip)
2124 {
2125#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002126 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002128 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002129 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 goto doend;
2131 }
2132#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002133 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002134 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002135 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002136 goto doend;
2137 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002138 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002141 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 goto doend;
2143 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002144
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002145 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002147 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2148 {
2149 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002150 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002151 goto doend;
2152 }
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002153 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2154 {
2155 // Command not allowed when text is locked
2156 errormsg = _(get_text_locked_msg());
2157 goto doend;
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002161 // Disallow editing another buffer when "curbuf_lock" is set.
2162 // Do allow ":checktime" (it is postponed).
2163 // Do allow ":edit" (check for an argument later).
2164 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002165 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002166 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002167 && ea.cmdidx != CMD_edit
2168 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002169 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170 && curbuf_locked())
2171 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002173 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002175 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 goto doend;
2177 }
2178 }
2179
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002180 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002182 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 goto doend;
2184 }
2185
2186 /*
2187 * Don't complain about the range if it is not used
2188 * (could happen if line_count is accidentally set to 0).
2189 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002190 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
2192 /*
2193 * If the range is backwards, ask for confirmation and, if given, swap
2194 * ea.line1 & ea.line2 so it's forwards again.
2195 * When global command is busy, don't ask, will fail below.
2196 */
2197 if (!global_busy && ea.line1 > ea.line2)
2198 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002199 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002201 if (sourcing || exmode_active)
2202 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002203 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002204 goto doend;
2205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (ask_yesno((char_u *)
2207 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002208 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
2210 lnum = ea.line1;
2211 ea.line1 = ea.line2;
2212 ea.line2 = lnum;
2213 }
2214 if ((errormsg = invalid_range(&ea)) != NULL)
2215 goto doend;
2216 }
2217
Bram Moolenaarb7316892019-05-01 18:08:42 +02002218 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2219 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 ea.line2 = 1;
2221
2222 correct_range(&ea);
2223
2224#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002225 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002226 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002228 // Put the first line at the start of a closed fold, put the last line
2229 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 (void)hasFolding(ea.line1, &ea.line1, NULL);
2231 (void)hasFolding(ea.line2, NULL, &ea.line2);
2232 }
2233#endif
2234
2235#ifdef FEAT_QUICKFIX
2236 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002237 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 * option here, so things like % get expanded.
2239 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002240 p = replace_makeprg(&ea, p, cmdlinep);
2241 if (p == NULL)
2242 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#endif
2244
2245 /*
2246 * Skip to start of argument.
2247 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2248 */
2249 if (ea.cmdidx == CMD_bang)
2250 ea.arg = p;
2251 else
2252 ea.arg = skipwhite(p);
2253
Bram Moolenaar379fb762018-08-30 15:58:28 +02002254 // ":file" cannot be run with an argument when "curbuf_lock" is set
2255 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2256 goto doend;
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 /*
2259 * Check for "++opt=val" argument.
2260 * Must be first, allow ":w ++enc=utf8 !cmd"
2261 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002262 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2264 if (getargopt(&ea) == FAIL && !ni)
2265 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002266 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 goto doend;
2268 }
2269
2270 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002272 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002274 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002276 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 goto doend;
2278 }
2279 ea.arg = skipwhite(ea.arg + 1);
2280 ea.append = TRUE;
2281 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002282 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
2284 ++ea.arg;
2285 ea.usefilter = TRUE;
2286 }
2287 }
2288
2289 if (ea.cmdidx == CMD_read)
2290 {
2291 if (ea.forceit)
2292 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002293 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 ea.forceit = FALSE;
2295 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002296 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 ++ea.arg;
2299 ea.usefilter = TRUE;
2300 }
2301 }
2302
2303 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2304 {
2305 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002306 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 ++ea.arg;
2309 ++ea.amount;
2310 }
2311 ea.arg = skipwhite(ea.arg);
2312 }
2313
2314 /*
2315 * Check for "+command" argument, before checking for next command.
2316 * Don't do this for ":read !cmd" and ":write !cmd".
2317 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002318 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2320
2321 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002322 * For commands that do not use '|' inside their argument: Check for '|' to
2323 * separate commands and '"' or '#' to start comments.
2324 *
2325 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002326 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002327 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002328 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002330 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2331 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002332 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002333 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002334 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002335 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002336 || ea.cmdidx == CMD_global
2337 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002338 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002339#ifdef FEAT_EVAL
2340 || inside_block(&ea)
2341#endif
2342 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 for (p = ea.arg; *p; ++p)
2345 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02002346 // Remove one backslash before a newline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002348 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002349 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351 ea.nextcmd = p + 1;
2352 *p = NUL;
2353 break;
2354 }
2355 }
2356 }
2357
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002358 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002359 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002361 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002364 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002365 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002366 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002368#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002369 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002370 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002372 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002373 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375#endif
64-bitmane08f10a2025-03-18 22:14:34 +01002376 if (valid_yank_reg(*ea.arg, (!IS_USER_CMDIDX(ea.cmdidx)
2377 && ea.cmdidx != CMD_put && ea.cmdidx != CMD_iput)))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002378 {
2379 ea.regname = *ea.arg++;
2380#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002381 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002382 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2383 {
kuuote08d7b1c2021-10-04 22:17:36 +01002384 if (!ea.skip)
2385 {
2386 set_expr_line(vim_strsave(ea.arg), &ea);
2387 did_set_expr_line = TRUE;
2388 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002389 ea.arg += STRLEN(ea.arg);
2390 }
2391#endif
2392 ea.arg = skipwhite(ea.arg);
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395
2396 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 * count, it's a buffer name.
2399 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002400 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002401 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002402 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002404 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002406 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002408 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 goto doend;
2410 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002411 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
2413 ea.line2 = n;
2414 if (ea.addr_count == 0)
2415 ea.addr_count = 1;
2416 }
2417 else
2418 {
2419 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002420 if (ea.line2 >= LONG_MAX - (n - 1))
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002421 ea.line2 = LONG_MAX; // avoid overflow
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002422 else
2423 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 ++ea.addr_count;
2425 /*
2426 * Be vi compatible: no error message for out of range.
2427 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002428 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 ea.line2 = curbuf->b_ml.ml_line_count;
2430 }
2431 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002432
2433 /*
2434 * Check for flags: 'l', 'p' and '#'.
2435 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002436 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002437 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002438 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2439 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002441 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002442 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 goto doend;
2444 }
2445
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002446 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002448 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 goto doend;
2450 }
2451
2452#ifdef FEAT_EVAL
2453 /*
2454 * Skip the command when it's not going to be executed.
2455 * The commands like :if, :endif, etc. always need to be executed.
2456 * Also make an exception for commands that handle a trailing command
2457 * themselves.
2458 */
2459 if (ea.skip)
2460 {
2461 switch (ea.cmdidx)
2462 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002463 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_while:
2465 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002466 case CMD_for:
2467 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 case CMD_if:
2469 case CMD_elseif:
2470 case CMD_else:
2471 case CMD_endif:
2472 case CMD_try:
2473 case CMD_catch:
2474 case CMD_finally:
2475 case CMD_endtry:
2476 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002477 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 break;
2479
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002480 // Commands that handle '|' themselves. Check: A command should
2481 // either have the EX_TRLBAR flag, appear in this list or appear in
2482 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 case CMD_aboveleft:
2484 case CMD_and:
2485 case CMD_belowright:
2486 case CMD_botright:
2487 case CMD_browse:
2488 case CMD_call:
2489 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002490 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_delfunction:
2492 case CMD_djump:
2493 case CMD_dlist:
2494 case CMD_dsearch:
2495 case CMD_dsplit:
2496 case CMD_echo:
2497 case CMD_echoerr:
2498 case CMD_echomsg:
2499 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002500 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002502 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002503 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 case CMD_help:
2505 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002506 case CMD_horizontal:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 case CMD_ijump:
2508 case CMD_ilist:
2509 case CMD_isearch:
2510 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002511 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 case CMD_keepjumps:
2513 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002514 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 case CMD_leftabove:
2516 case CMD_let:
2517 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002518 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002519 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002521 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002522 case CMD_noautocmd:
2523 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 case CMD_perl:
2525 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002526 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002527 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002528 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 case CMD_return:
2530 case CMD_rightbelow:
2531 case CMD_ruby:
2532 case CMD_silent:
2533 case CMD_smagic:
2534 case CMD_snomagic:
2535 case CMD_substitute:
2536 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002537 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 case CMD_tcl:
2539 case CMD_throw:
2540 case CMD_tilde:
2541 case CMD_topleft:
2542 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002543 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002544 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 case CMD_verbose:
2546 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002547 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 break;
2549
2550 default: goto doend;
2551 }
2552 }
2553#endif
2554
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002555 if ((ea.argt & EX_XFILE)
2556 && expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2557 goto doend;
2558
2559#ifdef FEAT_EVAL
2560 if (is_export && (ea.argt & EX_EXPORT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002562 emsg(_(e_invalid_command_after_export));
2563 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 /*
2568 * Accept buffer name. Cannot be used at the same time with a buffer
2569 * number. Don't do this for a user command.
2570 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002571 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002572 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
2574 /*
2575 * :bdelete, :bwipeout and :bunload take several arguments, separated
2576 * by spaces: find next space (skipping over escaped characters).
2577 * The others take one argument: ignore trailing spaces.
2578 */
2579 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2580 || ea.cmdidx == CMD_bunload)
2581 p = skiptowhite_esc(ea.arg);
2582 else
2583 {
2584 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002585 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 --p;
2587 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002588 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002589 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002590 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 goto doend;
2592 ea.addr_count = 1;
2593 ea.arg = skipwhite(p);
2594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002596 // The :try command saves the emsg_silent flag, reset it here when
2597 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002598 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002599 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002600 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002601 if (emsg_silent < 0)
2602 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002603 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002604 }
2605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002607 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaar958636c2014-10-21 20:01:58 +02002610 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
2612 /*
2613 * Execute a user-defined command.
2614 */
2615 do_ucmd(&ea);
2616 }
2617 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 {
2619 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002620 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2623 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002624 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
2626
2627#ifdef FEAT_EVAL
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002628 // A command will reset "is_export" when exporting an item. If it is still
LemonBoy90c27b22023-08-27 19:28:15 +02002629 // set something went wrong or the command was never executed.
2630 if (!ea.skip && is_export)
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002631 {
2632 if (errormsg == NULL)
2633 errormsg = _(e_export_with_invalid_argument);
2634 is_export = FALSE;
2635 }
2636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002638 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002639 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002640 && current_sctx.sc_sid > 0
2641 && ea.cmdidx != CMD_endif
2642 && (cstack->cs_idx < 0
2643 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002644 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 /*
2647 * If the command just executed called do_cmdline(), any throw or ":return"
2648 * or ":finish" encountered there must also check the cstack of the still
2649 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2650 * exception, or reanimate a returned function or finished script file and
2651 * return or finish it again.
2652 */
2653 if (need_rethrow)
2654 do_throw(cstack);
2655 else if (check_cstack)
2656 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002657 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002659 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 && current_func_returned())
2661 do_return(&ea, TRUE, FALSE, NULL);
2662 }
2663 need_rethrow = check_cstack = FALSE;
2664#endif
2665
2666doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002667 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002668 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002670 curwin->w_cursor.col = 0;
2671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
2673 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2674 {
Bram Moolenaar6ce1b592022-06-14 16:06:07 +01002675 if ((sourcing || !KeyTyped) && !did_append_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002677 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
2679 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002680 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002682 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
2684 emsg(errormsg);
2685 }
2686#ifdef FEAT_EVAL
2687 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002688 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2689 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002690
2691 if (did_set_expr_line)
2692 set_expr_line(NULL, NULL);
Bram Moolenaar5ab30012022-10-07 17:26:22 +01002693 is_export = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#endif
2695
Bram Moolenaare1004402020-10-24 20:49:43 +02002696 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002698 reg_executing = save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01002699 pending_end_reg_executing = save_pending_end_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002701 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 ea.nextcmd = NULL;
2703
2704#ifdef FEAT_EVAL
2705 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002706 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#endif
2708
2709 return ea.nextcmd;
2710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002712static char ex_error_buf[MSG_BUF_LEN];
2713
2714/*
2715 * Return an error message with argument included.
2716 * Uses a static buffer, only the last error will be kept.
2717 * "msg" will be translated, caller should use N_().
2718 */
2719 char *
2720ex_errmsg(char *msg, char_u *arg)
2721{
2722 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2723 return ex_error_buf;
2724}
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726/*
zeertzjq7d664bf2024-07-14 10:22:54 +02002727 * The "+" string used in place of an empty command in Ex mode.
2728 * This string is used in pointer comparison.
2729 */
2730static char exmode_plus[] = "+";
2731
2732/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002733 * Handle a range without a command.
2734 * Returns an error message on failure.
2735 */
2736 char *
2737ex_range_without_command(exarg_T *eap)
2738{
2739 char *errormsg = NULL;
2740
zeertzjq7d664bf2024-07-14 10:22:54 +02002741 if ((*eap->cmd == '|' ||
2742 (exmode_active && eap->cmd != (char_u *)exmode_plus + 1))
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002743#ifdef FEAT_EVAL
2744 && !in_vim9script()
2745#endif
2746 )
2747 {
2748 eap->cmdidx = CMD_print;
2749 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2750 if ((errormsg = invalid_range(eap)) == NULL)
2751 {
2752 correct_range(eap);
2753 ex_print(eap);
2754 }
2755 }
2756 else if (eap->addr_count != 0)
2757 {
2758 if (eap->line2 > curbuf->b_ml.ml_line_count)
2759 {
2760 // With '-' in 'cpoptions' a line number past the file is an
2761 // error, otherwise put it at the end of the file.
2762 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2763 eap->line2 = -1;
2764 else
2765 eap->line2 = curbuf->b_ml.ml_line_count;
2766 }
2767
2768 if (eap->line2 < 0)
2769 errormsg = _(e_invalid_range);
2770 else
2771 {
2772 if (eap->line2 == 0)
2773 curwin->w_cursor.lnum = 1;
2774 else
2775 curwin->w_cursor.lnum = eap->line2;
2776 beginline(BL_SOL | BL_FIX);
2777 }
2778 }
2779 return errormsg;
2780}
2781
2782/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002783 * Check for an Ex command with optional tail.
2784 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002785 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002786 */
2787 static int
2788checkforcmd_opt(
2789 char_u **pp, // start of command
2790 char *cmd, // name of command
2791 int len, // required length
2792 int noparen)
2793{
2794 int i;
2795
2796 for (i = 0; cmd[i] != NUL; ++i)
2797 if (((char_u *)cmd)[i] != (*pp)[i])
2798 break;
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002799 if (i >= len && !ASCII_ISALPHA((*pp)[i]) && (*pp)[i] != '_'
Bram Moolenaar68854a82022-01-31 18:59:13 +00002800 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002801 {
2802 *pp = skipwhite(*pp + i);
2803 return TRUE;
2804 }
2805 return FALSE;
2806}
2807
2808/*
2809 * Check for an Ex command with optional tail.
2810 * If there is a match advance "pp" to the argument and return TRUE.
2811 */
2812 int
2813checkforcmd(
2814 char_u **pp, // start of command
2815 char *cmd, // name of command
2816 int len) // required length
2817{
2818 return checkforcmd_opt(pp, cmd, len, FALSE);
2819}
2820
2821/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002822 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002823 * If there is a match advance "pp" to the argument and return TRUE.
2824 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002825 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002826checkforcmd_noparen(
2827 char_u **pp, // start of command
2828 char *cmd, // name of command
2829 int len) // required length
2830{
2831 return checkforcmd_opt(pp, cmd, len, TRUE);
2832}
2833
2834/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 * Parse and skip over command modifiers:
2836 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002837 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002839 * When "skip_only" is TRUE the global variables are not changed, except for
2840 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002841 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2842 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002843 * Call apply_cmdmod() to get the side effects of the modifiers:
2844 * - Increment "sandbox" for ":sandbox"
2845 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002846 * - set msg_silent for ":silent"
2847 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 * Return FAIL when the command is not to be executed.
2849 * May set "errormsg" to an error message.
2850 */
2851 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002852parse_command_modifiers(
2853 exarg_T *eap,
2854 char **errormsg,
2855 cmdmod_T *cmod,
2856 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002857{
Bram Moolenaarf50808e2022-04-16 18:52:17 +01002858 char_u *orig_cmd = eap->cmd;
Bram Moolenaar565d1272022-03-27 18:11:05 +01002859 char_u *cmd_start = NULL;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002860 int use_plus_cmd = FALSE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002861 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002862 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002863 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002864
Bram Moolenaare1004402020-10-24 20:49:43 +02002865 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002866 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002868 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2869 {
2870 // The automatically inserted Visual area range is skipped, so that
2871 // typing ":cmdmod cmd" in Visual mode works without having to move the
zeertzjqd9be94c2024-07-14 10:20:20 +02002872 // range to after the modifiers. The command will be "'<,'>cmdmod cmd",
2873 // parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below.
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002874 eap->cmd += 5;
2875 cmd_start = eap->cmd;
2876 has_visual_range = TRUE;
2877 }
2878
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002879 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 for (;;)
2881 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002882 char_u *p;
2883
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002885 {
2886 if (*eap->cmd == ':')
2887 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002888 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002889 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002890
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002891 // in ex mode, an empty command (after modifiers) works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002892 if (*eap->cmd == NUL && exmode_active
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01002893 && (getline_equal(eap->ea_getline, eap->cookie, getexmodeline)
2894 || getline_equal(eap->ea_getline, eap->cookie, getexline))
Bram Moolenaareffed932018-08-14 13:38:17 +02002895 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2896 {
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002897 use_plus_cmd = TRUE;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002898 if (!skip_only)
2899 ex_pressedreturn = TRUE;
Bram Moolenaar48ce1352022-06-14 15:43:18 +01002900 break; // no modifiers following
Bram Moolenaareffed932018-08-14 13:38:17 +02002901 }
2902
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002903 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002904 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002905 {
2906 // a comment ends at a NL
zeertzjqf7b86092024-09-05 17:26:30 +02002907 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2908 if (eap->nextcmd != NULL)
2909 ++eap->nextcmd;
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002910 if (vim9script)
2911 {
2912 if (has_cmdmod(cmod, FALSE))
2913 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002914#ifdef FEAT_EVAL
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002915 if (eap->cmd[0] == '#' && eap->cmd[1] == '{'
2916 && eap->cmd[2] != '{')
2917 *errormsg = _(e_cannot_use_hash_curly_to_start_comment);
Bram Moolenaarda70cf32022-08-06 22:13:03 +01002918#endif
Bram Moolenaar3f74c0a2022-08-06 18:12:06 +01002919 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002920 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002921 }
zeertzjqf7b86092024-09-05 17:26:30 +02002922 if (*eap->cmd == '\n')
zeertzjq2432b4a2024-09-03 22:58:30 +02002923 {
2924 eap->nextcmd = eap->cmd + 1;
2925 return FAIL;
2926 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 if (*eap->cmd == NUL)
2928 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002929 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002930 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002931 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002932 if (vim9script && has_cmdmod(cmod, FALSE))
2933 *errormsg = _(e_command_modifier_without_command);
2934 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002935 return FAIL;
2936 }
2937
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002938 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002939
2940 // In Vim9 script a variable can shadow a command modifier:
2941 // verbose = 123
2942 // verbose += 123
2943 // silent! verbose = func()
2944 // verbose.member = 2
2945 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002946 // But not:
2947 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002948 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002949 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002950 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002951
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002952 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002953 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002954 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002955 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2956 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002957 break;
2958 }
2959
Bram Moolenaareffed932018-08-14 13:38:17 +02002960 switch (*p)
2961 {
John Marriotted908f72024-04-18 22:46:56 +02002962 // When adding an entry, also modify cmdmod_info_tab[].
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002963 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002964 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002965 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002966 continue;
2967
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002968 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002969 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002970 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 continue;
2972 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002973 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002974 {
2975#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002976 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002977#endif
2978 continue;
2979 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002980 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002981 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002982 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002983 continue;
2984
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002985 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002986 break;
2987#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989#endif
2990 continue;
2991
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002992 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002993 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002994 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002995 continue;
2996 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002997 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002998 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002999 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003000 continue;
3001 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003002 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003003 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003004 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003005 continue;
3006 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003007 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003008 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003009 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 continue;
3011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003012 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003013 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02003014 char_u *reg_pat;
3015 char_u *nulp = NULL;
3016 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02003017
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003018 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003019 || *p == NUL
3020 || (ends_excmd(*p)
3021#ifdef FEAT_EVAL
3022 // in ":filter #pat# cmd" # does not
3023 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00003024 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00003025#endif
3026 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 break;
3028 if (*p == '!')
3029 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003030 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003031 p = skipwhite(p + 1);
3032 if (*p == NUL || ends_excmd(*p))
3033 break;
3034 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003035#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003036 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00003037 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02003038 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02003039#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003040 if (skip_only)
3041 p = skip_vimgrep_pat(p, NULL, NULL);
3042 else
3043 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02003044 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
3045 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02003046 if (p == NULL || *p == NUL)
3047 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003048 if (!skip_only)
3049 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003050 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02003051 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02003052 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003053 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02003054 // restore the character overwritten by NUL
3055 if (nulp != NULL)
3056 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003057 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003058 eap->cmd = p;
3059 continue;
3060 }
3061
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003062 case 'h': if (checkforcmd_noparen(&eap->cmd, "horizontal", 3))
3063 {
3064 cmod->cmod_split |= WSP_HOR;
3065 continue;
3066 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003067 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaar21c3a802022-08-31 17:49:14 +01003068 if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02003069 || *p == NUL || ends_excmd(*p))
3070 break;
3071 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02003072 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003073 continue;
3074
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003075 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003076 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003077 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02003078 continue;
3079 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003080 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
3081 {
3082 if (ends_excmd2(p, eap->cmd))
3083 {
3084 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02003085 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02003086 return FAIL;
3087 }
3088 cmod->cmod_flags |= CMOD_LEGACY;
3089 continue;
3090 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003091
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003092 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02003093 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003094 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003095 continue;
3096
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003097 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003098 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003099 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003100 continue;
3101 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003102 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003103 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003104 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003105 continue;
3106
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003107 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003108 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003109 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003110 continue;
3111
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003112 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003113 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003114 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003115 continue;
3116 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003117 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003118 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003119 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003120 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3121 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003122 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003123 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003124 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003125 }
3126 continue;
3127
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003128 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003129 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003130 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003131 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003132 long tabnr = get_address(eap, &eap->cmd,
3133 ADDR_TABS, eap->skip,
3134 skip_only, FALSE, 1);
3135 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003136 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003137 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003138 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003139 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3140 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003141 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003142 return FAIL;
3143 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003144 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003145 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003146 }
3147 eap->cmd = p;
3148 continue;
3149 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003150 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003151 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003152 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003153 continue;
3154
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003155 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003156 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003157 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003158 continue;
3159
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003160 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003161 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003162 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003163 continue;
3164 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003165 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003166 {
3167 if (ends_excmd2(p, eap->cmd))
3168 {
3169 *errormsg =
3170 _(e_vim9cmd_must_be_followed_by_command);
3171 return FAIL;
3172 }
3173 cmod->cmod_flags |= CMOD_VIM9CMD;
3174 continue;
3175 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003176 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003177 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003178 if (vim_isdigit(*eap->cmd))
Bram Moolenaar60895f32022-04-12 14:23:19 +01003179 {
zeertzjqcd749632022-06-14 13:30:35 +01003180 // zero means not set, one is verbose == 0, etc.
3181 cmod->cmod_verbose = atoi((char *)eap->cmd) + 1;
Bram Moolenaar60895f32022-04-12 14:23:19 +01003182 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003183 else
zeertzjqcd749632022-06-14 13:30:35 +01003184 cmod->cmod_verbose = 2; // default: verbose == 1
Bram Moolenaareffed932018-08-14 13:38:17 +02003185 eap->cmd = p;
3186 continue;
3187 }
3188 break;
3189 }
3190
Bram Moolenaar1501b632022-03-27 16:56:21 +01003191 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003192 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003193 if (eap->cmd > cmd_start)
3194 {
3195 // Move the '<,'> range to after the modifiers and insert a colon.
3196 // Since the modifiers have been parsed put the colon on top of the
3197 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3198 // Put eap->cmd after the colon.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003199 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003200 {
3201 size_t len = STRLEN(cmd_start);
3202
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003203 // Special case: empty command uses "+":
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003204 // "'<,'>mods" -> "mods *+
3205 // Use "*" instead of "'<,'>" to avoid the command getting
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003206 // longer, in case it was allocated.
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003207 mch_memmove(orig_cmd, cmd_start, len);
Bram Moolenaarc6fdb152022-07-02 13:43:21 +01003208 STRCPY(orig_cmd + len, " *+");
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003209 }
3210 else
3211 {
3212 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3213 eap->cmd -= 5;
3214 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3215 }
Bram Moolenaar1501b632022-03-27 16:56:21 +01003216 }
3217 else
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003218 // No modifiers, move the pointer back.
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003219 // Special case: change empty command to "+".
3220 if (use_plus_cmd)
Bram Moolenaarf50808e2022-04-16 18:52:17 +01003221 eap->cmd = (char_u *)"'<,'>+";
3222 else
3223 eap->cmd = orig_cmd;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003224 }
Bram Moolenaar48ce1352022-06-14 15:43:18 +01003225 else if (use_plus_cmd)
zeertzjq7d664bf2024-07-14 10:22:54 +02003226 eap->cmd = (char_u *)exmode_plus;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003227
Bram Moolenaareffed932018-08-14 13:38:17 +02003228 return OK;
3229}
3230
3231/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003232 * Return TRUE if "cmod" has anything set.
3233 */
3234 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003235has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003236{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003237 return (cmod->cmod_flags != 0 && (!ignore_silent
3238 || (cmod->cmod_flags
3239 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003240 || cmod->cmod_split != 0
zeertzjqcd749632022-06-14 13:30:35 +01003241 || cmod->cmod_verbose > 0
Bram Moolenaarfa984412021-03-25 22:15:28 +01003242 || cmod->cmod_tab != 0
3243 || cmod->cmod_filter_regmatch.regprog != NULL;
3244}
3245
Bram Moolenaar8991be22022-02-14 21:51:46 +00003246#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003247/*
3248 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3249 */
3250 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003251cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003252{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003253 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003254 {
3255 emsg(_(e_misplaced_command_modifier));
3256 return TRUE;
3257 }
3258 return FALSE;
3259}
Dominique Pelle748b3082022-01-08 12:41:16 +00003260#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003261
3262/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003263 * Apply the command modifiers. Saves current state in "cmdmod", call
3264 * undo_cmdmod() later.
3265 */
3266 void
3267apply_cmdmod(cmdmod_T *cmod)
3268{
3269#ifdef HAVE_SANDBOX
3270 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3271 {
3272 ++sandbox;
3273 cmod->cmod_did_sandbox = TRUE;
3274 }
3275#endif
zeertzjqcd749632022-06-14 13:30:35 +01003276 if (cmod->cmod_verbose > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003277 {
3278 if (cmod->cmod_verbose_save == 0)
3279 cmod->cmod_verbose_save = p_verbose + 1;
zeertzjqcd749632022-06-14 13:30:35 +01003280 p_verbose = cmod->cmod_verbose - 1;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003281 }
3282
3283 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3284 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003285 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003286 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003287 cmod->cmod_save_msg_scroll = msg_scroll;
3288 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003289 if (cmod->cmod_flags & CMOD_SILENT)
3290 ++msg_silent;
3291 if (cmod->cmod_flags & CMOD_UNSILENT)
3292 msg_silent = 0;
3293
3294 if (cmod->cmod_flags & CMOD_ERRSILENT)
3295 {
3296 ++emsg_silent;
3297 ++cmod->cmod_did_esilent;
3298 }
3299
Bram Moolenaare1004402020-10-24 20:49:43 +02003300 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003301 {
3302 // Set 'eventignore' to "all".
3303 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003304 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003305 set_string_option_direct((char_u *)"ei", -1,
3306 (char_u *)"all", OPT_FREE, SID_NONE);
3307 }
3308}
3309
3310/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003311 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003312 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003313 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003314undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003315{
Bram Moolenaare1004402020-10-24 20:49:43 +02003316 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003317 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003318 p_verbose = cmod->cmod_verbose_save - 1;
3319 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003320 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003321
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003322#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003323 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003324 {
3325 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003326 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003327 }
3328#endif
3329
Bram Moolenaare1004402020-10-24 20:49:43 +02003330 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003332 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003333 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3334 OPT_FREE, SID_NONE);
3335 free_string_option(cmod->cmod_save_ei);
3336 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003337 }
3338
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003339 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003340
Bram Moolenaare1004402020-10-24 20:49:43 +02003341 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003342 {
3343 // messages could be enabled for a serious error, need to check if the
3344 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003345 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3346 msg_silent = cmod->cmod_save_msg_silent - 1;
3347 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003348 if (emsg_silent < 0)
3349 emsg_silent = 0;
3350 // Restore msg_scroll, it's set by file I/O commands, even when no
3351 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003352 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003353
3354 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3355 // somewhere in the line. Put it back in the first column.
3356 if (redirecting())
3357 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003358
Bram Moolenaare1004402020-10-24 20:49:43 +02003359 cmod->cmod_save_msg_silent = 0;
3360 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003361 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003362}
3363
3364/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003365 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003366 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003367 * Return FAIL and set "errormsg" or return OK.
3368 */
3369 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003370parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003371{
3372 int address_count = 1;
3373 linenr_T lnum;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003374 int need_check_cursor = FALSE;
3375 int ret = FAIL;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003376
3377 // Repeat for all ',' or ';' separated addresses.
3378 for (;;)
3379 {
3380 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003381 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003382 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003383 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003384 eap->addr_count == 0, address_count++);
3385 if (eap->cmd == NULL) // error detected
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003386 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003387 if (lnum == MAXLNUM)
3388 {
3389 if (*eap->cmd == '%') // '%' - all lines
3390 {
3391 ++eap->cmd;
3392 switch (eap->addr_type)
3393 {
3394 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003395 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003396 eap->line1 = 1;
3397 eap->line2 = curbuf->b_ml.ml_line_count;
3398 break;
3399 case ADDR_LOADED_BUFFERS:
3400 {
3401 buf_T *buf = firstbuf;
3402
3403 while (buf->b_next != NULL
3404 && buf->b_ml.ml_mfp == NULL)
3405 buf = buf->b_next;
3406 eap->line1 = buf->b_fnum;
3407 buf = lastbuf;
3408 while (buf->b_prev != NULL
3409 && buf->b_ml.ml_mfp == NULL)
3410 buf = buf->b_prev;
3411 eap->line2 = buf->b_fnum;
3412 break;
3413 }
3414 case ADDR_BUFFERS:
3415 eap->line1 = firstbuf->b_fnum;
3416 eap->line2 = lastbuf->b_fnum;
3417 break;
3418 case ADDR_WINDOWS:
3419 case ADDR_TABS:
3420 if (IS_USER_CMDIDX(eap->cmdidx))
3421 {
3422 eap->line1 = 1;
3423 eap->line2 = eap->addr_type == ADDR_WINDOWS
3424 ? LAST_WIN_NR : LAST_TAB_NR;
3425 }
3426 else
3427 {
3428 // there is no Vim command which uses '%' and
3429 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003430 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003431 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003432 }
3433 break;
3434 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003435 case ADDR_UNSIGNED:
3436 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003437 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003438 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003439 case ADDR_ARGUMENTS:
3440 if (ARGCOUNT == 0)
3441 eap->line1 = eap->line2 = 0;
3442 else
3443 {
3444 eap->line1 = 1;
3445 eap->line2 = ARGCOUNT;
3446 }
3447 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003448 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003449#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003450 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003451 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003452 if (eap->line2 == 0)
3453 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003454#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003455 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003456 case ADDR_NONE:
3457 // Will give an error later if a range is found.
3458 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003459 }
3460 ++eap->addr_count;
3461 }
3462 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3463 {
3464 pos_T *fp;
3465
3466 // '*' - visual area
3467 if (eap->addr_type != ADDR_LINES)
3468 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003469 *errormsg = _(e_invalid_range);
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003470 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003471 }
3472
3473 ++eap->cmd;
3474 if (!eap->skip)
3475 {
3476 fp = getmark('<', FALSE);
3477 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003478 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003479 eap->line1 = fp->lnum;
3480 fp = getmark('>', FALSE);
3481 if (check_mark(fp) == FAIL)
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003482 goto theend;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003483 eap->line2 = fp->lnum;
3484 ++eap->addr_count;
3485 }
3486 }
3487 }
3488 else
3489 eap->line2 = lnum;
3490 eap->addr_count++;
3491
3492 if (*eap->cmd == ';')
3493 {
3494 if (!eap->skip)
3495 {
3496 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003497
Bram Moolenaar0e717042020-04-27 19:29:01 +02003498 // Don't leave the cursor on an illegal line or column, but do
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003499 // accept zero as address, so 0;/PATTERN/ works correctly
3500 // (where zero usually means to use the first line).
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003501 // Check the cursor position before returning.
Bram Moolenaar0e717042020-04-27 19:29:01 +02003502 if (eap->line2 > 0)
3503 check_cursor();
Bram Moolenaarf7c7c3f2022-06-22 19:08:38 +01003504 else
3505 check_cursor_col();
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003506 need_check_cursor = TRUE;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003507 }
3508 }
3509 else if (*eap->cmd != ',')
3510 break;
3511 ++eap->cmd;
3512 }
3513
3514 // One address given: set start and end lines.
3515 if (eap->addr_count == 1)
3516 {
3517 eap->line1 = eap->line2;
3518 // ... but only implicit: really no address given
3519 if (lnum == MAXLNUM)
3520 eap->addr_count = 0;
3521 }
Bram Moolenaar4d97a562022-05-28 14:25:35 +01003522 ret = OK;
3523
3524theend:
3525 if (need_check_cursor)
3526 check_cursor();
3527 return ret;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003528}
3529
3530/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003531 * Append "cmd" to the error message in IObuff.
3532 * Takes care of limiting the length and handling 0xa0, which would be
3533 * invisible otherwise.
3534 */
3535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003536append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003537{
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003538 size_t len = STRLEN(IObuff);
3539 char_u *s = cmd;
3540 char_u *d;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003541
Bram Moolenaar44a3f332022-06-06 15:38:21 +01003542 if (len > IOSIZE - 100)
3543 {
3544 // Not enough space, truncate and put in "...".
3545 d = IObuff + IOSIZE - 100;
3546 d -= mb_head_off(IObuff, d);
3547 STRCPY(d, "...");
3548 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003549 STRCAT(IObuff, ": ");
3550 d = IObuff + STRLEN(IObuff);
Bram Moolenaard8893442022-05-06 20:38:47 +01003551 while (*s != NUL && d - IObuff + 5 < IOSIZE)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003552 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003553 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003554 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003555 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003556 STRCPY(d, "<a0>");
3557 d += 4;
3558 }
Bram Moolenaard8893442022-05-06 20:38:47 +01003559 else if (d - IObuff + (*mb_ptr2len)(s) + 1 >= IOSIZE)
3560 break;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003561 else
3562 MB_COPY_CHAR(s, d);
3563 }
3564 *d = NUL;
3565}
3566
Dominique Pelle748b3082022-01-08 12:41:16 +00003567#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003568/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003569 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3570 * the name. Otherwise just return "start".
3571 */
3572 char_u *
3573skip_option_env_lead(char_u *start)
3574{
3575 char_u *name = start;
3576
3577 if (*start == '&')
3578 {
3579 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3580 name += 3;
3581 else
3582 name += 1;
3583 }
3584 else if (*start == '$')
3585 name += 1;
3586 return name;
3587}
Dominique Pelle748b3082022-01-08 12:41:16 +00003588#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003589
3590/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003591 * Return TRUE and set "*idx" if "p" points to a one letter command.
3592 * If not in Vim9 script:
Doug Kearnsea842022024-09-29 17:17:41 +02003593 * - The 'k' command can directly be followed by any character
3594 * but :keepa[lt] is another command, as are :keepj[umps],
3595 * :kee[pmarks] and :keepp[atterns].
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003596 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3597 * but :sre[wind] is another command, as are :scr[iptnames],
3598 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3599 */
3600 static int
3601one_letter_cmd(char_u *p, cmdidx_T *idx)
3602{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003603 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003604 return FALSE;
Doug Kearnsea842022024-09-29 17:17:41 +02003605 if (p[0] == 'k'
3606 && (p[1] != 'e' || (p[1] == 'e' && p[2] != 'e')))
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003607 {
3608 *idx = CMD_k;
3609 return TRUE;
3610 }
3611 if (p[0] == 's'
3612 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3613 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3614 || p[1] == 'g'
3615 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3616 || p[1] == 'I'
3617 || (p[1] == 'r' && p[2] != 'e')))
3618 {
3619 *idx = CMD_substitute;
3620 return TRUE;
3621 }
3622 return FALSE;
3623}
3624
Dominique Pellee764d1b2023-03-12 21:20:59 +00003625#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003626/*
Bram Moolenaar31d99482022-05-26 22:24:43 +01003627 * Return TRUE if "cmd" starts with "123->", a number followed by a method
3628 * call.
3629 */
3630 int
3631number_method(char_u *cmd)
3632{
3633 char_u *p = skipdigits(cmd);
3634
3635 return p > cmd && (p = skipwhite(p))[0] == '-' && p[1] == '>';
3636}
Dominique Pellee764d1b2023-03-12 21:20:59 +00003637#endif
Bram Moolenaar31d99482022-05-26 22:24:43 +01003638
3639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003641 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003642 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003643 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 *
3645 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3646 * assignment without "let". Sets eap->cmdidx to the command while returning
3647 * "eap->cmd".
3648 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 * Returns NULL for an ambiguous user command.
3650 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 char_u *
3652find_ex_command(
3653 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003654 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003655 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003656 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 int len;
3659 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003660 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003661#ifndef FEAT_EVAL
3662 int vim9 = FALSE;
3663#else
3664 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 /*
3667 * Recognize a Vim9 script function/method call and assignment:
3668 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3669 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003670 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003671 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003673 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003675 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003676 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3677 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003679 int oplen;
3680 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003681 char_u *swp;
3682
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003683 if (*eap->cmd == '&'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003684 || (eap->cmd[0] == '$'
3685 && eap->cmd[1] != '\'' && eap->cmd[1] != '"')
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003686 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003687 && (valid_yank_reg(eap->cmd[1], FALSE)
3688 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003689 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003690 if (*eap->cmd == '&')
3691 {
3692 p = eap->cmd + 1;
3693 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3694 p += 2;
3695 p = to_name_end(p, FALSE);
3696 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003697 else if (*eap->cmd == '$')
3698 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003699 else
3700 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003701 if (ends_excmd(*skipwhite(p)))
3702 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003703 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3704 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003705 eap->cmdidx = CMD_eval;
3706 return eap->cmd;
3707 }
3708 // "&option" can be followed by "->" or "=", check below
3709 }
3710
3711 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003713 if (
3714 // "(..." is an expression.
3715 // "funcname(" is always a function call.
3716 *p == '('
3717 || (p == eap->cmd
3718 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003719 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003720 *eap->cmd == '{'
3721 // "'string'->func()" is an expression.
3722 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003723 // '"string"->func()' is an expression.
3724 || *eap->cmd == '"'
Bram Moolenaar40c141d2022-05-17 13:14:23 +01003725 // '$"string"->func()' is an expression.
3726 // "$'string'->func()" is an expression.
3727 || (eap->cmd[0] == '$'
3728 && (eap->cmd[1] == '\'' || eap->cmd[1] == '"'))
3729 // '0z1234->func()' is an expression.
3730 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003731 // "g:varname" is an expression.
3732 || eap->cmd[1] == ':'
3733 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003734 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003735 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003736 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003737 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3738 {
3739 // "{" by itself is the start of a block.
3740 eap->cmdidx = CMD_block;
3741 return eap->cmd + 1;
3742 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003743 eap->cmdidx = CMD_eval;
3744 return eap->cmd;
3745 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003746
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003747 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003748 // "varname[]" is an expression.
3749 *p == '['
3750 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003751 || (*p == '.'
3752 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3753 // g:[key] is an expression
3754 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003755 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003756 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003757
3758 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003759 // Skip over the whole thing, it can be:
3760 // name.member = val
3761 // name[a : b] = val
3762 // name[idx] = val
3763 // name[idx].member = val
3764 // etc.
3765 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003766 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003767 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003768 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003769 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003770 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003771 || (after[0] == '.' && after[1] == '.'
3772 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003773 eap->cmdidx = CMD_var;
3774 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003775 --emsg_silent;
3776 return eap->cmd;
3777 }
3778
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003779 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3780 // an assignment.
3781 // If there is no line break inside the "[...]" then "p" is
3782 // advanced to after the "]" by to_name_const_end(): check if a "="
3783 // follows.
3784 // If "[...]" has a line break "p" still points at the "[" and it
3785 // can't be an assignment.
3786 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003787 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003788 char_u *eq;
3789
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003790 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003791 if (p == eap->cmd && *p == '[')
3792 {
3793 int count = 0;
3794 int semicolon = FALSE;
3795
3796 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3797 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003798 eq = p;
3799 if (eq != NULL)
3800 {
3801 eq = skipwhite(eq);
3802 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3803 ++eq;
3804 }
3805 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003806 {
3807 eap->cmdidx = CMD_eval;
3808 return eap->cmd;
3809 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003810 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003811 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003812 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003813 return eap->cmd;
3814 }
3815 }
3816
3817 // Recognize an assignment if we recognize the variable name:
3818 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003819 // "@r = expr"
3820 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003821 // "var = expr" where "var" is a variable name or we are skipping
3822 // (variable declaration might have been skipped).
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003823 // Not "redir => var" (when skipping).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003824 oplen = assignment_len(skipwhite(p), &heredoc);
3825 if (oplen > 0)
3826 {
3827 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3828 || *eap->cmd == '&'
3829 || *eap->cmd == '$'
3830 || *eap->cmd == '@'
Bram Moolenaarbc6f2bf2022-05-06 14:29:09 +01003831 || (eap->skip && IS_WHITE_OR_NUL(
3832 *(skipwhite(p) + oplen)))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003833 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003834 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003835 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003836 return eap->cmd;
3837 }
3838 }
3839
Bram Moolenaar9510d222022-09-11 15:14:05 +01003840 // Recognize trying to use a type for a w:, b:, t: or g: variable:
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003841 // "w:varname: number = 123".
3842 if (eap->cmd[1] == ':' && *p == ':')
3843 {
Bram Moolenaar9510d222022-09-11 15:14:05 +01003844 eap->cmdidx = CMD_var;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003845 return eap->cmd;
3846 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003847 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003848
Bram Moolenaar31d99482022-05-26 22:24:43 +01003849 // 1234->func() is a method call
3850 if (number_method(eap->cmd))
3851 {
3852 eap->cmdidx = CMD_eval;
3853 return eap->cmd;
3854 }
3855
Bram Moolenaar7b829262021-10-13 15:04:34 +01003856 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3857 // an expression. A global/substitute/list command needs to use a
3858 // longer name.
3859 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3860 {
3861 eap->cmdidx = CMD_eval;
3862 return eap->cmd;
3863 }
3864
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003865 // If it is an ID it might be a variable with an operator on the next
3866 // line, if the variable exists it can't be an Ex command.
3867 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003868 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003869 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3870 {
3871 eap->cmdidx = CMD_eval;
3872 return eap->cmd;
3873 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003874
3875 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003876 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3877 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003878 {
3879 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3880 return eap->cmd + 2;
3881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 }
3883#endif
3884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 /*
3886 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 */
3888 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003889 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 ++p;
Doug Kearnsea842022024-09-29 17:17:41 +02003892 if (full != NULL)
3893 *full = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895 else
3896 {
3897 while (ASCII_ISALPHA(*p))
3898 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003899 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003900 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003902 while (ASCII_ISALNUM(*p))
3903 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 }
3905 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3906 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003907 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 ++p;
3909 while (ASCII_ISALPHA(*p))
3910 ++p;
3911 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003913 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003914 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 ++p;
3916 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003917 // The "d" command can directly be followed by 'l' or 'p' flag, when
3918 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003919 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003920 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003921 // Check for ":dl", ":dell", etc. to ":deletel": that's
3922 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003923 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003924 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003925 break;
3926 if (i == len - 1)
3927 {
3928 --len;
3929 if (p[-1] == 'l')
3930 eap->flags |= EXFLAG_LIST;
3931 else
3932 eap->flags |= EXFLAG_PRINT;
3933 }
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003936 if (ASCII_ISLOWER(eap->cmd[0]))
3937 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003938 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003939 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003940
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003941 if (command_count != (int)CMD_SIZE)
3942 {
RestorerZ68ebcee2023-05-31 17:12:14 +01003943 iemsg(e_command_table_needs_to_be_updated_run_make_cmdidxs);
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003944 getout(1);
3945 }
3946
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003947 // Use a precomputed index for fast look-up in cmdnames[]
3948 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003949 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3950 if (ASCII_ISLOWER(c2))
3951 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3952 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003953 else if (ASCII_ISUPPER(eap->cmd[0]))
3954 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003956 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
3958 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3959 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3960 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3961 (size_t)len) == 0)
3962 {
3963#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003964 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 *full = TRUE;
3966#endif
3967 break;
3968 }
3969
Bram Moolenaar204852a2022-03-05 12:56:44 +00003970 // :Print and :mode are not supported in Vim9 script.
3971 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003972 if (vim9 && eap->cmdidx != CMD_SIZE)
3973 {
3974 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3975 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003976 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
John Marriotted908f72024-04-18 22:46:56 +02003977 && len < (int)cmdnames[eap->cmdidx].cmd_namelen)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003978 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003979 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003980 eap->cmdidx = CMD_SIZE;
3981 }
3982 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003983
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003984 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003985 // 'cpoptions'.
3986 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3987 p = eap->cmd;
3988
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003989 // Look for a user defined command as a last resort. Let ":Print" be
3990 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003991 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3992 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003994 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 while (ASCII_ISALNUM(*p))
3996 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003997 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003999 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 eap->cmdidx = CMD_SIZE;
4001 }
4002
Bram Moolenaar204852a2022-03-05 12:56:44 +00004003 // ":fina" means ":finally" in legacy script, for backwards compatibility.
4004 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02004005 eap->cmdidx = CMD_finally;
4006
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004007#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004008 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004009 && vim9
Christian Brabandt00cb2472023-09-05 20:46:25 +02004010 && !IS_WHITE_NL_OR_NUL(*p) && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004011 && (eap->cmdidx < 0 ||
4012 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004013 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004014 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
4015
4016 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004017 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02004018 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004019 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01004020#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01004021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 return p;
4023}
4024
4025#if defined(FEAT_EVAL) || defined(PROTO)
John Marriotted908f72024-04-18 22:46:56 +02004026typedef struct
Bram Moolenaared53fb92007-11-24 20:50:24 +00004027{
4028 char *name;
4029 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004030 int has_count; // :123verbose :3tab
John Marriotted908f72024-04-18 22:46:56 +02004031} cmdmod_info_T;
4032
4033static cmdmod_info_T cmdmod_info_tab[] = {
Bram Moolenaared53fb92007-11-24 20:50:24 +00004034 {"aboveleft", 3, FALSE},
4035 {"belowright", 3, FALSE},
4036 {"botright", 2, FALSE},
4037 {"browse", 3, FALSE},
4038 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02004039 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004040 {"hide", 3, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004041 {"horizontal", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004042 {"keepalt", 5, FALSE},
4043 {"keepjumps", 5, FALSE},
4044 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01004045 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004046 {"leftabove", 5, FALSE},
Bram Moolenaar91324262022-09-09 13:27:59 +01004047 {"legacy", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004048 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00004049 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01004050 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004051 {"rightbelow", 6, FALSE},
4052 {"sandbox", 3, FALSE},
4053 {"silent", 3, FALSE},
4054 {"tab", 3, TRUE},
4055 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00004056 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00004057 {"verbose", 4, TRUE},
4058 {"vertical", 4, FALSE},
John Marriotted908f72024-04-18 22:46:56 +02004059 {"vim9cmd", 4, FALSE}
4060}; // cmdmod_info_tab
4061
Bram Moolenaared53fb92007-11-24 20:50:24 +00004062/*
4063 * Return length of a command modifier (including optional count).
4064 * Return zero when it's not a modifier.
4065 */
4066 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004067modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004068{
Christian Brabandt70a11a62024-07-26 19:13:55 +02004069 int i, j;
Bram Moolenaared53fb92007-11-24 20:50:24 +00004070 char_u *p = cmd;
4071
4072 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004073 p = skipwhite(skipdigits(cmd + 1));
Christian Brabandt70a11a62024-07-26 19:13:55 +02004074 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00004075 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004076 for (j = 0; p[j] != NUL; ++j)
4077 if (p[j] != cmdmod_info_tab[i].name[j])
Bram Moolenaared53fb92007-11-24 20:50:24 +00004078 break;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004079 if (!ASCII_ISALPHA(p[j]) && j >= cmdmod_info_tab[i].minlen
4080 && (p == cmd || cmdmod_info_tab[i].has_count))
4081 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00004082 }
4083 return 0;
4084}
4085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086/*
4087 * Return > 0 if an Ex command "name" exists.
4088 * Return 2 if there is an exact match.
4089 * Return 3 if there is an ambiguous match.
4090 */
4091 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004092cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093{
4094 exarg_T ea;
4095 int full = FALSE;
Christian Brabandt70a11a62024-07-26 19:13:55 +02004096 int i;
4097 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004098 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
Christian Brabandt70a11a62024-07-26 19:13:55 +02004100 // Check command modifiers.
4101 for (i = 0; i < (int)ARRAY_LENGTH(cmdmod_info_tab); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
Christian Brabandt70a11a62024-07-26 19:13:55 +02004103 for (j = 0; name[j] != NUL; ++j)
4104 if (name[j] != cmdmod_info_tab[i].name[j])
4105 break;
4106 if (name[j] == NUL && j >= cmdmod_info_tab[i].minlen)
4107 return (cmdmod_info_tab[i].name[j] == NUL ? 2 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004110 // Check built-in commands and user defined commands.
4111 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00004112 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00004114 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004116 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00004118 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
4119 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00004120 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004121 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
4123}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004124
4125/*
4126 * "fullcommand" function
4127 */
4128 void
4129f_fullcommand(typval_T *argvars, typval_T *rettv)
4130{
Bram Moolenaaraa534142022-09-15 21:46:02 +01004131 exarg_T ea;
4132 char_u *name;
4133 char_u *p;
4134 int vim9script = in_vim9script();
4135 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004136
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004137 rettv->v_type = VAR_STRING;
4138 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004139
Bram Moolenaaraa534142022-09-15 21:46:02 +01004140 if (in_vim9script()
4141 && (check_for_string_arg(argvars, 0) == FAIL
4142 || check_for_opt_bool_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004143 return;
4144
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02004145 name = tv_get_string(&argvars[0]);
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02004146 if (name == NULL)
4147 return;
4148
Bram Moolenaaraa534142022-09-15 21:46:02 +01004149 if (argvars[1].v_type != VAR_UNKNOWN)
4150 {
4151 vim9script = tv_get_bool(&argvars[1]);
4152 cmdmod.cmod_flags &= ~(CMOD_VIM9CMD | CMOD_LEGACY);
4153 cmdmod.cmod_flags |= vim9script ? CMOD_VIM9CMD : CMOD_LEGACY;
4154 }
4155
zeertzjqc024ed92022-01-04 16:22:52 +00004156 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004157 name++;
4158 name = skip_range(name, TRUE, NULL);
4159
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004160 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
4161 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004162 ea.addr_count = 0;
Bram Moolenaaraa534142022-09-15 21:46:02 +01004163 ++emsg_silent; // don't complain about using "en" in Vim9 script
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004164 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004165 --emsg_silent;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004166 if (p == NULL || ea.cmdidx == CMD_SIZE)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004167 goto theend;
4168
4169 if (vim9script)
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004170 {
4171 int res;
4172
4173 ++emsg_silent;
4174 res = not_in_vim9(&ea);
4175 --emsg_silent;
4176
4177 if (res == FAIL)
Bram Moolenaaraa534142022-09-15 21:46:02 +01004178 goto theend;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02004179 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004180
4181 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02004182 ? get_user_command_name(ea.useridx, ea.cmdidx)
4183 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaaraa534142022-09-15 21:46:02 +01004184theend:
4185 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01004186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187#endif
4188
Bram Moolenaard0190392019-08-23 21:17:35 +02004189 cmdidx_T
4190excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
Bram Moolenaard0190392019-08-23 21:17:35 +02004192 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004194 if (!one_letter_cmd(cmd, &idx))
4195 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4196 idx = (cmdidx_T)((int)idx + 1))
4197 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaard0190392019-08-23 21:17:35 +02004200 return idx;
4201}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
Bram Moolenaard0190392019-08-23 21:17:35 +02004203 long
4204excmd_get_argt(cmdidx_T idx)
4205{
4206 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207}
4208
4209/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004210 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 *
4212 * Backslashed delimiters after / or ? will be skipped, and commands will
4213 * not be expanded between /'s and ?'s or after "'".
4214 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004215 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 * Returns the "cmd" pointer advanced to beyond the range.
4217 */
4218 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004219skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004220 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004221 int skip_star, // skip "*" used for Visual range
4222 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004224 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004225 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004227 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004229 if (*cmd == '\\')
4230 {
4231 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4232 ++cmd;
4233 else
4234 break;
4235 }
4236 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004238 char_u *p = cmd;
4239
4240 // a quote is only valid at the start or after a separator
4241 while (p > cmd_start)
4242 {
4243 --p;
4244 if (!VIM_ISWHITE(*p))
4245 break;
4246 }
4247 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4248 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (*++cmd == NUL && ctx != NULL)
4250 *ctx = EXPAND_NOTHING;
4251 }
4252 else if (*cmd == '/' || *cmd == '?')
4253 {
4254 delim = *cmd++;
4255 while (*cmd != NUL && *cmd != delim)
4256 if (*cmd++ == '\\' && *cmd != NUL)
4257 ++cmd;
4258 if (*cmd == NUL && ctx != NULL)
4259 *ctx = EXPAND_NOTHING;
4260 }
4261 if (*cmd != NUL)
4262 ++cmd;
4263 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004265 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004266 while (*cmd == ':')
4267 cmd = skipwhite(cmd + 1);
4268
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004269 // Skip "*" used for Visual range.
4270 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4271 cmd = skipwhite(cmd + 1);
4272
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 return cmd;
4274}
4275
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004276 static void
4277addr_error(cmd_addr_T addr_type)
4278{
4279 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004280 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004281 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004282 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004283}
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004286 * Return the default address for an address type.
4287 */
4288 static linenr_T
4289default_address(exarg_T *eap)
4290{
4291 linenr_T lnum = 0;
4292
4293 switch (eap->addr_type)
4294 {
4295 case ADDR_LINES:
4296 case ADDR_OTHER:
4297 // Default is the cursor line number. Avoid using an invalid
4298 // line number though.
4299 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4300 lnum = curbuf->b_ml.ml_line_count;
4301 else
4302 lnum = curwin->w_cursor.lnum;
4303 break;
4304 case ADDR_WINDOWS:
4305 lnum = CURRENT_WIN_NR;
4306 break;
4307 case ADDR_ARGUMENTS:
4308 lnum = curwin->w_arg_idx + 1;
4309 if (lnum > ARGCOUNT)
4310 lnum = ARGCOUNT;
4311 break;
4312 case ADDR_LOADED_BUFFERS:
4313 case ADDR_BUFFERS:
4314 lnum = curbuf->b_fnum;
4315 break;
4316 case ADDR_TABS:
4317 lnum = CURRENT_TAB_NR;
4318 break;
4319 case ADDR_TABS_RELATIVE:
4320 case ADDR_UNSIGNED:
4321 lnum = 1;
4322 break;
4323 case ADDR_QUICKFIX:
4324#ifdef FEAT_QUICKFIX
4325 lnum = qf_get_cur_idx(eap);
4326#endif
4327 break;
4328 case ADDR_QUICKFIX_VALID:
4329#ifdef FEAT_QUICKFIX
4330 lnum = qf_get_cur_valid_idx(eap);
4331#endif
4332 break;
4333 case ADDR_NONE:
4334 // Will give an error later if a range is found.
4335 break;
4336 }
4337 return lnum;
4338}
4339
4340/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004341 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 *
4343 * Set ptr to the next character after the part that was interpreted.
4344 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004345 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 *
4347 * Return MAXLNUM when no Ex address was found.
4348 */
4349 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004350get_address(
4351 exarg_T *eap UNUSED,
4352 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004353 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004354 int skip, // only skip the address, don't use it
4355 int silent, // no errors or side effects
4356 int to_other_file, // flag: may jump to other file
4357 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358{
4359 int c;
4360 int i;
4361 long n;
4362 char_u *cmd;
4363 pos_T pos;
4364 pos_T *fp;
4365 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004366 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 cmd = skipwhite(*ptr);
4369 lnum = MAXLNUM;
4370 do
4371 {
4372 switch (*cmd)
4373 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004374 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004375 ++cmd;
4376 switch (addr_type)
4377 {
4378 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004379 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 lnum = curwin->w_cursor.lnum;
4381 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004382 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004383 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004384 break;
4385 case ADDR_ARGUMENTS:
4386 lnum = curwin->w_arg_idx + 1;
4387 break;
4388 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004389 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004390 lnum = curbuf->b_fnum;
4391 break;
4392 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004393 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004395 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004396 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004397 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004398 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004399 cmd = NULL;
4400 goto error;
4401 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004402 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004403#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004404 lnum = qf_get_cur_idx(eap);
4405#endif
4406 break;
4407 case ADDR_QUICKFIX_VALID:
4408#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004409 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004410#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004411 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004412 }
4413 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004415 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 ++cmd;
4417 switch (addr_type)
4418 {
4419 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004420 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 lnum = curbuf->b_ml.ml_line_count;
4422 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004424 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004425 break;
4426 case ADDR_ARGUMENTS:
4427 lnum = ARGCOUNT;
4428 break;
4429 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004430 buf = lastbuf;
4431 while (buf->b_ml.ml_mfp == NULL)
4432 {
4433 if (buf->b_prev == NULL)
4434 break;
4435 buf = buf->b_prev;
4436 }
4437 lnum = buf->b_fnum;
4438 break;
4439 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004440 lnum = lastbuf->b_fnum;
4441 break;
4442 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004443 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004445 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004446 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004447 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004448 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004449 cmd = NULL;
4450 goto error;
4451 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004452 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004453#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004454 lnum = qf_get_size(eap);
4455 if (lnum == 0)
4456 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004457#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004458 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004459 case ADDR_QUICKFIX_VALID:
4460#ifdef FEAT_QUICKFIX
4461 lnum = qf_get_valid_size(eap);
4462 if (lnum == 0)
4463 lnum = 1;
4464#endif
4465 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 }
4467 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004469 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 if (*++cmd == NUL)
4471 {
4472 cmd = NULL;
4473 goto error;
4474 }
4475 if (addr_type != ADDR_LINES)
4476 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004477 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004478 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 goto error;
4480 }
4481 if (skip)
4482 ++cmd;
4483 else
4484 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004485 // Only accept a mark in another file when it is
4486 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4488 ++cmd;
4489 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004490 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 lnum = curwin->w_cursor.lnum;
4492 else
4493 {
4494 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
4496 cmd = NULL;
4497 goto error;
4498 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 lnum = fp->lnum;
4500 }
4501 }
4502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004505 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 c = *cmd++;
4507 if (addr_type != ADDR_LINES)
4508 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004509 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004510 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 goto error;
4512 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004513 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004515 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 if (*cmd == c)
4517 ++cmd;
4518 }
4519 else
4520 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004521 int flags;
4522
4523 pos = curwin->w_cursor; // save curwin->w_cursor
4524
4525 // When '/' or '?' follows another address, start from
4526 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004527 if (lnum > 0 && lnum != MAXLNUM)
4528 curwin->w_cursor.lnum =
4529 lnum > curbuf->b_ml.ml_line_count
4530 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004531
4532 // Start a forward search at the end of the line (unless
4533 // before the first line).
4534 // Start a backward search at the start of the line.
4535 // This makes sure we never match in the current
4536 // line, and can match anywhere in the
4537 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004538 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004539 curwin->w_cursor.col = MAXCOL;
4540 else
4541 curwin->w_cursor.col = 0;
4542 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004543 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
John Marriott8c85a2a2024-05-20 19:18:26 +02004544 if (!do_search(NULL, c, c, cmd, STRLEN(cmd), 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 {
4546 curwin->w_cursor = pos;
4547 cmd = NULL;
4548 goto error;
4549 }
4550 lnum = curwin->w_cursor.lnum;
4551 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004552 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004553 cmd += searchcmdlen;
4554 }
4555 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004557 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004558 ++cmd;
4559 if (addr_type != ADDR_LINES)
4560 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004561 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004562 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004563 goto error;
4564 }
4565 if (*cmd == '&')
4566 i = RE_SUBST;
4567 else if (*cmd == '?' || *cmd == '/')
4568 i = RE_SEARCH;
4569 else
4570 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004571 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004572 cmd = NULL;
4573 goto error;
4574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004576 if (!skip)
4577 {
4578 /*
4579 * When search follows another address, start from
4580 * there.
4581 */
4582 if (lnum != MAXLNUM)
4583 pos.lnum = lnum;
4584 else
4585 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004587 /*
4588 * Start the search just like for the above
4589 * do_search().
4590 */
4591 if (*cmd != '?')
4592 pos.col = MAXCOL;
4593 else
4594 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004595 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004596 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004597 *cmd == '?' ? BACKWARD : FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02004598 (char_u *)"", 0, 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004599 lnum = pos.lnum;
4600 else
4601 {
4602 cmd = NULL;
4603 goto error;
4604 }
4605 }
4606 ++cmd;
4607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004610 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004611 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613
4614 for (;;)
4615 {
4616 cmd = skipwhite(cmd);
4617 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4618 break;
4619
4620 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004621 {
4622 switch (addr_type)
4623 {
4624 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004625 case ADDR_OTHER:
4626 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004627 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004628 break;
4629 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004630 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004631 break;
4632 case ADDR_ARGUMENTS:
4633 lnum = curwin->w_arg_idx + 1;
4634 break;
4635 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004636 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004637 lnum = curbuf->b_fnum;
4638 break;
4639 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004640 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004641 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004642 case ADDR_TABS_RELATIVE:
4643 lnum = 1;
4644 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004645 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004646#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004647 lnum = qf_get_cur_idx(eap);
4648#endif
4649 break;
4650 case ADDR_QUICKFIX_VALID:
4651#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004652 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004653#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004654 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004655 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004656 case ADDR_UNSIGNED:
4657 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004658 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004659 }
4660 }
4661
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004663 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 else
4665 i = *cmd++;
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004666 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 n = 1;
4668 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004669 {
Bram Moolenaarf00112d2022-11-11 01:20:35 +00004670 // "number", "+number" or "-number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004672 if (n == MAXLNUM)
4673 {
4674 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004675 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004676 goto error;
4677 }
4678 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004679
4680 if (addr_type == ADDR_TABS_RELATIVE)
4681 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004682 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004683 cmd = NULL;
4684 goto error;
4685 }
4686 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004687 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004688 lnum = compute_buffer_local_count(
4689 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004691 {
4692#ifdef FEAT_FOLDING
Bram Moolenaar9954dc32022-11-11 22:58:36 +00004693 // Relative line addressing: need to adjust for lines in a
4694 // closed fold after the first address.
4695 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4696 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004697 (void)hasFolding(lnum, NULL, &lnum);
4698#endif
4699 if (i == '-')
4700 lnum -= n;
4701 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004702 {
Christian Brabandt060623e2023-11-14 21:33:29 +01004703 if (lnum >= 0 && n >= LONG_MAX - lnum)
Bram Moolenaar03725c52021-11-24 12:17:53 +00004704 {
4705 emsg(_(e_line_number_out_of_range));
zeertzjqd1b5ea92024-07-13 19:04:10 +02004706 cmd = NULL;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004707 goto error;
4708 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004709 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004710 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
4713 } while (*cmd == '/' || *cmd == '?');
4714
4715error:
4716 *ptr = cmd;
4717 return lnum;
4718}
4719
4720/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004721 * Set eap->line1 and eap->line2 to the whole range.
4722 * Used for commands with the EX_DFLALL flag and no range given.
4723 */
4724 static void
4725address_default_all(exarg_T *eap)
4726{
4727 eap->line1 = 1;
4728 switch (eap->addr_type)
4729 {
4730 case ADDR_LINES:
4731 case ADDR_OTHER:
4732 eap->line2 = curbuf->b_ml.ml_line_count;
4733 break;
4734 case ADDR_LOADED_BUFFERS:
4735 {
4736 buf_T *buf = firstbuf;
4737
4738 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4739 buf = buf->b_next;
4740 eap->line1 = buf->b_fnum;
4741 buf = lastbuf;
4742 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4743 buf = buf->b_prev;
4744 eap->line2 = buf->b_fnum;
4745 }
4746 break;
4747 case ADDR_BUFFERS:
4748 eap->line1 = firstbuf->b_fnum;
4749 eap->line2 = lastbuf->b_fnum;
4750 break;
4751 case ADDR_WINDOWS:
4752 eap->line2 = LAST_WIN_NR;
4753 break;
4754 case ADDR_TABS:
4755 eap->line2 = LAST_TAB_NR;
4756 break;
4757 case ADDR_TABS_RELATIVE:
4758 eap->line2 = 1;
4759 break;
4760 case ADDR_ARGUMENTS:
4761 if (ARGCOUNT == 0)
4762 eap->line1 = eap->line2 = 0;
4763 else
4764 eap->line2 = ARGCOUNT;
4765 break;
4766 case ADDR_QUICKFIX_VALID:
4767#ifdef FEAT_QUICKFIX
4768 eap->line2 = qf_get_valid_size(eap);
4769 if (eap->line2 == 0)
4770 eap->line2 = 1;
4771#endif
4772 break;
4773 case ADDR_NONE:
4774 case ADDR_UNSIGNED:
4775 case ADDR_QUICKFIX:
Bram Moolenaar097c5372023-05-24 21:02:24 +01004776 iemsg("Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX");
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004777 break;
4778 }
4779}
4780
4781
4782/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004783 * Get flags from an Ex command argument.
4784 */
4785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004786get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004787{
4788 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4789 {
4790 if (*eap->arg == 'l')
4791 eap->flags |= EXFLAG_LIST;
4792 else if (*eap->arg == 'p')
4793 eap->flags |= EXFLAG_PRINT;
4794 else
4795 eap->flags |= EXFLAG_NR;
4796 eap->arg = skipwhite(eap->arg + 1);
4797 }
4798}
4799
4800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 * Function called for command which is Not Implemented. NI!
4802 */
4803 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004804ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
4806 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004807 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004808 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809}
4810
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004811#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812/*
4813 * Function called for script command which is Not Implemented. NI!
4814 * Skips over ":perl <<EOF" constructs.
4815 */
4816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004817ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
4819 if (!eap->skip)
4820 ex_ni(eap);
4821 else
4822 vim_free(script_get(eap, eap->arg));
4823}
4824#endif
4825
4826/*
4827 * Check range in Ex command for validity.
4828 * Return NULL when valid, error message when invalid.
4829 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004830 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004831invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004833 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if ( eap->line1 < 0
4836 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004837 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004838 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004839
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004840 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004841 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004842 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004843 {
4844 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004845 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004846#ifdef FEAT_DIFF
4847 + (eap->cmdidx == CMD_diffget)
4848#endif
4849 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004850 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004851 break;
4852 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004853 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004854 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004855 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004856 break;
4857 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004858 // Only a boundary check, not whether the buffers actually
4859 // exist.
4860 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004861 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004862 break;
4863 case ADDR_LOADED_BUFFERS:
4864 buf = firstbuf;
4865 while (buf->b_ml.ml_mfp == NULL)
4866 {
4867 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004868 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004869 buf = buf->b_next;
4870 }
4871 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004872 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004873 buf = lastbuf;
4874 while (buf->b_ml.ml_mfp == NULL)
4875 {
4876 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004877 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004878 buf = buf->b_prev;
4879 }
4880 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004881 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004882 break;
4883 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004884 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004885 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004886 break;
4887 case ADDR_TABS:
4888 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004889 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004890 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004891 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004892 case ADDR_OTHER:
4893 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004894 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004895 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004896#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004897 // No error for value that is too big, will use the last entry.
4898 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004899 {
4900 if (eap->addr_count == 0)
4901 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004902 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004903 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004904#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004905 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004906 case ADDR_QUICKFIX_VALID:
4907#ifdef FEAT_QUICKFIX
4908 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4909 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004910 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004911#endif
4912 break;
4913 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004914 case ADDR_NONE:
4915 // Will give an error elsewhere.
4916 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004917 }
4918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 return NULL;
4920}
4921
4922/*
4923 * Correct the range for zero line number, if required.
4924 */
4925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004926correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004928 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
4930 if (eap->line1 == 0)
4931 eap->line1 = 1;
4932 if (eap->line2 == 0)
4933 eap->line2 = 1;
4934 }
4935}
4936
Bram Moolenaar748bf032005-02-02 23:04:36 +00004937#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004938/*
4939 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4940 * pattern. Otherwise return eap->arg.
4941 */
4942 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004943skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004944{
4945 char_u *p = eap->arg;
4946
Bram Moolenaara37420f2006-02-04 22:37:47 +00004947 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4948 || eap->cmdidx == CMD_vimgrepadd
4949 || eap->cmdidx == CMD_lvimgrepadd
4950 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004951 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004952 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004953 if (p == NULL)
4954 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004955 }
4956 return p;
4957}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004958
4959/*
4960 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4961 * in the command line, so that things like % get expanded.
4962 */
4963 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004964replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004965{
4966 char_u *new_cmdline;
4967 char_u *program;
4968 char_u *pos;
4969 char_u *ptr;
4970 int len;
4971 int i;
4972
4973 /*
4974 * Don't do it when ":vimgrep" is used for ":grep".
4975 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004976 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4977 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4978 || eap->cmdidx == CMD_grepadd
4979 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004980 && !grep_internal(eap->cmdidx))
4981 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004982 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4983 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004984 {
4985 if (*curbuf->b_p_gp == NUL)
4986 program = p_gp;
4987 else
4988 program = curbuf->b_p_gp;
4989 }
4990 else
4991 {
4992 if (*curbuf->b_p_mp == NUL)
4993 program = p_mp;
4994 else
4995 program = curbuf->b_p_mp;
4996 }
4997
4998 p = skipwhite(p);
4999
5000 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5001 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005002 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005003 i = 1;
5004 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
5005 ++i;
5006 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005007 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005008 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005009 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005010 ptr = new_cmdline;
5011 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
5012 {
5013 i = (int)(pos - program);
5014 STRNCPY(ptr, program, i);
5015 STRCPY(ptr += i, p);
5016 ptr += len;
5017 program = pos + 2;
5018 }
5019 STRCPY(ptr, program);
5020 }
5021 else
5022 {
John Marriotted908f72024-04-18 22:46:56 +02005023 size_t program_len = STRLEN(program);
5024
5025 new_cmdline = alloc(program_len + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005026 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005027 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005028 STRCPY(new_cmdline, program);
John Marriotted908f72024-04-18 22:46:56 +02005029 STRCPY(new_cmdline + program_len, " ");
5030 STRCPY(new_cmdline + program_len + 1, p);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005031 }
5032 msg_make(p);
5033
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005034 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005035 vim_free(*cmdlinep);
5036 *cmdlinep = new_cmdline;
5037 p = new_cmdline;
5038 }
5039 return p;
5040}
Bram Moolenaar748bf032005-02-02 23:04:36 +00005041#endif
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043/*
5044 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02005045 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 * Return FAIL for failure, OK otherwise.
5047 */
5048 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005049expand_filename(
5050 exarg_T *eap,
5051 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005052 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005054 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 char_u *repl;
Mike Williams51024bb2024-05-30 07:46:30 +02005056 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 char_u *p;
5058 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00005059 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
Bram Moolenaar748bf032005-02-02 23:04:36 +00005061#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005062 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00005063 p = skip_grep_pat(eap);
5064#else
5065 p = eap->arg;
5066#endif
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 /*
5069 * Decide to expand wildcards *before* replacing '%', '#', etc. If
5070 * the file name contains a wildcard it should not cause expanding.
5071 * (it will be expanded anyway if there is a wildcard before replacing).
5072 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00005073 has_wildcards = mch_has_wildcard(p);
5074 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005076#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005077 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005078 if (p[0] == '`' && p[1] == '=')
5079 {
5080 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005081 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005082 if (*p == '`')
5083 ++p;
5084 continue;
5085 }
5086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 /*
5088 * Quick check if this cannot be the start of a special string.
5089 * Also removes backslash before '%', '#' and '<'.
5090 */
5091 if (vim_strchr((char_u *)"%#<", *p) == NULL)
5092 {
5093 ++p;
5094 continue;
5095 }
5096
5097 /*
5098 * Try to find a match at this position.
5099 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00005100 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
Bram Moolenaara96edb72022-04-28 17:52:24 +01005101 errormsgp, &escaped, TRUE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005102 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005104 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
5106 p += srclen;
5107 continue;
5108 }
5109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005110 // Wildcards won't be expanded below, the replacement is taken
5111 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00005112 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
5113 {
5114 char_u *l = repl;
5115
5116 repl = expand_env_save(repl);
5117 vim_free(l);
5118 }
5119
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005120 // Need to escape white space et al. with a backslash.
5121 // Don't do this for:
5122 // - replacement that already has been escaped: "##"
5123 // - shell commands (may have to use quotes instead).
5124 // - non-unix systems when there is a single argument (spaces don't
5125 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00005127 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 && eap->cmdidx != CMD_grep
5130 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02005131 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02005132 && eap->cmdidx != CMD_lgrep
5133 && eap->cmdidx != CMD_lgrepadd
5134 && eap->cmdidx != CMD_lmake
5135 && eap->cmdidx != CMD_make
5136 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005138 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139#endif
5140 )
5141 {
5142 char_u *l;
5143#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005144 // Don't escape a backslash here, because rem_backslash() doesn't
5145 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 static char_u *nobslash = (char_u *)" \t\"|";
5147# define ESCAPE_CHARS nobslash
5148#else
5149# define ESCAPE_CHARS escape_chars
5150#endif
5151
5152 for (l = repl; *l; ++l)
5153 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
5154 {
5155 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
5156 if (l != NULL)
5157 {
5158 vim_free(repl);
5159 repl = l;
5160 }
5161 break;
5162 }
5163 }
5164
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005165 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02005166 if ((eap->usefilter || eap->cmdidx == CMD_bang
5167 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005168 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
5170 char_u *l;
5171
Bram Moolenaar31b7d382014-04-01 18:54:48 +02005172 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (l != NULL)
5174 {
5175 vim_free(repl);
5176 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178 }
5179
Mike Williams51024bb2024-05-30 07:46:30 +02005180 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 vim_free(repl);
5182 if (p == NULL)
5183 return FAIL;
5184 }
5185
5186 /*
5187 * One file argument: Expand wildcards.
5188 * Don't do this with ":r !command" or ":w !command".
5189 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005190 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
5192 /*
5193 * May do this twice:
5194 * 1. Replace environment variables.
5195 * 2. Replace any other wildcards, remove backslashes.
5196 */
5197 for (n = 1; n <= 2; ++n)
5198 {
5199 if (n == 2)
5200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 /*
5202 * Halve the number of backslashes (this is Vi compatible).
5203 * For Unix and OS/2, when wildcards are expanded, this is
5204 * done by ExpandOne() below.
5205 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005206#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (!has_wildcards)
5208#endif
5209 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
5211
5212 if (has_wildcards)
5213 {
5214 if (n == 1)
5215 {
5216 /*
5217 * First loop: May expand environment variables. This
5218 * can be done much faster with expand_env() than with
5219 * something else (e.g., calling a shell).
5220 * After expanding environment variables, check again
5221 * if there are still wildcards present.
5222 */
5223 if (vim_strchr(eap->arg, '$') != NULL
5224 || vim_strchr(eap->arg, '~') != NULL)
5225 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005226 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005227 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 has_wildcards = mch_has_wildcard(NameBuff);
5229 p = NameBuff;
5230 }
5231 else
5232 p = NULL;
5233 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005234 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
5236 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005237 int options = WILD_LIST_NOTFOUND
5238 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 ExpandInit(&xpc);
5241 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005242 if (p_wic)
5243 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005245 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 if (p == NULL)
5247 return FAIL;
5248 }
5249 if (p != NULL)
5250 {
John Marriotted908f72024-04-18 22:46:56 +02005251 (void)repl_cmdline(eap, eap->arg, STRLEN(eap->arg),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005253 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 vim_free(p);
5255 }
5256 }
5257 }
5258 }
5259 return OK;
5260}
5261
5262/*
5263 * Replace part of the command line, keeping eap->cmd, eap->arg and
5264 * eap->nextcmd correct.
5265 * "src" points to the part that is to be replaced, of length "srclen".
5266 * "repl" is the replacement string.
5267 * Returns a pointer to the character after the replaced string.
5268 * Returns NULL for failure.
5269 */
5270 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005271repl_cmdline(
5272 exarg_T *eap,
5273 char_u *src,
John Marriotted908f72024-04-18 22:46:56 +02005274 size_t srclen,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005275 char_u *repl,
5276 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
John Marriotted908f72024-04-18 22:46:56 +02005278 size_t repllen;
5279 size_t taillen;
5280 size_t i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 char_u *new_cmdline;
John Marriotted908f72024-04-18 22:46:56 +02005282 size_t new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
5284 /*
5285 * The new command line is build in new_cmdline[].
5286 * First allocate it.
5287 * Careful: a "+cmd" argument may have been NUL terminated.
5288 */
John Marriotted908f72024-04-18 22:46:56 +02005289 repllen = STRLEN(repl);
5290 taillen = STRLEN(src + srclen);
5291 i = (src - *cmdlinep) + repllen + taillen + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005292 if (eap->nextcmd != NULL)
John Marriotted908f72024-04-18 22:46:56 +02005293 i += STRLEN(eap->nextcmd); // add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005294 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005295 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 /*
5298 * Copy the stuff before the expanded part.
5299 * Copy the expanded stuff.
5300 * Copy what came after the expanded part.
5301 * Copy the next commands, if there are any.
5302 */
John Marriotted908f72024-04-18 22:46:56 +02005303 new_cmdlinelen = src - *cmdlinep; // length of part before replacement
5304 mch_memmove(new_cmdline, *cmdlinep, new_cmdlinelen);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005305
John Marriotted908f72024-04-18 22:46:56 +02005306 mch_memmove(new_cmdline + new_cmdlinelen, repl, repllen);
5307 new_cmdlinelen += repllen; // remember the end of the string
5308 STRCPY(new_cmdline + new_cmdlinelen, src + srclen);
5309 src = new_cmdline + new_cmdlinelen; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005311 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
John Marriotted908f72024-04-18 22:46:56 +02005313 new_cmdlinelen += taillen + 1;
5314 STRCPY(new_cmdline + new_cmdlinelen, eap->nextcmd);
5315 eap->nextcmd = new_cmdline + new_cmdlinelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 }
5317 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5318 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5319 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5320 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5321 vim_free(*cmdlinep);
5322 *cmdlinep = new_cmdline;
5323
5324 return src;
5325}
5326
5327/*
5328 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005329 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 */
5331 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005332separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 char_u *p;
5335
Bram Moolenaar86b68352004-12-27 21:59:20 +00005336#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005337 p = skip_grep_pat(eap);
5338#else
5339 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005340#endif
5341
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005342 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
5344 if (*p == Ctrl_V)
5345 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005346 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005347 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005349 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005350 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005351 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 break;
5353 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005354
5355#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005356 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005357 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005358 {
5359 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005360 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005361 if (*p == NUL) // stop at NUL after CTRL-V
5362 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005363 }
5364#endif
5365
Bram Moolenaareda29c92022-10-02 12:59:00 +01005366 // Check for '"'/'#': start of comment or '|': next command
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005367 // :@" and :*" do not start a comment!
5368 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005369 else if ((*p == '"'
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005370 && !in_vim9script()
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005371 && !(eap->argt & EX_NOTRLCOM)
5372 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5373 || p != eap->arg)
5374 && (eap->cmdidx != CMD_redir
5375 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005376 || (*p == '#'
5377 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005378 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005379 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Mohamed Akram8c446da2024-07-13 18:49:55 +02005380 || (*p == '|'
5381 && eap->cmdidx != CMD_append
5382 && eap->cmdidx != CMD_change
5383 && eap->cmdidx != CMD_insert)
5384 || *p == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
5386 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005387 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 * AND 'b' is present in 'cpoptions'.
5389 */
5390 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005391 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005393 if (!keep_backslash)
5394 {
5395 STRMOVE(p - 1, p); // remove the '\'
5396 --p;
5397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 else
5400 {
5401 eap->nextcmd = check_nextcmd(p);
5402 *p = NUL;
5403 break;
5404 }
5405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005407
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005408 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 del_trailing_spaces(eap->arg);
5410}
5411
5412/*
5413 * get + command from ex argument
5414 */
5415 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005416getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
5418 char_u *arg = *argp;
5419 char_u *command = NULL;
5420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005421 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
5423 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005424 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 command = dollar_command;
5426 else
5427 {
5428 command = arg;
5429 arg = skip_cmd_arg(command, TRUE);
5430 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005431 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
5433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005434 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 *argp = arg;
5436 }
5437 return command;
5438}
5439
5440/*
5441 * Find end of "+command" argument. Skip over "\ " and "\\".
5442 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005443 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005444skip_cmd_arg(
5445 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005446 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 while (*p && !vim_isspace(*p))
5449 {
5450 if (*p == '\\' && p[1] != NUL)
5451 {
5452 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005453 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 else
5455 ++p;
5456 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005457 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 return p;
5460}
5461
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005462 int
5463get_bad_opt(char_u *p, exarg_T *eap)
5464{
5465 if (STRICMP(p, "keep") == 0)
5466 eap->bad_char = BAD_KEEP;
5467 else if (STRICMP(p, "drop") == 0)
5468 eap->bad_char = BAD_DROP;
5469 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5470 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005471 else
5472 return FAIL;
5473 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005474}
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005477 * Function given to ExpandGeneric() to obtain the list of bad= names.
5478 */
5479 static char_u *
5480get_bad_name(expand_T *xp UNUSED, int idx)
5481{
5482 // Note: Keep this in sync with getargopt.
5483 static char *(p_bad_values[]) =
5484 {
5485 "?",
5486 "keep",
5487 "drop",
5488 };
5489
John Marriotted908f72024-04-18 22:46:56 +02005490 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_bad_values))
5491 return NULL;
5492
5493 return (char_u*)p_bad_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005494}
5495
5496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 * Get "++opt=arg" argument.
5498 * Return FAIL or OK.
5499 */
5500 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005501getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
5503 char_u *arg = eap->arg + 2;
5504 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005505 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005508 // Note: Keep this in sync with get_argopt_name.
5509
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005510 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5512 {
5513 if (*arg == 'n')
5514 {
5515 arg += 2;
5516 eap->force_bin = FORCE_NOBIN;
5517 }
5518 else
5519 eap->force_bin = FORCE_BIN;
5520 if (!checkforcmd(&arg, "binary", 3))
5521 return FAIL;
5522 eap->arg = skipwhite(arg);
5523 return OK;
5524 }
5525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005526 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005527 if (STRNCMP(arg, "edit", 4) == 0)
5528 {
5529 eap->read_edit = TRUE;
5530 eap->arg = skipwhite(arg + 4);
5531 return OK;
5532 }
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (STRNCMP(arg, "ff", 2) == 0)
5535 {
5536 arg += 2;
5537 pp = &eap->force_ff;
5538 }
5539 else if (STRNCMP(arg, "fileformat", 10) == 0)
5540 {
5541 arg += 10;
5542 pp = &eap->force_ff;
5543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 else if (STRNCMP(arg, "enc", 3) == 0)
5545 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005546 if (STRNCMP(arg, "encoding", 8) == 0)
5547 arg += 8;
5548 else
5549 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 pp = &eap->force_enc;
5551 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005552 else if (STRNCMP(arg, "bad", 3) == 0)
5553 {
5554 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005555 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
5558 if (pp == NULL || *arg != '=')
5559 return FAIL;
5560
5561 ++arg;
5562 *pp = (int)(arg - eap->cmd);
5563 arg = skip_cmd_arg(arg, FALSE);
5564 eap->arg = skipwhite(arg);
5565 *arg = NUL;
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 if (pp == &eap->force_ff)
5568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5570 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005571 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005573 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005575 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5577 *p = TOLOWER_ASC(*p);
5578 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005579 else
5580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005581 // Check ++bad= argument. Must be a single-byte character, "keep" or
5582 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005583 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005584 return FAIL;
5585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
5587 return OK;
5588}
5589
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005590/*
5591 * Function given to ExpandGeneric() to obtain the list of ++opt names.
5592 */
5593 static char_u *
5594get_argopt_name(expand_T *xp UNUSED, int idx)
5595{
5596 // Note: Keep this in sync with getargopt.
5597 static char *(p_opt_values[]) =
5598 {
5599 "fileformat=",
5600 "encoding=",
5601 "binary",
5602 "nobinary",
5603 "bad=",
5604 "edit",
5605 };
5606
John Marriotted908f72024-04-18 22:46:56 +02005607 if (idx < 0 || idx >= (int)ARRAY_LENGTH(p_opt_values))
5608 return NULL;
5609
5610 return (char_u*)p_opt_values[idx];
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005611}
5612
5613/*
5614 * Command-line expansion for ++opt=name.
5615 */
5616 int
5617expand_argopt(
5618 char_u *pat,
5619 expand_T *xp,
5620 regmatch_T *rmp,
5621 char_u ***matches,
5622 int *numMatches)
5623{
5624 if (xp->xp_pattern > xp->xp_line && *(xp->xp_pattern-1) == '=')
5625 {
5626 char_u *(*cb)(expand_T *, int) = NULL;
5627
5628 char_u *name_end = xp->xp_pattern - 1;
5629 if (name_end - xp->xp_line >= 2
5630 && STRNCMP(name_end - 2, "ff", 2) == 0)
5631 cb = get_fileformat_name;
5632 else if (name_end - xp->xp_line >= 10
5633 && STRNCMP(name_end - 10, "fileformat", 10) == 0)
5634 cb = get_fileformat_name;
5635 else if (name_end - xp->xp_line >= 3
5636 && STRNCMP(name_end - 3, "enc", 3) == 0)
5637 cb = get_encoding_name;
5638 else if (name_end - xp->xp_line >= 8
5639 && STRNCMP(name_end - 8, "encoding", 8) == 0)
5640 cb = get_encoding_name;
5641 else if (name_end - xp->xp_line >= 3
5642 && STRNCMP(name_end - 3, "bad", 3) == 0)
5643 cb = get_bad_name;
5644
5645 if (cb != NULL)
5646 {
5647 return ExpandGeneric(
5648 pat,
5649 xp,
5650 rmp,
5651 matches,
5652 numMatches,
5653 cb,
5654 FALSE);
5655 }
5656 return FAIL;
5657 }
5658
5659 // Special handling of "ff" which acts as a short form of
5660 // "fileformat", as "ff" is not a substring of it.
Yee Cheng Chin209ec902023-10-17 10:56:25 +02005661 if (xp->xp_pattern_len == 2
5662 && STRNCMP(xp->xp_pattern, "ff", xp->xp_pattern_len) == 0)
Yee Cheng Chin989426b2023-10-14 11:46:51 +02005663 {
5664 *matches = ALLOC_MULT(char_u *, 1);
5665 if (*matches == NULL)
5666 return FAIL;
5667 *numMatches = 1;
5668 (*matches)[0] = vim_strsave((char_u*)"fileformat=");
5669 return OK;
5670 }
5671
5672 return ExpandGeneric(
5673 pat,
5674 xp,
5675 rmp,
5676 matches,
5677 numMatches,
5678 get_argopt_name,
5679 FALSE);
5680}
5681
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005683ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
5685 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005686 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 * directory for security reasons.
5688 */
5689 if (secure)
5690 {
5691 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005692 eap->errmsg =
5693 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005696 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 else
5698 do_augroup(eap->arg, eap->forceit);
5699}
5700
5701/*
5702 * ":doautocmd": Apply the automatic commands to the current buffer.
5703 */
5704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005705ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005707 char_u *arg = eap->arg;
5708 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005709 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005710
Bram Moolenaar1610d052016-06-09 22:53:01 +02005711 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005712 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005713 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005714 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717/*
5718 * :[N]bunload[!] [N] [bufname] unload buffer
5719 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5720 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5721 */
5722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005723ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005725 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005726 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 eap->errmsg = do_bufdel(
5728 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5729 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5730 : DOBUF_UNLOAD, eap->arg,
5731 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5732}
5733
5734/*
5735 * :[N]buffer [N] to buffer N
5736 * :[N]sbuffer [N] to buffer N
5737 */
5738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005741 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005742 return;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01005743 do_exbuffer(eap);
5744}
5745
5746/*
5747 * ":buffer" command and alike.
5748 */
5749 static void
5750do_exbuffer(exarg_T *eap)
5751{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005753 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 else
5755 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005756 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5758 else
5759 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005760 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005761 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 }
5763}
5764
5765/*
5766 * :[N]bmodified [N] to next mod. buffer
5767 * :[N]sbmodified [N] to next mod. buffer
5768 */
5769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005770ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005773 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005774 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775}
5776
5777/*
5778 * :[N]bnext [N] to next buffer
5779 * :[N]sbnext [N] split and to next buffer
5780 */
5781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005782ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005784 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005785 return;
5786
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005788 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005789 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790}
5791
5792/*
5793 * :[N]bNext [N] to previous buffer
5794 * :[N]bprevious [N] to previous buffer
5795 * :[N]sbNext [N] split and to previous buffer
5796 * :[N]sbprevious [N] split and to previous buffer
5797 */
5798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005799ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005801 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005802 return;
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005805 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005806 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807}
5808
5809/*
5810 * :brewind to first buffer
5811 * :bfirst to first buffer
5812 * :sbrewind split and to first buffer
5813 * :sbfirst split and to first buffer
5814 */
5815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005816ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005818 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005819 return;
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005822 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005823 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824}
5825
5826/*
5827 * :blast to last buffer
5828 * :sblast split and to last buffer
5829 */
5830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005833 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005834 return;
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005837 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005838 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
Bram Moolenaar7a092242020-04-16 22:10:49 +02005841/*
5842 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005843 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005846ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005848 int comment_char = '"';
5849
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005850 if (in_vim9script())
5851 comment_char = '#';
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005852 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005853}
5854
5855/*
5856 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5857 * to "cmd_start" or has a white space character before it.
5858 */
5859 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005860ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005861{
5862 int c = *cmd;
5863
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005864 if (c == NUL || c == '|' || c == '\n')
5865 return TRUE;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005866 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005867 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5868 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005869 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005870 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871}
5872
5873#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5874 || defined(PROTO)
5875/*
5876 * Return the next command, after the first '|' or '\n'.
5877 * Return NULL if not found.
5878 */
5879 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005880find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 while (*p != '|' && *p != '\n')
5883 {
5884 if (*p == NUL)
5885 return NULL;
5886 ++p;
5887 }
5888 return (p + 1);
5889}
5890#endif
5891
5892/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005893 * Check if *p is a separator between Ex commands, skipping over white space.
5894 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 */
5896 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005897check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005899 char_u *s = skipwhite(p);
5900
5901 if (*s == '|' || *s == '\n')
5902 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 else
5904 return NULL;
5905}
5906
5907/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005908 * If "eap->nextcmd" is not set, check for a next command at "p".
5909 */
5910 void
5911set_nextcmd(exarg_T *eap, char_u *arg)
5912{
5913 char_u *p = check_nextcmd(arg);
5914
5915 if (eap->nextcmd == NULL)
5916 eap->nextcmd = p;
5917 else if (p != NULL)
5918 // cannot use "| command" inside a {} block
5919 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5920}
5921
5922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 * - if there are more files to edit
5924 * - and this is the last window
5925 * - and forceit not used
5926 * - and not repeated twice on a row
5927 * return FAIL and give error message if 'message' TRUE
5928 * return OK otherwise
5929 */
5930 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005931check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005932 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005933 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5936
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005937 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005938 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {
5940 if (message)
5941 {
5942#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005943 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5944 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005946 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005948 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5949 NGETTEXT("%d more file to edit. Quit anyway?",
5950 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5952 return OK;
5953 return FAIL;
5954 }
5955#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005956 semsg(NGETTEXT(e_nr_more_file_to_edit,
5957 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005958 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 }
5960 return FAIL;
5961 }
5962 return OK;
5963}
5964
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965/*
5966 * Function given to ExpandGeneric() to obtain the list of command names.
5967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005969get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
5971 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005972 return expand_user_command_name(idx);
Christian Brabandt70a11a62024-07-26 19:13:55 +02005973 // the following are no real commands
5974 if (STRNCMP(cmdnames[idx].cmd_name, "{", 1) == 0 ||
5975 STRNCMP(cmdnames[idx].cmd_name, "}", 1) == 0)
5976 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 return cmdnames[idx].cmd_name;
5978}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005981ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
Bram Moolenaare6850792010-05-14 15:28:44 +02005983 if (*eap->arg == NUL)
5984 {
5985#ifdef FEAT_EVAL
5986 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5987 char_u *p = NULL;
5988
5989 if (expr != NULL)
5990 {
5991 ++emsg_off;
Bram Moolenaara4e0b972022-10-01 19:43:52 +01005992 p = eval_to_string(expr, FALSE, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005993 --emsg_off;
5994 vim_free(expr);
5995 }
5996 if (p != NULL)
5997 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005998 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005999 vim_free(p);
6000 }
6001 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006002 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02006003#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006004 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02006005#endif
6006 }
6007 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00006008 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006009
6010#ifdef FEAT_VTP
6011 else if (has_vtp_working())
6012 {
6013 // background color change requires clear + redraw
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006014 update_screen(UPD_CLEAR);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01006015 redrawcmd();
6016 }
6017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018}
6019
6020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006021ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022{
6023 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01006024 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 do_highlight(eap->arg, eap->forceit, FALSE);
6026}
6027
6028
6029/*
6030 * Call this function if we thought we were going to exit, but we won't
6031 * (because of an error). May need to restore the terminal mode.
6032 */
6033 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006034not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036 exiting = FALSE;
6037 settmode(TMODE_RAW);
6038}
6039
Bram Moolenaar3552e742021-05-29 12:21:58 +02006040 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006041before_quit_autocmds(win_T *wp, int quit_all, int forceit)
6042{
6043 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
6044
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006045 // Bail out when autocommands closed the window.
6046 // Refuse to quit when the buffer in the last window is being closed (can
6047 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006048 if (!win_valid(wp)
6049 || curbuf_locked()
6050 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
6051 return TRUE;
6052
6053 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
6054 {
6055 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02006056 // Refuse to quit when locked or when the window was closed or the
6057 // buffer in the last window is being closed (can only happen in
6058 // autocommands).
6059 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006060 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
6061 return TRUE;
6062 }
6063
6064 return FALSE;
6065}
6066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01006068 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006069 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006070 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02006072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006073ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006075 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (cmdwin_type != 0)
6078 {
6079 cmdwin_result = Ctrl_C;
6080 return;
6081 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006082 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006083 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006084 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006085 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006086 return;
6087 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006088 if (eap->addr_count > 0)
6089 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01006090 int wnr = eap->line2;
6091
6092 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
6093 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006094 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006095 }
6096 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006097 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006099 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02006100 if (curbuf_locked())
6101 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006104 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006105 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107#ifdef FEAT_NETBEANS_INTG
6108 netbeansForcedQuit = eap->forceit;
6109#endif
6110
6111 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02006112 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 */
6114 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6115 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02006116 if ((!buf_hide(wp->w_buffer)
6117 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006118 | (eap->forceit ? CCGD_FORCEIT : 0)
6119 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006121 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
6123 not_exiting();
6124 }
6125 else
6126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006127 // quit last window
6128 // Note: only_one_window() returns true, even so a help window is
6129 // still open. In that case only quit, if no address has been
6130 // specified. Example:
6131 // :h|wincmd w|1q - don't quit
6132 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01006133 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006135 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02006136#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006138#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006139 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02006140 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 }
6142}
6143
6144/*
6145 * ":cquit".
6146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006148ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01006150 // this does not always pass on the exit code to the Manx compiler. why?
6151 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152}
6153
6154/*
Bram Moolenaar411da642023-05-10 16:53:27 +01006155 * Do preparations for "qall" and "wqall".
6156 * Returns FAIL when quitting should be aborted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 */
Bram Moolenaar411da642023-05-10 16:53:27 +01006158 int
6159before_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 if (cmdwin_type != 0)
6162 {
6163 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006164 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 else
6166 cmdwin_result = K_XF2;
Bram Moolenaar411da642023-05-10 16:53:27 +01006167 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006169
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006170 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006171 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006172 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006173 text_locked_msg();
Bram Moolenaar411da642023-05-10 16:53:27 +01006174 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006175 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006176
6177 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar411da642023-05-10 16:53:27 +01006178 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006179
Bram Moolenaar411da642023-05-10 16:53:27 +01006180 return OK;
6181}
6182
6183/*
6184 * ":qall": try to quit all windows
6185 */
6186 static void
6187ex_quit_all(exarg_T *eap)
6188{
6189 if (before_quit_all(eap) == FAIL)
6190 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01006192 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 getout(0);
6194 not_exiting();
6195}
6196
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197/*
6198 * ":close": close current window, unless it is the last one
6199 */
6200 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006201ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006203 win_T *win;
6204 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006206 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006208 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006209 {
6210 if (eap->addr_count == 0)
6211 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02006212 else
6213 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006214 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006215 {
6216 winnr++;
6217 if (winnr == eap->line2)
6218 break;
6219 }
6220 if (win == NULL)
6221 win = lastwin;
6222 ex_win_close(eap->forceit, win, NULL);
6223 }
6224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225}
6226
Bram Moolenaar4033c552017-09-16 20:54:51 +02006227#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006228/*
6229 * ":pclose": Close any preview window.
6230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006232ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006233{
6234 win_T *win;
6235
Bram Moolenaar79648732019-07-18 21:43:07 +02006236 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02006237 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006238 if (win->w_p_pvw)
6239 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006240 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02006241 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006242 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006243# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02006244 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02006245 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02006246# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006247}
Bram Moolenaar4033c552017-09-16 20:54:51 +02006248#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006249
Bram Moolenaarf740b292006-02-16 22:11:02 +00006250/*
6251 * Close window "win" and take care of handling closing the last window for a
6252 * modified buffer.
6253 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006255ex_win_close(
6256 int forceit,
6257 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006258 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 int need_hide;
6261 buf_T *buf = win->w_buffer;
6262
Bram Moolenaarcf844172020-06-26 19:44:06 +02006263 // Never close the autocommand window.
Bram Moolenaare76062c2022-11-28 18:51:43 +00006264 if (is_aucmd_win(win))
Bram Moolenaarcf844172020-06-26 19:44:06 +02006265 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006266 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02006267 return;
6268 }
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006269 if (window_layout_locked(CMD_close))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006270 return;
Bram Moolenaarcf844172020-06-26 19:44:06 +02006271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006273 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006275#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02006276 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006278# ifdef FEAT_TERMINAL
6279 if (term_job_running(buf->b_term))
6280 {
6281 if (term_confirm_stop(buf) == FAIL)
6282 return;
6283 // Manually kill the terminal here because this command will
6284 // hide it otherwise.
6285 free_terminal(buf);
6286 need_hide = FALSE;
6287 }
6288 else
6289# endif
6290 {
6291 bufref_T bufref;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006292
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006293 set_bufref(&bufref, buf);
6294 dialog_changed(buf, FALSE);
6295 if (bufref_valid(&bufref) && bufIsChanged(buf))
6296 return;
6297 need_hide = FALSE;
6298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 }
6300 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02006303 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 return;
6305 }
6306 }
6307
Bram Moolenaar4033c552017-09-16 20:54:51 +02006308#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006310#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006312 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00006313 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02006314 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006315 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02006316 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317}
6318
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319/*
Bram Moolenaardea25702017-01-29 15:18:10 +01006320 * Handle the argument for a tabpage related ex command.
6321 * Returns a tabpage number.
6322 * When an error is encountered then eap->errmsg is set.
6323 */
6324 static int
6325get_tabpage_arg(exarg_T *eap)
6326{
6327 int tab_number;
6328 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
6329
6330 if (eap->arg && *eap->arg != NUL)
6331 {
6332 char_u *p = eap->arg;
6333 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006334 int relative = 0; // argument +N/-N means: go to N places to the
6335 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01006336
6337 if (*p == '-')
6338 {
6339 relative = -1;
6340 p++;
6341 }
6342 else if (*p == '+')
6343 {
6344 relative = 1;
6345 p++;
6346 }
6347
6348 p_save = p;
6349 tab_number = getdigits(&p);
6350
6351 if (relative == 0)
6352 {
6353 if (STRCMP(p, "$") == 0)
6354 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006355 else if (STRCMP(p, "#") == 0)
6356 if (valid_tabpage(lastused_tabpage))
6357 tab_number = tabpage_index(lastused_tabpage);
6358 else
6359 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01006360 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str,
6361 eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006362 tab_number = 0;
6363 goto theend;
6364 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006365 else if (p == p_save || *p_save == '-' || *p != NUL
6366 || tab_number > LAST_TAB_NR)
6367 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006368 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006369 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006370 goto theend;
6371 }
6372 }
6373 else
6374 {
6375 if (*p_save == NUL)
6376 tab_number = 1;
6377 else if (p == p_save || *p_save == '-' || *p != NUL
6378 || tab_number == 0)
6379 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006380 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006381 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006382 goto theend;
6383 }
6384 tab_number = tab_number * relative + tabpage_index(curtab);
6385 if (!unaccept_arg0 && relative == -1)
6386 --tab_number;
6387 }
6388 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006389 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006390 }
6391 else if (eap->addr_count > 0)
6392 {
6393 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006394 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006395 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006396 tab_number = 0;
6397 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006398 else
6399 {
6400 tab_number = eap->line2;
zeertzjq076faac2024-03-25 16:41:06 +01006401 if (!unaccept_arg0)
Bram Moolenaardea25702017-01-29 15:18:10 +01006402 {
zeertzjq076faac2024-03-25 16:41:06 +01006403 char_u *cmdp = eap->cmd;
6404
6405 while (--cmdp > *eap->cmdlinep
6406 && (VIM_ISWHITE(*cmdp) || VIM_ISDIGIT(*cmdp)))
6407 ;
6408 if (*cmdp == '-')
6409 {
6410 --tab_number;
6411 if (tab_number < unaccept_arg0)
6412 eap->errmsg = _(e_invalid_range);
6413 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006414 }
6415 }
6416 }
6417 else
6418 {
6419 switch (eap->cmdidx)
6420 {
6421 case CMD_tabnext:
6422 tab_number = tabpage_index(curtab) + 1;
6423 if (tab_number > LAST_TAB_NR)
6424 tab_number = 1;
6425 break;
6426 case CMD_tabmove:
6427 tab_number = LAST_TAB_NR;
6428 break;
6429 default:
6430 tab_number = tabpage_index(curtab);
6431 }
6432 }
6433
6434theend:
6435 return tab_number;
6436}
6437
6438/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006439 * ":tabclose": close current tab page, unless it is the last one.
6440 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 */
6442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006443ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006445 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006446 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006447
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006448 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006449 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006450 cmdwin_result = K_IGNORE;
6451 return;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006452 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006453
6454 if (first_tabpage->tp_next == NULL)
6455 {
6456 emsg(_(e_cannot_close_last_tab_page));
6457 return;
6458 }
6459
6460 if (window_layout_locked(CMD_tabclose))
6461 return;
6462
6463 tab_number = get_tabpage_arg(eap);
6464 if (eap->errmsg != NULL)
6465 return;
6466
6467 tp = find_tabpage(tab_number);
6468 if (tp == NULL)
6469 {
6470 beep_flush();
6471 return;
6472 }
6473 if (tp != curtab)
6474 {
6475 tabpage_close_other(tp, eap->forceit);
6476 return;
6477 }
6478 else if (!text_locked() && !curbuf_locked())
6479 tabpage_close(eap->forceit);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006480}
6481
6482/*
6483 * ":tabonly": close all tab pages except the current one
6484 */
6485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006486ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006487{
6488 tabpage_T *tp;
6489 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006490 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006491
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006492 if (cmdwin_type != 0)
Martin Tournoij7904fa42022-10-04 16:28:45 +01006493 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006494 cmdwin_result = K_IGNORE;
6495 return;
6496 }
6497
6498 if (first_tabpage->tp_next == NULL)
6499 {
6500 msg(_("Already only one tab page"));
6501 return;
6502 }
6503
6504 if (window_layout_locked(CMD_tabonly))
6505 return;
6506
6507 tab_number = get_tabpage_arg(eap);
6508 if (eap->errmsg != NULL)
6509 return;
6510
6511 goto_tabpage(tab_number);
6512 // Repeat this up to a 1000 times, because autocommands may
6513 // mess up the lists.
6514 for (done = 0; done < 1000; ++done)
6515 {
6516 FOR_ALL_TABPAGES(tp)
6517 if (tp->tp_topframe != topframe)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006518 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006519 tabpage_close_other(tp, eap->forceit);
6520 // if we failed to close it quit
6521 if (valid_tabpage(tp))
6522 done = 1000;
6523 // start over, "tp" is now invalid
6524 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006525 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006526 if (first_tabpage->tp_next == NULL)
6527 break;
Martin Tournoij7904fa42022-10-04 16:28:45 +01006528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
6531/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006532 * Close the current tab page.
6533 */
6534 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006535tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006536{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006537 if (window_layout_locked(CMD_tabclose))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006538 return;
6539
Jim Zhoubcf66e02025-03-16 20:24:57 +01006540 trigger_tabclosedpre(curtab, TRUE);
6541
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006542 // First close all the windows but the current one. If that worked then
6543 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006544 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006545 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006546 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006547 ex_win_close(forceit, curwin, NULL);
6548# ifdef FEAT_GUI
6549 need_mouse_correct = TRUE;
6550# endif
6551}
6552
6553/*
6554 * Close tab page "tp", which is not the current tab page.
6555 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006556 * Also takes care of the tab pages line disappearing when closing the
6557 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006558 */
6559 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006560tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006561{
6562 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006563 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006564
Jim Zhoubcf66e02025-03-16 20:24:57 +01006565 trigger_tabclosedpre(tp, TRUE);
6566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006567 // Limit to 1000 windows, autocommands may add a window while we close
6568 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006569 while (++done < 1000)
6570 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006571 wp = tp->tp_firstwin;
6572 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006574 // Autocommands may delete the tab page under our fingers and we may
6575 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006576 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006577 break;
6578 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006579
Bram Moolenaar29323592016-07-24 22:04:11 +02006580 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006581}
6582
6583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 * ":only".
6585 */
6586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006587ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588{
Bram Moolenaar9fda8152022-11-19 13:14:10 +00006589 if (window_layout_locked(CMD_only))
Bram Moolenaard63a8552022-11-19 11:41:30 +00006590 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591# ifdef FEAT_GUI
6592 need_mouse_correct = TRUE;
6593# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006594 if (eap->addr_count > 0)
6595 {
Bram Moolenaard63a8552022-11-19 11:41:30 +00006596 win_T *wp;
6597 int wnr = eap->line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006598 for (wp = firstwin; --wnr > 0; )
6599 {
6600 if (wp->w_next == NULL)
6601 break;
6602 else
6603 wp = wp->w_next;
6604 }
6605 win_goto(wp);
6606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 close_others(TRUE, eap->forceit);
6608}
6609
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006611ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006613 // ":hide" or ":hide | cmd": hide current window
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006614 if (eap->skip)
6615 return;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006616
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006617 if (window_layout_locked(CMD_hide))
6618 return;
6619#ifdef FEAT_GUI
6620 need_mouse_correct = TRUE;
6621#endif
6622 if (eap->addr_count == 0)
6623 win_close(curwin, FALSE); // don't free buffer
6624 else
6625 {
6626 int winnr = 0;
6627 win_T *win;
6628
6629 FOR_ALL_WINDOWS(win)
6630 {
6631 winnr++;
6632 if (winnr == eap->line2)
6633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006635 if (win == NULL)
6636 win = lastwin;
6637 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
6639}
6640
6641/*
6642 * ":stop" and ":suspend": Suspend Vim.
6643 */
dbivolaruab16ad32021-12-29 19:41:47 +00006644 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006645ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 /*
6648 * Disallow suspending for "rvim".
6649 */
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00006650 if (check_restricted())
6651 return;
6652
6653 if (!eap->forceit)
6654 autowrite_all();
6655 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
6656 windgoto((int)Rows - 1, 0);
6657 out_char('\n');
6658 out_flush();
6659 stoptermcap();
6660 out_flush(); // needed for SUN to restore xterm buffer
6661 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
6662 ui_suspend(); // call machine specific function
6663 maketitle();
6664 resettitle(); // force updating the title
6665 starttermcap();
6666 scroll_start(); // scroll screen before redrawing
6667 redraw_later_clear();
6668 shell_resized(); // may have resized window
6669 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670}
6671
6672/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006673 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 */
6675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006676ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006678#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006679 if (not_in_vim9(eap) == FAIL)
6680 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (cmdwin_type != 0)
6683 {
6684 cmdwin_result = Ctrl_C;
6685 return;
6686 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006687 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006688 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006689 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006690 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006691 return;
6692 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006693
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006695 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 */
6697 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6698 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006699
6700 // Write the buffer for ":wq" or when it was changed.
6701 // Trigger QuitPre and ExitPre.
6702 // Check if we can exit now, after autocommands have changed things.
6703 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6704 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006706 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
6708 not_exiting();
6709 }
6710 else
6711 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006712 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006714 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715# ifdef FEAT_GUI
6716 need_mouse_correct = TRUE;
6717# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006718 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006719 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
6721}
6722
6723/*
6724 * ":print", ":list", ":number".
6725 */
6726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006727ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006729 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006730 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006733 for ( ;!got_int; ui_breakcheck())
6734 {
6735 print_line(eap->line1,
6736 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6737 || (eap->flags & EXFLAG_NR)),
6738 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6739 if (++eap->line1 > eap->line2)
6740 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006741 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006742 }
6743 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006744 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006745 curwin->w_cursor.lnum = eap->line2;
6746 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
6748
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750}
6751
6752#ifdef FEAT_BYTEOFF
6753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006754ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 goto_byte(eap->line2);
6757}
6758#endif
6759
6760/*
6761 * ":shell".
6762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006764ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766 do_shell(NULL, 0);
6767}
6768
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006769#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006771static int drop_busy = FALSE;
6772static int drop_filec;
6773static char_u **drop_filev = NULL;
6774static int drop_split;
6775static void (*drop_callback)(void *);
6776static void *drop_cookie;
6777
6778 static void
6779handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 exarg_T ea;
6782 int save_msg_scroll = msg_scroll;
6783
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006784 // Setting the argument list may cause screen updates and being called
6785 // recursively. Avoid that by setting drop_busy.
6786 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006788 // Check whether the current buffer is changed. If so, we will need
6789 // to split the current window or data could be lost.
6790 // We don't need to check if the 'hidden' option is set, as in this
6791 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006792 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
6794 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006795 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 --emsg_off;
6797 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006798 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (win_split(0, 0) == FAIL)
6801 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006802 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006804 // When splitting the window, create a new alist. Otherwise the
6805 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 alist_unlink(curwin->w_alist);
6807 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 }
6809
6810 /*
6811 * Set up the new argument list.
6812 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006813 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
6815 /*
6816 * Move to the first file.
6817 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006818 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006819 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 ea.cmd = (char_u *)"next";
6821 do_argfile(&ea, 0);
6822
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006823 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6824 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 need_start_insertmode = FALSE;
6826
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006827 // Restore msg_scroll, otherwise a following command may cause scrolling
6828 // unexpectedly. The screen will be redrawn by the caller, thus
6829 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006831
6832 if (drop_callback != NULL)
6833 drop_callback(drop_cookie);
6834
6835 drop_filev = NULL;
6836 drop_busy = FALSE;
6837}
6838
6839/*
6840 * Handle a file drop. The code is here because a drop is *nearly* like an
6841 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006842 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006843 * code is very similar to :args and hence needs access to a lot of the static
6844 * functions in this file.
6845 *
6846 * The "filev" list must have been allocated using alloc(), as should each item
6847 * in the list. This function takes over responsibility for freeing the "filev"
6848 * list.
6849 */
6850 void
6851handle_drop(
6852 int filec, // the number of files dropped
6853 char_u **filev, // the list of files dropped
6854 int split, // force splitting the window
6855 void (*callback)(void *), // to be called after setting the argument
6856 // list
6857 void *cookie) // argument for "callback" (allocated)
6858{
6859 // Cannot handle recursive drops, finish the pending one.
6860 if (drop_busy)
6861 {
6862 FreeWild(filec, filev);
6863 vim_free(cookie);
6864 return;
6865 }
6866
6867 // When calling handle_drop() more than once in a row we only use the last
6868 // one.
6869 if (drop_filev != NULL)
6870 {
6871 FreeWild(drop_filec, drop_filev);
6872 vim_free(drop_cookie);
6873 }
6874
6875 drop_filec = filec;
6876 drop_filev = filev;
6877 drop_split = split;
6878 drop_callback = callback;
6879 drop_cookie = cookie;
6880
6881 // Postpone this when:
6882 // - editing the command line
6883 // - not possible to change the current buffer
6884 // - updating the screen
6885 // As it may change buffers and window structures that are in use and cause
6886 // freed memory to be used.
6887 if (text_locked() || curbuf_locked() || updating_screen)
6888 return;
6889
6890 handle_drop_internal();
6891}
6892
6893/*
6894 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6895 * reset: Handle a postponed drop.
6896 */
6897 void
6898handle_any_postponed_drop(void)
6899{
6900 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006901 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006902 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903}
6904#endif
6905
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 * ":preserve".
6908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006910ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006912 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 ml_preserve(curbuf, TRUE);
6914}
6915
6916/*
6917 * ":recover".
6918 */
6919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006920ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006922 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006924 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6925 | CCGD_MULTWIN
6926 | (eap->forceit ? CCGD_FORCEIT : 0)
6927 | CCGD_EXCMD)
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 && (*eap->arg == NUL
6930 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006931 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 recoverymode = FALSE;
6933}
6934
6935/*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006936 * Command modifier used in a wrong way. Also for other commands that can't
6937 * appear at the toplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 */
6939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006940ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941{
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00006942 eap->errmsg = ex_errmsg(e_invalid_command_str, eap->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943}
6944
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02006945#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006946
6947// callback function for 'findfunc'
6948static callback_T ffu_cb;
6949
6950 static callback_T *
6951get_findfunc_callback(void)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006952{
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006953 return *curbuf->b_p_ffu != NUL ? &curbuf->b_ffu_cb : &ffu_cb;
6954}
6955
zeertzjq6eda2692024-11-03 09:23:33 +01006956/*
6957 * Call 'findfunc' to obtain a list of file names.
6958 */
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006959 static list_T *
6960call_findfunc(char_u *pat, int cmdcomplete)
6961{
6962 typval_T args[3];
6963 callback_T *cb;
6964 typval_T rettv;
6965 int retval;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006966 sctx_T saved_sctx = current_sctx;
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006967 sctx_T *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006968
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006969 args[0].v_type = VAR_STRING;
6970 args[0].vval.v_string = pat;
6971 args[1].v_type = VAR_BOOL;
6972 args[1].vval.v_number = cmdcomplete;
6973 args[2].v_type = VAR_UNKNOWN;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006974
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006975 // Lock the text to prevent weird things from happening. Also disallow
6976 // switching to another window, it should not be needed and may end up in
6977 // Insert mode in another buffer.
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006978 ++textlock;
6979
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006980 ctx = get_option_sctx("findfunc");
6981 if (ctx != NULL)
6982 current_sctx = *ctx;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006983
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006984 cb = get_findfunc_callback();
6985 retval = call_callback(cb, -1, &rettv, 2, args);
6986
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006987 current_sctx = saved_sctx;
6988
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01006989 --textlock;
6990
6991 list_T *retlist = NULL;
6992
6993 if (retval == OK)
6994 {
6995 if (rettv.v_type == VAR_LIST)
6996 retlist = list_copy(rettv.vval.v_list, FALSE, FALSE, get_copyID());
6997 else
6998 emsg(_(e_invalid_return_type_from_findfunc));
6999
7000 clear_tv(&rettv);
7001 }
7002
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007003 return retlist;
7004}
7005
7006/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007007 * Find file names matching "pat" using 'findfunc' and return it in "files".
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007008 * Used for expanding the :find, :sfind and :tabfind command argument.
7009 * Returns OK on success and FAIL otherwise.
7010 */
7011 int
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007012expand_findfunc(char_u *pat, char_u ***files, int *numMatches)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007013{
7014 list_T *l;
7015 int len;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007016
7017 *numMatches = 0;
7018 *files = NULL;
7019
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007020 l = call_findfunc(pat, VVAL_TRUE);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02007021
7022 if (l == NULL)
7023 return FAIL;
7024
7025 len = list_len(l);
7026 if (len == 0) // empty List
7027 return FAIL;
7028
7029 *files = ALLOC_MULT(char_u *, len);
7030 if (*files == NULL)
7031 return FAIL;
7032
7033 // Copy all the List items
7034 listitem_T *li;
7035 int idx = 0;
7036 FOR_ALL_LIST_ITEMS(l, li)
7037 {
7038 if (li->li_tv.v_type == VAR_STRING)
7039 {
7040 (*files)[idx] = vim_strsave(li->li_tv.vval.v_string);
7041 idx++;
7042 }
7043 }
7044
7045 *numMatches = idx;
7046 list_free(l);
7047
7048 return OK;
7049}
7050
7051/*
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007052 * Use 'findfunc' to find file 'findarg'. The 'count' argument is used to find
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007053 * the n'th matching file.
7054 */
7055 static char_u *
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007056findfunc_find_file(char_u *findarg, int findarg_len, int count)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007057{
7058 list_T *fname_list;
7059 char_u *ret_fname = NULL;
7060 char_u cc;
7061 int fname_count;
7062
7063 cc = findarg[findarg_len];
7064 findarg[findarg_len] = NUL;
7065
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007066 fname_list = call_findfunc(findarg, VVAL_FALSE);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007067 fname_count = list_len(fname_list);
7068
7069 if (fname_count == 0)
7070 semsg(_(e_cant_find_file_str_in_path), findarg);
7071 else
7072 {
7073 if (count > fname_count)
7074 semsg(_(e_no_more_file_str_found_in_path), findarg);
7075 else
7076 {
7077 listitem_T *li = list_find(fname_list, count - 1);
7078 if (li != NULL && li->li_tv.v_type == VAR_STRING)
7079 ret_fname = vim_strsave(li->li_tv.vval.v_string);
7080 }
7081 }
7082
7083 if (fname_list != NULL)
7084 list_free(fname_list);
7085
7086 findarg[findarg_len] = cc;
7087
7088 return ret_fname;
7089}
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007090
7091/*
7092 * Process the 'findfunc' option value.
7093 * Returns NULL on success and an error message on failure.
7094 */
7095 char *
7096did_set_findfunc(optset_T *args UNUSED)
7097{
7098 int retval;
7099
zeertzjq6eda2692024-11-03 09:23:33 +01007100 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007101 // buffer-local option set
7102 retval = option_set_callback_func(curbuf->b_p_ffu, &curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007103 else
7104 {
7105 // global option set
7106 retval = option_set_callback_func(p_ffu, &ffu_cb);
zeertzjq6eda2692024-11-03 09:23:33 +01007107 // when using :set, free the local callback
7108 if (!(args->os_flags & OPT_GLOBAL))
7109 free_callback(&curbuf->b_ffu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007110 }
7111
7112 if (retval == FAIL)
7113 return e_invalid_argument;
7114
7115 // If the option value starts with <SID> or s:, then replace that with
7116 // the script identifier.
7117 char_u **varp = (char_u **)args->os_varp;
7118 char_u *name = get_scriptlocal_funcname(*varp);
7119 if (name != NULL)
7120 {
7121 free_string_option(*varp);
7122 *varp = name;
7123 }
7124
7125 return NULL;
7126}
7127
7128# if defined(EXITFREE) || defined(PROTO)
7129 void
7130free_findfunc_option(void)
7131{
7132 free_callback(&ffu_cb);
7133}
7134# endif
7135
7136/*
7137 * Mark the global 'findfunc' callback with "copyID" so that it is not
7138 * garbage collected.
7139 */
7140 int
7141set_ref_in_findfunc(int copyID UNUSED)
7142{
7143 int abort = FALSE;
7144 abort = set_ref_in_callback(&ffu_cb, copyID);
7145 return abort;
7146}
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007147#endif
7148
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149/*
7150 * :sview [+command] file split window with new file, read-only
7151 * :split [[+command] file] split window with current or new file
7152 * :vsplit [[+command] file] split window vertically with current or new file
7153 * :new [[+command] file] split window with no or new file
7154 * :vnew [[+command] file] split vertically window with no or new file
7155 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007156 *
7157 * :tabedit open new Tab page with empty window
7158 * :tabedit [+command] file open new Tab page and edit "file"
7159 * :tabnew [[+command] file] just like :tabedit
7160 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 */
7162 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007163ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007165 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007167#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007168 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02007169 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007170#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02007171 int use_tab = eap->cmdidx == CMD_tabedit
7172 || eap->cmdidx == CMD_tabfind
7173 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007175 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007176 return;
7177
Bram Moolenaar4033c552017-09-16 20:54:51 +02007178#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaar4033c552017-09-16 20:54:51 +02007182#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007183 // A ":split" in the quickfix window works like ":new". Don't want two
7184 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02007185 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
7187 if (eap->cmdidx == CMD_split)
7188 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (eap->cmdidx == CMD_vsplit)
7190 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007194 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007196 char_u *file_to_find = NULL;
7197 char *search_ctx = NULL;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007198
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007199 if (*get_findfunc() != NUL)
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007200 {
7201#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007202 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007203 eap->addr_count > 0 ? eap->line2 : 1);
7204#endif
7205 }
7206 else
7207 {
7208 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7209 FNAME_MESS, TRUE, curbuf->b_ffname,
7210 &file_to_find, &search_ctx);
7211 vim_free(file_to_find);
7212 vim_findfile_cleanup(search_ctx);
7213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 if (fname == NULL)
7215 goto theend;
7216 eap->arg = fname;
7217 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007218# ifdef FEAT_BROWSE
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007219 else if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 && eap->cmdidx != CMD_new)
7222 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007223 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007224# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007225 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007226# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007227 au_has_group((char_u *)"FileExplorer"))
7228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007229 // No browsing supported but we do have the file explorer:
7230 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007231 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02007232 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007233 }
7234 else
7235 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02007236 fname = do_browse(0, (char_u *)(use_tab
7237 ? _("Edit File in new tab page")
7238 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007240 if (fname == NULL)
7241 goto theend;
7242 eap->arg = fname;
7243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007245 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02007246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007248 /*
7249 * Either open new tab page or split the window.
7250 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02007251 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007252 {
Bram Moolenaare1004402020-10-24 20:49:43 +02007253 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007254 : eap->addr_count == 0 ? 0
7255 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007256 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007257 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007258
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007259 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007260 if (curwin != old_curwin
7261 && win_valid(old_curwin)
7262 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007263 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007264 old_curwin->w_alt_fnum = curbuf->b_fnum;
7265 }
7266 }
7267 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007270 // Reset 'scrollbind' when editing another file, but keep it when
7271 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02007272 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007273 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 else
7275 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 do_exedit(eap, old_curwin);
7277 }
7278
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007279# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007280 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283theend:
7284 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007286
7287/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007288 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007289 */
7290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007291tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007292{
7293 exarg_T ea;
7294
Bram Moolenaara80faa82020-04-12 19:37:17 +02007295 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007296 ea.cmdidx = CMD_tabnew;
7297 ea.cmd = (char_u *)"tabn";
7298 ea.arg = (char_u *)"";
7299 ex_splitview(&ea);
7300}
7301
7302/*
7303 * :tabnext command
7304 */
7305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007306ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007307{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007308 int tab_number;
7309
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007310 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007311 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007312 switch (eap->cmdidx)
7313 {
7314 case CMD_tabfirst:
7315 case CMD_tabrewind:
7316 goto_tabpage(1);
7317 break;
7318 case CMD_tablast:
7319 goto_tabpage(9999);
7320 break;
7321 case CMD_tabprevious:
7322 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007323 if (eap->arg && *eap->arg != NUL)
7324 {
7325 char_u *p = eap->arg;
7326 char_u *p_save = p;
7327
7328 tab_number = getdigits(&p);
7329 if (p == p_save || *p_save == '-' || *p != NUL
7330 || tab_number == 0)
7331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007332 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007333 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007334 return;
7335 }
7336 }
7337 else
7338 {
7339 if (eap->addr_count == 0)
7340 tab_number = 1;
7341 else
7342 {
7343 tab_number = eap->line2;
7344 if (tab_number < 1)
7345 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007346 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007347 return;
7348 }
7349 }
7350 }
7351 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007352 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007353 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007354 tab_number = get_tabpage_arg(eap);
7355 if (eap->errmsg == NULL)
7356 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007357 break;
7358 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007359}
7360
7361/*
7362 * :tabmove command
7363 */
7364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007365ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007366{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02007367 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007368
Bram Moolenaar2f72c702017-01-29 14:48:10 +01007369 tab_number = get_tabpage_arg(eap);
7370 if (eap->errmsg == NULL)
7371 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007372}
7373
7374/*
7375 * :tabs command: List tabs and their contents.
7376 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007378ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007379{
7380 tabpage_T *tp;
7381 win_T *wp;
7382 int tabcount = 1;
7383
7384 msg_start();
7385 msg_scroll = TRUE;
7386 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7387 {
7388 msg_putchar('\n');
7389 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007390 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007391 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392 ui_breakcheck();
7393
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007394 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007395 wp = firstwin;
7396 else
7397 wp = tp->tp_firstwin;
7398 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7399 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007400 msg_putchar('\n');
7401 msg_putchar(wp == curwin ? '>' : ' ');
7402 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007403 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7404 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007405 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007406 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007407 else
7408 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7409 IObuff, IOSIZE, TRUE);
7410 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007411 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007412 ui_breakcheck();
7413 }
7414 }
7415}
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417/*
7418 * ":mode": Set screen mode.
7419 * If no argument given, just get the screen size and redraw.
7420 */
7421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007422ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423{
7424 if (*eap->arg == NUL)
7425 shell_resized();
7426 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00007427 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428}
7429
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430/*
7431 * ":resize".
7432 * set, increment or decrement current window height
7433 */
7434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007435ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
7437 int n;
7438 win_T *wp = curwin;
7439
7440 if (eap->addr_count > 0)
7441 {
7442 n = eap->line2;
7443 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7444 ;
7445 }
7446
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007447# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02007451 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 {
7453 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007454 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007455 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007457 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
7459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {
7461 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02007462 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007463 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007465 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 }
7467}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
7469/*
7470 * ":find [+command] <file>" command.
7471 */
7472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007473ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
Colin Kennedy21570352024-03-03 16:16:47 +01007475 if (!check_can_set_curbuf_forceit(eap->forceit))
zeertzjqd9be94c2024-07-14 10:20:20 +02007476 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007477
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007478 char_u *fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 int count;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007480 char_u *file_to_find = NULL;
7481 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007483 if (*get_findfunc() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 {
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007485#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01007486 fname = findfunc_find_file(eap->arg, (int)STRLEN(eap->arg),
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007487 eap->addr_count > 0 ? eap->line2 : 1);
7488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007490 else
7491 {
7492 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7493 TRUE, curbuf->b_ffname, &file_to_find, &search_ctx);
7494 if (eap->addr_count > 0)
7495 {
7496 // Repeat finding the file "count" times. This matters when it appears
7497 // several times in the path.
7498 count = eap->line2;
7499 while (fname != NULL && --count > 0)
7500 {
7501 vim_free(fname);
7502 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7503 FALSE, curbuf->b_ffname, &file_to_find, &search_ctx);
7504 }
7505 }
7506 VIM_CLEAR(file_to_find);
7507 vim_findfile_cleanup(search_ctx);
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00007510 if (fname == NULL)
7511 return;
7512
7513 eap->arg = fname;
7514 do_exedit(eap, NULL);
7515 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516}
7517
7518/*
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007519 * ":open" simulation: for now works just like ":visual".
Bram Moolenaardf177f62005-02-22 08:39:57 +00007520 */
7521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007522ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007523{
7524 regmatch_T regmatch;
7525 char_u *p;
7526
Bram Moolenaar65088802021-03-13 21:07:21 +01007527#ifdef FEAT_EVAL
7528 if (not_in_vim9(eap) == FAIL)
7529 return;
7530#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007531 curwin->w_cursor.lnum = eap->line2;
7532 beginline(BL_SOL | BL_FIX);
7533 if (*eap->arg == '/')
7534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007535 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00007536 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007537 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00007538 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007539 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007540 if (regmatch.regprog != NULL)
7541 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00007542 // make a copy of the line, when searching for a mark it might be
7543 // flushed
7544 char_u *line = vim_strsave(ml_get_curline());
7545
Bram Moolenaardf177f62005-02-22 08:39:57 +00007546 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00007547 if (vim_regexec(&regmatch, line, (colnr_T)0))
7548 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007549 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007550 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02007551 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00007552 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007553 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007554 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007555 eap->arg += STRLEN(eap->arg);
7556 }
7557 check_cursor();
7558
7559 eap->cmdidx = CMD_visual;
7560 do_exedit(eap, NULL);
7561}
7562
7563/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007564 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 */
7566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007567ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
Colin Kennedy65e580b2024-03-26 18:29:30 +01007569 char_u *ffname = eap->cmdidx == CMD_enew ? NULL : eap->arg;
7570
Colin Kennedy21570352024-03-03 16:16:47 +01007571 // Exclude commands which keep the window's current buffer
7572 if (
7573 eap->cmdidx != CMD_badd
7574 && eap->cmdidx != CMD_balt
7575 // All other commands must obey 'winfixbuf' / ! rules
Colin Kennedy65e580b2024-03-26 18:29:30 +01007576 && (is_other_file(0, ffname) && !check_can_set_curbuf_forceit(eap->forceit))
7577 )
zeertzjqd9be94c2024-07-14 10:20:20 +02007578 return;
Colin Kennedy21570352024-03-03 16:16:47 +01007579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 do_exedit(eap, NULL);
7581}
7582
7583/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007584 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007587do_exedit(
7588 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007589 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
7591 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007593 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01007595 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
7596 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007597 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 /*
7599 * ":vi" command ends Ex mode.
7600 */
7601 if (exmode_active && (eap->cmdidx == CMD_visual
7602 || eap->cmdidx == CMD_view))
7603 {
7604 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007605 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007607 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007608 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007609 if (global_busy)
7610 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007611 if (eap->nextcmd != NULL)
7612 {
7613 stuffReadbuff(eap->nextcmd);
7614 eap->nextcmd = NULL;
7615 }
7616
7617 if (exmode_was != EXMODE_VIM)
7618 settmode(TMODE_RAW);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007619 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007620 RedrawingDisabled = 0;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007621 int save_nwr = no_wait_return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007622 no_wait_return = 0;
7623 need_wait_return = FALSE;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007624 int save_ms = msg_scroll;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007625 msg_scroll = 0;
7626#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007627 int save_he = hold_gui_events;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007628 hold_gui_events = 0;
7629#endif
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01007630 set_must_redraw(UPD_CLEAR);
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007631 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007632
7633 main_loop(FALSE, TRUE);
7634
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007635 pending_exmode_active = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01007636 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007637 no_wait_return = save_nwr;
7638 msg_scroll = save_ms;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007639#ifdef FEAT_GUI
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00007640 hold_gui_events = save_he;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007641#endif
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 }
7646
7647 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007648 || eap->cmdidx == CMD_tabnew
7649 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007650 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007652 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 setpcmark();
7654 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007655 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7656 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007658 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 || *eap->arg != NUL
7660#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007661 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662#endif
7663 )
7664 {
Bram Moolenaard6211a52022-06-18 19:48:14 +01007665 // Can't edit another file when "textlock" or "curbuf_lock" is set.
7666 // Only ":edit" or ":script" can bring us here, others are stopped
7667 // earlier.
7668 if (*eap->arg != NUL && text_or_buf_locked())
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007669 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 n = readonlymode;
7672 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7673 readonlymode = TRUE;
7674 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007675 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7676 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007677 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7678 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7680 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007681 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7683 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7684 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007685 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007687 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007688 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007689 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7690 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007691 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007693 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 if (old_curwin != NULL)
7695 {
7696 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007697 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007699#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007700 cleanup_T cs;
7701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007702 // Reset the error/interrupt/exception state here so that
7703 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007704 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007705#endif
7706#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007708#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007709 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007710
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007711#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007712 // Restore the error/interrupt/exception state if not
7713 // discarded by a new aborting error, interrupt, or
7714 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007715 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 }
7718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 }
7720 else if (readonlymode && curbuf->b_nwindows == 1)
7721 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007722 // When editing an already visited buffer, 'readonly' won't be set
7723 // but the previous value is kept. With ":view" and ":sview" we
7724 // want the file to be readonly, except when another window is
7725 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 curbuf->b_p_ro = TRUE;
7727 }
7728 readonlymode = n;
7729 }
7730 else
7731 {
7732 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007733 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 if (n != curwin->w_arg_idx_invalid)
7737 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 /*
7741 * if ":split file" worked, set alternate file name in old window to new
7742 * file
7743 */
7744 if (old_curwin != NULL
7745 && *eap->arg != NUL
7746 && curwin != old_curwin
7747 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007748 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007749 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
7752 ex_no_reprint = TRUE;
7753}
7754
7755#ifndef FEAT_GUI
7756/*
7757 * ":gui" and ":gvim" when there is no GUI.
7758 */
7759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007760ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007762 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763}
7764#endif
7765
Bram Moolenaar4f974752019-02-17 17:44:42 +01007766#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007768ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769{
7770 gui_make_tearoff(eap->arg);
7771}
7772#endif
7773
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007774#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7775 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007777ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007779# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7780 if (gui.in_use)
7781 gui_make_popup(eap->arg, eap->forceit);
7782# ifdef FEAT_TERM_POPUP_MENU
7783 else
7784# endif
7785# endif
7786# ifdef FEAT_TERM_POPUP_MENU
7787 pum_make_popup(eap->arg, eap->forceit);
7788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
7790#endif
7791
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007793ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007796 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007798 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799}
7800
7801/*
7802 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7803 * offset.
7804 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007807ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007810 win_T *save_curwin = curwin;
7811 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 long topline;
7813 long y;
7814 linenr_T old_linenr = curwin->w_cursor.lnum;
7815
7816 setpcmark();
7817
7818 /*
7819 * determine max topline
7820 */
7821 if (curwin->w_p_scb)
7822 {
7823 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007824 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
7826 if (wp->w_p_scb && wp->w_buffer)
7827 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007828 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 if (topline > y)
7830 topline = y;
7831 }
7832 }
7833 if (topline < 1)
7834 topline = 1;
7835 }
7836 else
7837 {
7838 topline = 1;
7839 }
7840
7841
7842 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007843 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007845 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {
7847 if (curwin->w_p_scb)
7848 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007849 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 y = topline - curwin->w_topline;
7851 if (y > 0)
7852 scrollup(y, TRUE);
7853 else
7854 scrolldown(-y, TRUE);
7855 curwin->w_scbind_pos = topline;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01007856 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 }
7860 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007861 curwin = save_curwin;
7862 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 if (curwin->w_p_scb)
7864 {
7865 did_syncbind = TRUE;
7866 checkpcmark();
7867 if (old_linenr != curwin->w_cursor.lnum)
7868 {
7869 char_u ctrl_o[2];
7870
7871 ctrl_o[0] = Ctrl_O;
7872 ctrl_o[1] = 0;
7873 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7874 }
7875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876}
7877
7878
7879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007880ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007882 int i;
7883 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7884 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007886 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007888 do_bang(1, eap, FALSE, FALSE, TRUE);
7889 return;
7890 }
7891
7892 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7893 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894
7895#ifdef FEAT_BROWSE
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007896 if (cmdmod.cmod_flags & CMOD_BROWSE)
7897 {
7898 char_u *browseFile;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007900 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
7901 NULL, NULL, NULL, curbuf);
7902 if (browseFile != NULL)
7903 {
7904 i = readfile(browseFile, NULL,
7905 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7906 vim_free(browseFile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 }
7908 else
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007909 i = OK;
7910 }
7911 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007913 if (*eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007915 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 return;
7917 i = readfile(curbuf->b_ffname, curbuf->b_fname,
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007918 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 }
7920 else
7921 {
7922 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7923 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7924 i = readfile(eap->arg, NULL,
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 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007928 if (i != OK)
7929 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007930#if defined(FEAT_EVAL)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007931 if (!aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932#endif
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007933 semsg(_(e_cant_open_file_str), eap->arg);
7934 }
7935 else
7936 {
7937 if (empty && exmode_active)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007938 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007939 // Delete the empty line that remains. Historically ex does
7940 // this but vi doesn't.
7941 if (eap->line2 == 0)
7942 lnum = curbuf->b_ml.ml_line_count;
7943 else
7944 lnum = 1;
7945 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007946 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007947 ml_delete(lnum);
7948 if (curwin->w_cursor.lnum > 1
7949 && curwin->w_cursor.lnum >= lnum)
7950 --curwin->w_cursor.lnum;
7951 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007952 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007953 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00007954 redraw_curbuf_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 }
7956}
7957
Bram Moolenaarea408852005-06-25 22:49:46 +00007958static char_u *prev_dir = NULL;
7959
7960#if defined(EXITFREE) || defined(PROTO)
7961 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007962free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007963{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007964 VIM_CLEAR(prev_dir);
7965 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007966}
7967#endif
7968
Bram Moolenaarf4258302013-06-02 18:20:17 +02007969/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007970 * Get the previous directory for the given chdir scope.
7971 */
7972 static char_u *
7973get_prevdir(cdscope_T scope)
7974{
7975 if (scope == CDSCOPE_WINDOW)
7976 return curwin->w_prevdir;
7977 else if (scope == CDSCOPE_TABPAGE)
7978 return curtab->tp_prevdir;
7979 return prev_dir;
7980}
7981
7982/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007983 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007984 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7985 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007986 */
7987 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007988post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007989{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007990 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007991 // Clear tab local directory for both :cd and :tcd
7992 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007993 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007994 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007995 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007996 char_u *pdir = get_prevdir(scope);
7997
7998 // If still in the global directory, need to remember current
7999 // directory as the global directory.
8000 if (globaldir == NULL && pdir != NULL)
8001 globaldir = vim_strsave(pdir);
8002
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008003 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02008004 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008005 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008006 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008007 curtab->tp_localdir = vim_strsave(NameBuff);
8008 else
8009 curwin->w_localdir = vim_strsave(NameBuff);
8010 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02008011 }
8012 else
8013 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008014 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01008015 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02008016 }
8017
zeertzjq64be6aa2021-11-19 11:59:08 +00008018 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008019 shorten_fnames(TRUE);
8020}
8021
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008022/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008023 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
8024 */
8025 void
8026trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
8027{
8028#ifdef FEAT_EVAL
8029 dict_T *v_event;
8030 save_v_event_T save_v_event;
8031
8032 v_event = get_v_event(&save_v_event);
8033 (void)dict_add_string(v_event, "directory", new_dir);
8034 dict_set_items_ro(v_event);
8035#endif
8036 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
8037#ifdef FEAT_EVAL
8038 restore_v_event(v_event, &save_v_event);
8039#endif
8040}
8041
8042/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008043 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02008044 * chdir() function.
8045 * scope == CDSCOPE_WINDOW: changes the window-local directory
8046 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
8047 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008048 * Returns TRUE if the directory is successfully changed.
8049 */
8050 int
8051changedir_func(
8052 char_u *new_dir,
8053 int forceit,
8054 cdscope_T scope)
8055{
Bram Moolenaar002bc792020-06-05 22:33:42 +02008056 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008057 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008058 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00008059 char_u **pp;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008060 char_u *tofree;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008061
Bram Moolenaar7cc96922020-01-31 22:41:38 +01008062 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008063 return FALSE;
8064
8065 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
8066 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00008067 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008068 return FALSE;
8069 }
8070
8071 // ":cd -": Change to previous directory
8072 if (STRCMP(new_dir, "-") == 0)
8073 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02008074 pdir = get_prevdir(scope);
8075 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008076 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008077 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008078 return FALSE;
8079 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02008080 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008081 }
8082
8083 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008084 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02008085 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008086 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02008087 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008088
Bakudankun29f3a452021-12-11 12:28:08 +00008089 // For UNIX ":cd" means: go to home directory.
8090 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008091#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008092 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00008093#else
8094 if (*new_dir == NUL && p_cdh)
8095#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008096 {
8097 // use NameBuff for home directory name
8098# ifdef VMS
8099 char_u *p;
8100
8101 p = mch_getenv((char_u *)"SYS$LOGIN");
8102 if (p == NULL || *p == NUL) // empty is the same as not set
8103 NameBuff[0] = NUL;
8104 else
8105 vim_strncpy(NameBuff, p, MAXPATHL - 1);
8106# else
8107 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8108# endif
8109 new_dir = NameBuff;
8110 }
zeertzjqf38aad82021-12-30 13:45:57 +00008111 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008112 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
8113 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00008114 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008115 if (scope == CDSCOPE_WINDOW)
8116 acmd_fname = (char_u *)"window";
8117 else if (scope == CDSCOPE_TABPAGE)
8118 acmd_fname = (char_u *)"tabpage";
8119 else
8120 acmd_fname = (char_u *)"global";
8121 trigger_DirChangedPre(acmd_fname, new_dir);
8122
8123 if (vim_chdir(new_dir))
8124 {
8125 emsg(_(e_command_failed));
8126 vim_free(pdir);
8127 return FALSE;
8128 }
Richard Doty3d0abad2021-12-29 14:39:08 +00008129 }
zeertzjq73257142022-02-02 13:16:37 +00008130
8131 if (scope == CDSCOPE_WINDOW)
8132 pp = &curwin->w_prevdir;
8133 else if (scope == CDSCOPE_TABPAGE)
8134 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008135 else
zeertzjq73257142022-02-02 13:16:37 +00008136 pp = &prev_dir;
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008137 tofree = *pp; // new_dir may use this
zeertzjq73257142022-02-02 13:16:37 +00008138 *pp = pdir;
8139
8140 post_chdir(scope);
8141
8142 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00008143 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
Bram Moolenaard8c9d322022-06-12 11:49:16 +01008144 vim_free(tofree);
zeertzjq73257142022-02-02 13:16:37 +00008145 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008146}
Bram Moolenaarea408852005-06-25 22:49:46 +00008147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02008149 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008152ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01008154 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
8156 new_dir = eap->arg;
8157#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00008158 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
8159 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008161 ex_pwd(NULL);
8162 return;
8163 }
8164#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008165
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008166 cdscope_T scope = CDSCOPE_GLOBAL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02008167
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008168 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8169 scope = CDSCOPE_WINDOW;
8170 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
8171 scope = CDSCOPE_TABPAGE;
8172
8173 if (changedir_func(new_dir, eap->forceit, scope))
8174 {
8175 // Echo the new current directory if the command was typed.
8176 if (KeyTyped || p_verbose >= 5)
8177 ex_pwd(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
8179}
8180
8181/*
8182 * ":pwd".
8183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008185ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186{
8187 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8188 {
8189#ifdef BACKSLASH_IN_FILENAME
8190 slash_adjust(NameBuff);
8191#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02008192 if (p_verbose > 0)
8193 {
8194 char *context = "global";
8195
Bram Moolenaar05268152021-11-18 18:53:45 +00008196 if (last_chdir_reason != NULL)
8197 context = last_chdir_reason;
8198 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02008199 context = "window";
8200 else if (curtab->tp_localdir != NULL)
8201 context = "tabpage";
8202 smsg("[%s] %s", context, (char *)NameBuff);
8203 }
8204 else
8205 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 }
8207 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00008208 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209}
8210
8211/*
8212 * ":=".
8213 */
8214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008215ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008217 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008218 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219}
8220
8221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008222ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008224 int n;
8225 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226
8227 if (cursor_valid())
8228 {
8229 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8230 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00008231 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008233
8234 len = eap->line2;
8235 switch (*eap->arg)
8236 {
8237 case 'm': break;
8238 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008239 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008240 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008241
8242 // Hide the cursor if invoked with !
8243 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244}
8245
8246/*
8247 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008248 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 */
8250 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008251do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252{
Bram Moolenaar52953192019-08-16 10:27:13 +02008253 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01008254 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02008255# ifdef ELAPSED_FUNC
8256 elapsed_T start_tv;
8257
8258 // Remember at what time we started, so that we know how much longer we
8259 // should wait after waiting for a bit.
8260 ELAPSED_INIT(start_tv);
8261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008263 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008264 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008265 else
Richard Doty3d0abad2021-12-29 14:39:08 +00008266 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01008267
Bram Moolenaarf45d7472018-09-30 18:22:26 +02008268 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008269 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01008271 wait_now = msec - done > 1000L ? 1000L : msec - done;
8272#ifdef FEAT_TIMERS
8273 {
8274 long due_time = check_due_timer();
8275
8276 if (due_time > 0 && due_time < wait_now)
8277 wait_now = due_time;
8278 }
8279#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008280#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02008281 if (has_any_channel() && wait_now > 20L)
8282 wait_now = 20L;
8283#endif
8284#ifdef FEAT_SOUND
8285 if (has_any_sound_callback() && wait_now > 20L)
8286 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008287#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01008288 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02008289
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02008290#ifdef FEAT_JOB_CHANNEL
8291 if (has_any_channel())
8292 ui_breakcheck_force(TRUE);
8293 else
8294#endif
8295 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008296#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02008297 // Process the netbeans and clientserver messages that may have been
8298 // received in the call to ui_breakcheck() when the GUI is in use. This
8299 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02008300 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008301#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02008302
8303# ifdef ELAPSED_FUNC
8304 // actual time passed
8305 done = ELAPSED_FUNC(start_tv);
8306# else
8307 // guestimate time passed (will actually be more)
8308 done += wait_now;
8309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02008311
8312 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
8313 // input buffer, otherwise a following call to input() fails.
8314 if (got_int)
8315 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02008316
8317 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00008318 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319}
8320
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321/*
8322 * ":winsize" command (obsolete).
8323 */
8324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008325ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326{
8327 int w, h;
8328 char_u *arg = eap->arg;
8329 char_u *p;
8330
Keith Thompson184f71c2024-01-04 21:19:04 +01008331 if (!SAFE_isdigit(*arg))
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008332 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008333 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01008334 return;
8335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 w = getdigits(&arg);
8337 arg = skipwhite(arg);
8338 p = arg;
8339 h = getdigits(&arg);
8340 if (*p != NUL && *arg == NUL)
8341 set_shellsize(w, h, TRUE);
8342 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008343 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344}
8345
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008347ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348{
8349 int xchar = NUL;
8350 char_u *p;
8351
8352 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8353 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008354 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 if (eap->arg[1] == NUL)
8356 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008357 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 return;
8359 }
8360 xchar = eap->arg[1];
8361 p = eap->arg + 2;
8362 }
8363 else
8364 p = eap->arg + 1;
8365
Bram Moolenaar63b91732021-08-05 20:40:03 +02008366 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02008368 if (*p != NUL && *p != (
8369#ifdef FEAT_EVAL
8370 in_vim9script() ? '#' :
8371#endif
8372 '"')
8373 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008374 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008375 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008377 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02008378 postponed_split_flags = cmdmod.cmod_split;
8379 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8381 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008382 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 }
8384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385
Bram Moolenaar843ee412004-06-30 16:16:41 +00008386#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387/*
8388 * ":winpos".
8389 */
8390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008391ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392{
8393 int x, y;
8394 char_u *arg = eap->arg;
8395 char_u *p;
8396
8397 if (*arg == NUL)
8398 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008399# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008400# ifdef VIMDLL
8401 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
8402 mch_get_winpos(&x, &y) != FAIL)
8403# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008405# else
8406 if (mch_get_winpos(&x, &y) != FAIL)
8407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {
8409 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008410 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 }
8412 else
8413# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00008414 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 }
8416 else
8417 {
8418 x = getdigits(&arg);
8419 arg = skipwhite(arg);
8420 p = arg;
8421 y = getdigits(&arg);
8422 if (*p == NUL || *arg != NUL)
8423 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00008424 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 return;
8426 }
8427# ifdef FEAT_GUI
8428 if (gui.in_use)
8429 gui_mch_set_winpos(x, y);
8430 else if (gui.starting)
8431 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008432 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 gui_win_x = x;
8434 gui_win_y = y;
8435 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008436# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 else
8438# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008439# endif
8440# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00008441 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442# endif
8443# ifdef HAVE_TGETENT
8444 if (*T_CWP)
8445 term_set_winpos(x, y);
8446# endif
8447 }
8448}
8449#endif
8450
8451/*
8452 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8453 */
8454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008455ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
8457 oparg_T oa;
8458
8459 clear_oparg(&oa);
8460 oa.regname = eap->regname;
8461 oa.start.lnum = eap->line1;
8462 oa.end.lnum = eap->line2;
8463 oa.line_count = eap->line2 - eap->line1 + 1;
8464 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008466 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {
8468 setpcmark();
8469 curwin->w_cursor.lnum = eap->line1;
8470 beginline(BL_SOL | BL_FIX);
8471 }
8472
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008473 if (VIsual_active)
8474 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008475
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 switch (eap->cmdidx)
8477 {
8478 case CMD_delete:
8479 oa.op_type = OP_DELETE;
8480 op_delete(&oa);
8481 break;
8482
8483 case CMD_yank:
8484 oa.op_type = OP_YANK;
8485 (void)op_yank(&oa, FALSE, TRUE);
8486 break;
8487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008488 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02008489 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008491 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8492#else
8493 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008495 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 oa.op_type = OP_RSHIFT;
8497 else
8498 oa.op_type = OP_LSHIFT;
8499 op_shift(&oa, FALSE, eap->amount);
8500 break;
8501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008503 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504}
8505
8506/*
8507 * ":put".
8508 */
8509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008510ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008512 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 if (eap->line2 == 0)
8514 {
8515 eap->line2 = 1;
8516 eap->forceit = TRUE;
8517 }
8518 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00008519 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008520 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00008521 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522}
8523
8524/*
64-bitmane08f10a2025-03-18 22:14:34 +01008525 * ":iput".
8526 */
8527 static void
8528ex_iput(exarg_T *eap)
8529{
8530 // ":0iput" works like ":1iput!".
8531 if (eap->line2 == 0)
8532 {
8533 eap->line2 = 1;
8534 eap->forceit = TRUE;
8535 }
8536 curwin->w_cursor.lnum = eap->line2;
8537 check_cursor_col();
8538 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
8539 PUT_LINE|PUT_CURSLINE
8540 |PUT_FIXINDENT);
8541}
8542
8543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 * Handle ":copy" and ":move".
8545 */
8546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008547ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 long n;
8550
Bram Moolenaar491799b2020-08-01 19:23:43 +02008551#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008552 if (not_in_vim9(eap) == FAIL)
8553 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02008554#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008555 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008556 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {
8558 eap->nextcmd = NULL;
8559 return;
8560 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008561 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562
8563 /*
8564 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8565 */
8566 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8567 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008568 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 return;
8570 }
8571
8572 if (eap->cmdidx == CMD_move)
8573 {
8574 if (do_move(eap->line1, eap->line2, n) == FAIL)
8575 return;
8576 }
8577 else
8578 ex_copy(eap->line1, eap->line2, n);
8579 u_clearline();
8580 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008581 ex_may_print(eap);
8582}
8583
8584/*
8585 * Print the current line if flags were given to the Ex command.
8586 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008587 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008588ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008589{
8590 if (eap->flags != 0)
8591 {
8592 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8593 (eap->flags & EXFLAG_LIST));
8594 ex_no_reprint = TRUE;
8595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596}
8597
8598/*
8599 * ":smagic" and ":snomagic".
8600 */
8601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008602ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008604 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008606 magic_overruled = eap->cmdidx == CMD_smagic
8607 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02008608 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008609 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610}
8611
8612/*
8613 * ":join".
8614 */
8615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008616ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617{
8618 curwin->w_cursor.lnum = eap->line1;
8619 if (eap->line1 == eap->line2)
8620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008621 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 return;
8623 if (eap->line2 == curbuf->b_ml.ml_line_count)
8624 {
8625 beep_flush();
8626 return;
8627 }
8628 ++eap->line2;
8629 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008630 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008632 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633}
8634
8635/*
8636 * ":[addr]@r" or ":[addr]*r": execute register
8637 */
8638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008639ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
8641 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008642 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643
8644 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008645 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
8647#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008648 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649#endif
8650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008651 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 c = *eap->arg;
8653 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8654 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008655 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008656 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8657 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008658 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 beep_flush();
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008660 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008663 int save_efr = exec_from_reg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008665 exec_from_reg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00008667 /*
8668 * Execute from the typeahead buffer.
8669 * Continue until the stuff buffer is empty and all added characters
8670 * have been consumed.
8671 */
8672 while (!stuff_empty() || typebuf.tb_len > prev_len)
8673 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8674
8675 exec_from_reg = save_efr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676}
8677
8678/*
8679 * ":!".
8680 */
8681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008682ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
8684 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8685}
8686
8687/*
8688 * ":undo".
8689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008691ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008693 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008694 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008695 else
8696 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697}
8698
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008699#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008701ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008702{
8703 char_u hash[UNDO_HASH_SIZE];
8704
8705 u_compute_hash(hash);
8706 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8707}
8708
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008710ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008711{
8712 char_u hash[UNDO_HASH_SIZE];
8713
8714 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008715 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008716}
8717#endif
8718
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719/*
8720 * ":redo".
8721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008723ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725 u_redo(1);
8726}
8727
8728/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008729 * ":earlier" and ":later".
8730 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008732ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008733{
8734 long count = 0;
8735 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008736 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008737 char_u *p = eap->arg;
8738
8739 if (*p == NUL)
8740 count = 1;
Keith Thompson184f71c2024-01-04 21:19:04 +01008741 else if (SAFE_isdigit(*p))
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008742 {
8743 count = getdigits(&p);
8744 switch (*p)
8745 {
8746 case 's': ++p; sec = TRUE; break;
8747 case 'm': ++p; sec = TRUE; count *= 60; break;
8748 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008749 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8750 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008751 }
8752 }
8753
8754 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008755 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008756 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008757 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8758 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008759}
8760
8761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 * ":redir": start/stop redirection.
8763 */
8764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008765ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
8767 char *mode;
8768 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008769 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770
Bram Moolenaarba768492016-07-08 15:32:54 +02008771#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008772 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008773 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008774 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008775 return;
8776 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008777#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008778
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 if (STRICMP(eap->arg, "END") == 0)
8780 close_redir();
8781 else
8782 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008783 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008785 ++arg;
8786 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008788 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 mode = "a";
8790 }
8791 else
8792 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008793 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794
8795 close_redir();
8796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008797 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008798 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 if (fname == NULL)
8800 return;
8801#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008802 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
8804 char_u *browseFile;
8805
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008806 browseFile = do_browse(BROWSE_SAVE,
8807 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008808 fname, NULL, NULL,
8809 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008811 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 vim_free(fname);
8813 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008814 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 }
8816#endif
8817
8818 redir_fd = open_exfile(fname, eap->forceit, mode);
8819 vim_free(fname);
8820 }
8821#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008822 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008824 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008826 ++arg;
8827 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008829 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008830 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008832 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008834 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008835 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008836 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008837 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008839 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008840 if (*arg == '>')
8841 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008842 // Make register empty when not using @A-@Z and the
8843 // command is valid.
Keith Thompson184f71c2024-01-04 21:19:04 +01008844 if (*arg == NUL && !SAFE_isupper(redir_reg))
Bram Moolenaarc188b882007-10-19 14:20:54 +00008845 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008847 }
8848 if (*arg != NUL)
8849 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008850 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008851 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008852 }
8853 }
8854 else if (*arg == '=' && arg[1] == '>')
8855 {
8856 int append;
8857
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008858 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008859 close_redir();
8860 arg += 2;
8861
8862 if (*arg == '>')
8863 {
8864 ++arg;
8865 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 }
8867 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008868 append = FALSE;
8869
8870 if (var_redir_start(skipwhite(arg), append) == OK)
8871 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 }
8873#endif
8874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008875 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008878 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008880
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008881 // Make sure redirection is not off. Can happen for cmdline completion
8882 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008883 if (redir_fd != NULL
8884#ifdef FEAT_EVAL
8885 || redir_reg || redir_vname
8886#endif
8887 )
8888 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889}
8890
8891/*
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008892 * ":redraw": force redraw, with clear for ":redraw!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008894 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008895ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008897 redraw_cmd(eap->forceit);
8898}
8899
8900/*
8901 * ":redraw": force redraw, with clear if "clear" is TRUE.
8902 */
8903 void
8904redraw_cmd(int clear)
8905{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008906 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008908
8909 int save_p_lz = p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008911
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008912 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 update_topline();
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01008914 update_screen(clear ? UPD_CLEAR : VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 if (need_maketitle)
8916 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008917#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8918# ifdef VIMDLL
8919 if (!gui.in_use)
8920# endif
8921 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008922#endif
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008923 RedrawingDisabled = save_RedrawingDisabled;
8924 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925
Bram Moolenaar5017c662022-04-07 18:06:08 +01008926 // After drawing the statusline screen_attr may still be set.
8927 screen_stop_highlight();
8928
Bram Moolenaara2a89732022-08-31 14:46:18 +01008929 // Reset msg_didout, so that a message that's there is overwritten.
8930 msg_didout = FALSE;
8931 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008933 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008934 need_wait_return = FALSE;
8935
Bram Moolenaara653e532022-04-19 11:38:24 +01008936 // When invoked from a callback or autocmd the command line may be active.
Bram Moolenaar24959102022-05-07 20:01:16 +01008937 if (State & MODE_CMDLINE)
Bram Moolenaara653e532022-04-19 11:38:24 +01008938 redrawcmdline();
8939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 out_flush();
8941}
8942
8943/*
8944 * ":redrawstatus": force redraw of status line(s)
8945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008947ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 if (eap->forceit)
8950 status_redraw_all();
8951 else
8952 status_redraw_curbuf();
zeertzjq320d9102022-09-20 17:12:13 +01008953 if (msg_scrolled && (State & MODE_CMDLINE))
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008954 return; // redraw later
8955
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008956 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008957 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008958
8959 int save_p_lz = p_lz;
Bram Moolenaarbcd69242022-09-19 21:16:12 +01008960 p_lz = FALSE;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008961
zeertzjq320d9102022-09-20 17:12:13 +01008962 if (State & MODE_CMDLINE)
8963 redraw_statuslines();
8964 else
8965 update_screen(VIsual_active ? UPD_INVERTED : 0);
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008966 RedrawingDisabled = save_RedrawingDisabled;
8967 p_lz = save_p_lz;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969}
8970
Bram Moolenaare12bab32019-01-08 22:02:56 +01008971/*
8972 * ":redrawtabline": force redraw of the tabline
8973 */
8974 static void
8975ex_redrawtabline(exarg_T *eap UNUSED)
8976{
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008977 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008978 RedrawingDisabled = 0;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008979
8980 int save_p_lz = p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008981 p_lz = FALSE;
8982
8983 draw_tabline();
8984
Bram Moolenaar79cdf022023-05-20 14:07:00 +01008985 RedrawingDisabled = save_RedrawingDisabled;
8986 p_lz = save_p_lz;
Bram Moolenaare12bab32019-01-08 22:02:56 +01008987 out_flush();
8988}
8989
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008991close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992{
8993 if (redir_fd != NULL)
8994 {
8995 fclose(redir_fd);
8996 redir_fd = NULL;
8997 }
8998#ifdef FEAT_EVAL
8999 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009000 if (redir_vname)
9001 {
9002 var_redir_stop();
9003 redir_vname = 0;
9004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005#endif
9006}
9007
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02009008#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009009 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02009010vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009011{
9012 if (vim_mkdir(name, prot) != 0)
9013 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00009014 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009015 return FAIL;
9016 }
9017 return OK;
9018}
9019#endif
9020
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021/*
9022 * Open a file for writing for an Ex command, with some checks.
9023 * Return file descriptor, or NULL on failure.
9024 */
9025 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009026open_exfile(
9027 char_u *fname,
9028 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009029 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
9031 FILE *fd;
9032
9033#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 if (mch_isdir(fname))
9036 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01009037 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 return NULL;
9039 }
9040#endif
9041 if (!forceit && *mode != 'a' && vim_fexists(fname))
9042 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00009043 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 return NULL;
9045 }
9046
9047 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00009048 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049
9050 return fd;
9051}
9052
9053/*
9054 * ":mark" and ":k".
9055 */
9056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009057ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 pos_T pos;
9060
Bram Moolenaar10b94212021-02-19 21:42:57 +01009061#ifdef FEAT_EVAL
9062 if (not_in_vim9(eap) == FAIL)
9063 return;
9064#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009065 if (*eap->arg == NUL) // No argument?
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009067 emsg(_(e_argument_required));
9068 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00009070
9071 if (eap->arg[1] != NUL) // more than one character?
9072 {
9073 semsg(_(e_trailing_characters_str), eap->arg);
9074 return;
9075 }
9076
9077 pos = curwin->w_cursor; // save curwin->w_cursor
9078 curwin->w_cursor.lnum = eap->line2;
9079 beginline(BL_WHITE | BL_FIX);
9080 if (setmark(*eap->arg) == FAIL) // set mark
9081 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
9082 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083}
9084
9085/*
9086 * Update w_topline, w_leftcol and the cursor position.
9087 */
9088 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009089update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009091 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 update_topline();
9093 if (!curwin->w_p_wrap)
9094 validate_cursor();
9095 update_curswant();
9096}
9097
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009099 * Save the current State and go to Normal mode.
9100 * Return TRUE if the typeahead could be saved.
9101 */
9102 int
9103save_current_state(save_state_T *sst)
9104{
9105 sst->save_msg_scroll = msg_scroll;
9106 sst->save_restart_edit = restart_edit;
9107 sst->save_msg_didout = msg_didout;
9108 sst->save_State = State;
9109 sst->save_insertmode = p_im;
9110 sst->save_finish_op = finish_op;
9111 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009112 sst->save_reg_executing = reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009113 sst->save_pending_end_reg_executing = pending_end_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009114
Bram Moolenaar4324d872020-12-01 20:12:24 +01009115 msg_scroll = FALSE; // no msg scrolling in Normal mode
9116 restart_edit = 0; // don't go to Insert mode
9117 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01009118
Bram Moolenaara452b802020-12-01 21:47:59 +01009119 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01009120 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009121
9122 /*
9123 * Save the current typeahead. This is required to allow using ":normal"
9124 * from an event handler and makes sure we don't hang when the argument
9125 * ends with half a command.
9126 */
9127 save_typeahead(&sst->tabuf);
9128 return sst->tabuf.typebuf_valid;
9129}
9130
9131 void
9132restore_current_state(save_state_T *sst)
9133{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009134 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02009135 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009136
9137 msg_scroll = sst->save_msg_scroll;
9138 restart_edit = sst->save_restart_edit;
9139 p_im = sst->save_insertmode;
9140 finish_op = sst->save_finish_op;
9141 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01009142 reg_executing = sst->save_reg_executing;
zeertzjq6d4e7252022-04-07 13:58:04 +01009143 pending_end_reg_executing = sst->save_pending_end_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009144 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01009145 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009147 // Restore the state (needed when called from a function executed for
9148 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009149 State = sst->save_State;
9150#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009151 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009152#endif
9153}
9154
9155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 * ":normal[!] {commands}": Execute normal mode commands.
9157 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01009158 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009159ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009161 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 char_u *arg = NULL;
9163 int l;
9164 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009166 if (ex_normal_lock > 0)
9167 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00009168 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009169 return;
9170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (ex_normal_busy >= p_mmd)
9172 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009173 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 return;
9175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 /*
9178 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9179 * this for the K_SPECIAL leading byte, otherwise special keys will not
9180 * work.
9181 */
9182 if (has_mbyte)
9183 {
9184 int len = 0;
9185
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009186 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 for (p = eap->arg; *p != NUL; ++p)
9188 {
Bram Moolenaar13505972019-01-24 15:04:48 +01009189#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009190 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01009192#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009193 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009194 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009195#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01009197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 )
9199 len += 2;
9200 }
9201 if (len > 0)
9202 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009203 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 if (arg != NULL)
9205 {
9206 len = 0;
9207 for (p = eap->arg; *p != NUL; ++p)
9208 {
9209 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01009210#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 if (*p == CSI)
9212 {
9213 arg[len++] = KS_EXTRA;
9214 arg[len++] = (int)KE_CSI;
9215 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009216#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009217 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 {
9219 arg[len++] = *++p;
9220 if (*p == K_SPECIAL)
9221 {
9222 arg[len++] = KS_SPECIAL;
9223 arg[len++] = KE_FILLER;
9224 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009225#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 else if (*p == CSI)
9227 {
9228 arg[len++] = KS_EXTRA;
9229 arg[len++] = (int)KE_CSI;
9230 }
Bram Moolenaar13505972019-01-24 15:04:48 +01009231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 }
9233 arg[len] = NUL;
9234 }
9235 }
9236 }
9237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009239 ++ex_normal_busy;
9240 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 {
9242 /*
9243 * Repeat the :normal command for each line in the range. When no
9244 * range given, execute it just once, without positioning the cursor
9245 * first.
9246 */
9247 do
9248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 if (eap->addr_count != 0)
9250 {
9251 curwin->w_cursor.lnum = eap->line1++;
9252 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02009253 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 }
9255
Bram Moolenaar13505972019-01-24 15:04:48 +01009256 exec_normal_cmd(arg != NULL
9257 ? arg
9258 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 }
9260 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9261 }
9262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009263 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 update_topline_cursor();
9265
Bram Moolenaara21a6a92017-09-23 16:33:50 +02009266 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01009268 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01009269#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009270 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01009271#endif
9272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274}
9275
9276/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009277 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 */
9279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009280ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009282 if (eap->forceit)
9283 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009284 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02009285 if (!curwin->w_cursor.lnum)
9286 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009287 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00009288 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02009289#ifdef FEAT_TERMINAL
9290 // Ignore this when running in an active terminal.
9291 if (term_job_running(curbuf->b_term))
9292 return;
9293#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00009294
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009295 // Ignore the command when already in Insert mode. Inserting an
9296 // expression register that invokes a function can do this.
Bram Moolenaar24959102022-05-07 20:01:16 +01009297 if (State & MODE_INSERT)
Bram Moolenaara40c5002005-01-09 21:16:21 +00009298 return;
9299
Bram Moolenaar64969662005-12-14 21:59:55 +00009300 if (eap->cmdidx == CMD_startinsert)
9301 restart_edit = 'a';
9302 else if (eap->cmdidx == CMD_startreplace)
9303 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009305 restart_edit = 'V';
9306
9307 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009309 if (eap->cmdidx == CMD_startinsert)
9310 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009311 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01009313
9314 if (VIsual_active)
9315 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316}
9317
9318/*
9319 * ":stopinsert"
9320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009322ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323{
9324 restart_edit = 0;
9325 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02009326 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009329/*
9330 * Execute normal mode command "cmd".
9331 * "remap" can be REMAP_NONE or REMAP_YES.
9332 */
9333 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009334exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009335{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009336 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01009337 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009338 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01009339}
Bram Moolenaar25281632016-01-21 23:32:32 +01009340
Bram Moolenaar25281632016-01-21 23:32:32 +01009341/*
9342 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009343 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009344 */
9345 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009346exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009347{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009348 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009349 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009350
Bram Moolenaar905dd902019-04-07 14:21:47 +02009351 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9352 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009353 clear_oparg(&oa);
9354 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009355 while ((!stuff_empty()
9356 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009357 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009358 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009359 {
9360 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009361#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009362 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009363 && oa.op_type == OP_NOP && oa.regname == NUL
9364 && !VIsual_active)
9365 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009366 // If terminal_loop() returns OK we got a key that is handled
9367 // in Normal model. With FAIL we first need to position the
9368 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009369 if (terminal_loop(TRUE) == OK)
9370 normal_cmd(&oa, TRUE);
9371 }
9372 else
9373#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009374 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009375 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009376 }
9377}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009378
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379#ifdef FEAT_FIND_ID
9380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009381ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9384 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
Colin Kennedy21570352024-03-03 16:16:47 +01009385 (linenr_T)1, (linenr_T)MAXLNUM, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386}
9387
Bram Moolenaar4033c552017-09-16 20:54:51 +02009388#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389/*
9390 * ":psearch"
9391 */
9392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009393ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394{
9395 g_do_tagpreview = p_pvh;
9396 ex_findpat(eap);
9397 g_do_tagpreview = 0;
9398}
9399#endif
9400
9401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009402ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403{
9404 int whole = TRUE;
9405 long n;
9406 char_u *p;
9407 int action;
9408
9409 switch (cmdnames[eap->cmdidx].cmd_name[2])
9410 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009411 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9413 action = ACTION_GOTO;
9414 else
9415 action = ACTION_SHOW;
9416 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009417 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 action = ACTION_SHOW_ALL;
9419 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009420 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 action = ACTION_GOTO;
9422 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009423 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 action = ACTION_SPLIT;
9425 break;
9426 }
9427
9428 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009429 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 {
9431 n = getdigits(&eap->arg);
9432 eap->arg = skipwhite(eap->arg);
9433 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009434 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 {
9436 whole = FALSE;
9437 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01009438 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 if (*p)
9440 {
9441 *p++ = NUL;
9442 p = skipwhite(p);
9443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009444 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02009445 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00009446 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02009448 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 }
9450 }
9451 if (!eap->skip)
9452 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9453 whole, !eap->forceit,
9454 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
Colin Kennedy21570352024-03-03 16:16:47 +01009455 n, action, eap->line1, eap->line2, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456}
9457#endif
9458
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459
Bram Moolenaar4033c552017-09-16 20:54:51 +02009460#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461/*
9462 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9463 */
9464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009465ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009467 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9469}
9470
9471/*
9472 * ":pedit"
9473 */
9474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009475ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 win_T *curwin_save = curwin;
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009478 prepare_preview_window();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009480 // Edit the file.
9481 do_exedit(eap, NULL);
9482
9483 back_to_current_window(curwin_save);
9484}
9485
9486/*
9487 * ":pbuffer"
9488 */
9489 static void
9490ex_pbuffer(exarg_T *eap)
9491{
9492 win_T *curwin_save = curwin;
9493 prepare_preview_window();
9494
9495 // Go to the buffer.
9496 do_exbuffer(eap);
9497
9498 back_to_current_window(curwin_save);
9499}
9500
9501 static void
9502prepare_preview_window(void)
9503{
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01009504 if (ERROR_IF_ANY_POPUP_WINDOW)
9505 return;
9506
Bram Moolenaar026587b2019-08-17 15:08:00 +02009507 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02009509 prepare_tagpreview(TRUE, TRUE, FALSE);
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009510}
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009511
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01009512 static void
9513back_to_current_window(win_T *curwin_save)
9514{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 if (curwin != curwin_save && win_valid(curwin_save))
9516 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02009517 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01009519 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 win_enter(curwin_save, TRUE);
9521 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01009522# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02009523 else if (WIN_IS_POPUP(curwin))
9524 {
9525 // can't keep focus in popup window
9526 win_enter(firstwin, TRUE);
9527 }
9528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 g_do_tagpreview = 0;
9530}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
9533/*
9534 * ":stag", ":stselect" and ":stjump".
9535 */
9536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009537ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02009540 postponed_split_flags = cmdmod.cmod_split;
9541 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9543 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009544 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546
9547/*
9548 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9549 */
9550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009551ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9554}
9555
9556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009557ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
9559 int cmd;
9560
9561 switch (name[1])
9562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009563 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009565 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009567 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009569 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009571 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009573 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009575 case 'f': // ":tfirst"
9576 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009578 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009580 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009582 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009584 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 return;
9586 }
9587#endif
9588 cmd = DT_TAG;
9589 break;
9590 }
9591
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009592 if (name[0] == 'l')
9593 {
9594#ifndef FEAT_QUICKFIX
9595 ex_ni(eap);
9596 return;
9597#else
9598 cmd = DT_LTAG;
9599#endif
9600 }
9601
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9603 eap->forceit, TRUE);
9604}
9605
John Marriotted908f72024-04-18 22:46:56 +02009606enum {
9607 SPEC_PERC = 0,
9608 SPEC_HASH,
9609 SPEC_CWORD, // cursor word
9610 SPEC_CCWORD, // cursor WORD
9611 SPEC_CEXPR, // expr under cursor
9612 SPEC_CFILE, // cursor path name
9613 SPEC_SFILE, // ":so" file name
9614 SPEC_SLNUM, // ":so" file line number
9615 SPEC_STACK, // call stack
9616 SPEC_SCRIPT, // script file name
9617 SPEC_AFILE, // autocommand file name
9618 SPEC_ABUF, // autocommand buffer number
9619 SPEC_AMATCH, // autocommand match name
9620 SPEC_SFLNUM, // script file line number
9621 SPEC_SID // script ID: <SNR>123_
9622#ifdef FEAT_CLIENTSERVER
9623 , SPEC_CLIENT
9624#endif
9625};
9626
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009628 * Check "str" for starting with a special cmdline variable.
9629 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9630 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9631 */
9632 int
Mike Williams51024bb2024-05-30 07:46:30 +02009633find_cmdline_var(char_u *src, size_t *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009634{
John Marriotted908f72024-04-18 22:46:56 +02009635 // must be sorted by the 'value' field because it is used by bsearch()!
9636 static keyvalue_T spec_str_tab[] = {
9637 KEYVALUE_ENTRY(SPEC_SID, "SID>"), // script ID: <SNR>123_
9638 KEYVALUE_ENTRY(SPEC_ABUF, "abuf>"), // autocommand buffer number
9639 KEYVALUE_ENTRY(SPEC_AFILE, "afile>"), // autocommand file name
9640 KEYVALUE_ENTRY(SPEC_AMATCH, "amatch>"), // autocommand match name
9641 KEYVALUE_ENTRY(SPEC_CCWORD, "cWORD>"), // cursor WORD
9642 KEYVALUE_ENTRY(SPEC_CEXPR, "cexpr>"), // expr under cursor
9643 KEYVALUE_ENTRY(SPEC_CFILE, "cfile>"), // cursor path name
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009644#ifdef FEAT_CLIENTSERVER
John Marriotted908f72024-04-18 22:46:56 +02009645 KEYVALUE_ENTRY(SPEC_CLIENT, "client>"),
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009646#endif
John Marriotted908f72024-04-18 22:46:56 +02009647 KEYVALUE_ENTRY(SPEC_CWORD, "cword>"), // cursor word
9648 KEYVALUE_ENTRY(SPEC_SCRIPT, "script>"), // script file name
9649 KEYVALUE_ENTRY(SPEC_SFILE, "sfile>"), // ":so" file name
9650 KEYVALUE_ENTRY(SPEC_SFLNUM, "sflnum>"), // script file line number
9651 KEYVALUE_ENTRY(SPEC_SLNUM, "slnum>"), // ":so" file line number
9652 KEYVALUE_ENTRY(SPEC_STACK, "stack>") // call stack
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009653 };
John Marriotted908f72024-04-18 22:46:56 +02009654 keyvalue_T target;
9655 keyvalue_T *entry;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009656
John Marriotted908f72024-04-18 22:46:56 +02009657 switch (*src)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009658 {
John Marriotted908f72024-04-18 22:46:56 +02009659 case '%':
9660 *usedlen = 1;
9661 return SPEC_PERC;
9662
9663 case '#':
9664 *usedlen = 1;
9665 return SPEC_HASH;
9666
9667 case '<':
9668 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01009669 target.value.string = src + 1; // skip over '<'
9670 target.value.length = 0; // not used, see cmp_keyvalue_value_n()
John Marriotted908f72024-04-18 22:46:56 +02009671
John Marriott8d4477e2024-11-02 15:59:01 +01009672 entry = (keyvalue_T *)bsearch(&target, &spec_str_tab,
9673 ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]),
9674 cmp_keyvalue_value_n);
John Marriotted908f72024-04-18 22:46:56 +02009675 if (entry == NULL)
9676 return -1;
9677
John Marriott8d4477e2024-11-02 15:59:01 +01009678 *usedlen = entry->value.length + 1;
John Marriotted908f72024-04-18 22:46:56 +02009679 return entry->key;
9680
9681 default:
9682 break;
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009683 }
John Marriotted908f72024-04-18 22:46:56 +02009684
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009685 return -1;
9686}
9687
9688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 * Evaluate cmdline variables.
9690 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009691 * change "%" to curbuf->b_ffname
9692 * "#" to curwin->w_alt_fnum
9693 * "%%" to curwin->w_alt_fnum in Vim9 script
9694 * "<cword>" to word under the cursor
9695 * "<cWORD>" to WORD under the cursor
9696 * "<cexpr>" to C-expression under the cursor
9697 * "<cfile>" to path name under the cursor
9698 * "<sfile>" to sourced file name
9699 * "<stack>" to call stack
Bram Moolenaar60895f32022-04-12 14:23:19 +01009700 * "<script>" to current script name
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009701 * "<slnum>" to sourced file line number
9702 * "<afile>" to file name for autocommand
9703 * "<abuf>" to buffer number for autocommand
9704 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 *
9706 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9707 * "" for error without a message) and NULL is returned.
9708 * Returns an allocated string if a valid match was found.
9709 * Returns NULL if no match was found. "usedlen" then still contains the
9710 * number of characters to skip.
9711 */
9712 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009713eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009714 char_u *src, // pointer into commandline
9715 char_u *srcstart, // beginning of valid memory for src
Mike Williams51024bb2024-05-30 07:46:30 +02009716 size_t *usedlen, // characters after src that are used
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009717 linenr_T *lnump, // line number for :e command, or NULL
9718 char **errormsg, // pointer to error message
Bram Moolenaara96edb72022-04-28 17:52:24 +01009719 int *escaped, // return value has escaped white space (can
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009720 // be NULL)
Bram Moolenaara96edb72022-04-28 17:52:24 +01009721 int empty_is_error) // empty result is considered an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722{
9723 int i;
9724 char_u *s;
9725 char_u *result;
9726 char_u *resultbuf = NULL;
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +01009727 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009729 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009731 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734
9735 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009736 if (escaped != NULL)
9737 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738
9739 /*
9740 * Check if there is something to do.
9741 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009742 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009743 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 {
9745 *usedlen = 1;
9746 return NULL;
9747 }
9748
9749 /*
9750 * Skip when preceded with a backslash "\%" and "\#".
9751 * Note: In "\\%" the % is also not recognized!
9752 */
9753 if (src > srcstart && src[-1] == '\\')
9754 {
9755 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009756 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 return NULL;
9758 }
9759
9760 /*
9761 * word or WORD under cursor
9762 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009763 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9764 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009766 resultlen = find_ident_under_cursor(&result,
9767 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9768 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9769 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 if (resultlen == 0)
9771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009772 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 return NULL;
9774 }
9775 }
9776
9777 /*
9778 * '#': Alternate file name
9779 * '%': Current file name
9780 * File name under the cursor
9781 * File name for autocommand
9782 * and following modifiers
9783 */
9784 else
9785 {
Mike Williams51024bb2024-05-30 07:46:30 +02009786 size_t off = 0;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009787
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 switch (spec_idx)
9789 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009790 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009791#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009792 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009795 // '%': current file
9796 if (curbuf->b_fname == NULL)
9797 {
9798 result = (char_u *)"";
9799 valid = 0; // Must have ":p:h" to be valid
9800 }
9801 else
9802 {
9803 result = curbuf->b_fname;
9804 tilde_file = STRCMP(result, "~") == 0;
9805 }
9806 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009808#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009809 // "%%" alternate file
9810 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009811#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009812 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009813 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009814 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009816 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 result = arg_all();
9818 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009819 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009820 if (escaped != NULL)
9821 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 break;
9824 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009825 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009826 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009827 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009829 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009830 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009831 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009832 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009834 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009836 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009838 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009839 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009840 return NULL;
9841 }
9842#ifdef FEAT_EVAL
9843 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9844 (long)i);
9845 if (result == NULL)
9846 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009847 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009848 return NULL;
9849 }
9850#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009851 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 }
9855 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009856 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009857 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9858 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009859 buf = buflist_findnr(i);
9860 if (buf == NULL)
9861 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009862 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009863 return NULL;
9864 }
9865 if (lnump != NULL)
9866 *lnump = ECMD_LAST;
9867 if (buf->b_fname == NULL)
9868 {
9869 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009870 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009871 }
9872 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009873 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009874 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009875 tilde_file = STRCMP(result, "~") == 0;
9876 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 break;
9879
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009880 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009881 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 if (result == NULL)
9883 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009884 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 return NULL;
9886 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009887 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009890 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009892 if (result != NULL && !autocmd_fname_full)
9893 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009894 // Still need to turn the fname into a full path. It is
9895 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009896 autocmd_fname_full = TRUE;
9897 result = FullName_save(autocmd_fname, FALSE);
9898 vim_free(autocmd_fname);
9899 autocmd_fname = result;
9900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 if (result == NULL)
9902 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009903 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 return NULL;
9905 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009906 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 break;
9908
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009909 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 if (autocmd_bufnr <= 0)
9911 {
Bram Moolenaar70e80282023-05-05 22:58:34 +01009912 *errormsg = _(e_no_autocommand_buffer_number_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 return NULL;
9914 }
9915 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9916 result = strbuf;
9917 break;
9918
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009919 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 result = autocmd_match;
9921 if (result == NULL)
9922 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009923 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 return NULL;
9925 }
9926 break;
9927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009928 case SPEC_SFILE: // file name for ":so" command
LemonBoy6013d002022-04-09 21:42:10 +01009929 result = estack_sfile(ESTACK_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (result == NULL)
9931 {
LemonBoy6013d002022-04-09 21:42:10 +01009932 *errormsg = _(e_no_source_file_name_to_substitute_for_sfile);
9933 return NULL;
9934 }
9935 resultbuf = result; // remember allocated string
9936 break;
9937 case SPEC_STACK: // call stack
9938 result = estack_sfile(ESTACK_STACK);
9939 if (result == NULL)
9940 {
9941 *errormsg = _(e_no_call_stack_to_substitute_for_stack);
9942 return NULL;
9943 }
9944 resultbuf = result; // remember allocated string
9945 break;
9946 case SPEC_SCRIPT: // script file name
9947 result = estack_sfile(ESTACK_SCRIPT);
9948 if (result == NULL)
9949 {
9950 *errormsg = _(e_no_script_file_name_to_substitute_for_script);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 return NULL;
9952 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009953 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009955
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009956 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009957 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009958 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009959 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009960 return NULL;
9961 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009962 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009963 result = strbuf;
9964 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009965
9966#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009967 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009968 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009969 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009970 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009971 return NULL;
9972 }
9973 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009974 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009975 result = strbuf;
9976 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009977
Bram Moolenaar90944302020-08-01 20:45:11 +02009978 case SPEC_SID:
9979 if (current_sctx.sc_sid <= 0)
9980 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009981 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009982 return NULL;
9983 }
9984 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9985 result = strbuf;
9986 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009987#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009988
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009989#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009990 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009991 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9992 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 result = strbuf;
9994 break;
9995#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009996
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009997 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009998 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009999 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 }
10001
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010002 resultlen = STRLEN(result); // length of new string
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010003 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 {
10005 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Yegappan Lakshmanan00d34592024-12-25 10:20:51 +010010007 resultlen = s - result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 else if (!skip_mod)
10010 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010011 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 &resultlen);
10013 if (result == NULL)
10014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010015 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 return NULL;
10017 }
10018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 }
10020
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010021 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 {
Bram Moolenaar211a5bb2022-04-28 19:09:03 +010010023 if (empty_is_error)
10024 {
10025 if (valid != VALID_HEAD + VALID_PATH)
10026 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
10027 else
10028 *errormsg = _(e_evaluates_to_an_empty_string);
10029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 result = NULL;
10031 }
10032 else
10033 result = vim_strnsave(result, resultlen);
10034 vim_free(resultbuf);
10035 return result;
10036}
10037
10038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 * Expand the <sfile> string in "arg".
10040 *
10041 * Returns an allocated string, or NULL for any error.
10042 */
10043 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010044expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010046 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 char_u *result;
John Marriotted908f72024-04-18 22:46:56 +020010048 size_t resultlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 char_u *newres;
John Marriotted908f72024-04-18 22:46:56 +020010050 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 char_u *repl;
John Marriotted908f72024-04-18 22:46:56 +020010052 size_t repllen;
Mike Williams51024bb2024-05-30 07:46:30 +020010053 size_t srclen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 char_u *p;
10055
John Marriotted908f72024-04-18 22:46:56 +020010056 resultlen = STRLEN(arg);
10057 result = vim_strnsave(arg, resultlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 if (result == NULL)
10059 return NULL;
10060
10061 for (p = result; *p; )
10062 {
10063 if (STRNCMP(p, "<sfile>", 7) != 0)
10064 ++p;
10065 else
10066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010067 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaara96edb72022-04-28 17:52:24 +010010068 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (errormsg != NULL)
10070 {
10071 if (*errormsg)
10072 emsg(errormsg);
10073 vim_free(result);
10074 return NULL;
10075 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010076 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 {
10078 p += srclen;
10079 continue;
10080 }
John Marriotted908f72024-04-18 22:46:56 +020010081 repllen = STRLEN(repl);
10082 resultlen += repllen - srclen;
10083 newres = alloc(resultlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 if (newres == NULL)
10085 {
10086 vim_free(repl);
10087 vim_free(result);
10088 return NULL;
10089 }
John Marriotted908f72024-04-18 22:46:56 +020010090 len = p - result;
10091 mch_memmove(newres, result, len);
10092 STRCPY(newres + len, repl);
10093 len += repllen;
10094 STRCPY(newres + len, p + srclen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 vim_free(repl);
10096 vim_free(result);
10097 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010098 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 }
10100 }
10101
10102 return result;
10103}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010106/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010107 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010108 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010111dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 if (fname == NULL)
10114 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010115 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116}
10117#endif
10118
10119/*
10120 * ":behave {mswin,xterm}"
10121 */
10122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010123ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124{
10125 if (STRCMP(eap->arg, "mswin") == 0)
10126 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010127 set_option_value_give_err((char_u *)"selection",
10128 0L, (char_u *)"exclusive", 0);
10129 set_option_value_give_err((char_u *)"selectmode",
10130 0L, (char_u *)"mouse,key", 0);
10131 set_option_value_give_err((char_u *)"mousemodel",
10132 0L, (char_u *)"popup", 0);
10133 set_option_value_give_err((char_u *)"keymodel",
10134 0L, (char_u *)"startsel,stopsel", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 }
10136 else if (STRCMP(eap->arg, "xterm") == 0)
10137 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +010010138 set_option_value_give_err((char_u *)"selection",
10139 0L, (char_u *)"inclusive", 0);
10140 set_option_value_give_err((char_u *)"selectmode", 0L, (char_u *)"", 0);
10141 set_option_value_give_err((char_u *)"mousemodel",
10142 0L, (char_u *)"extend", 0);
10143 set_option_value_give_err((char_u *)"keymodel", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 }
10145 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010146 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147}
10148
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149static int filetype_detect = FALSE;
10150static int filetype_plugin = FALSE;
10151static int filetype_indent = FALSE;
10152
10153/*
10154 * ":filetype [plugin] [indent] {on,off,detect}"
10155 * on: Load the filetype.vim file to install autocommands for file types.
10156 * off: Load the ftoff.vim file to remove all autocommands for file types.
10157 * plugin on: load filetype.vim and ftplugin.vim
10158 * plugin off: load ftplugof.vim
10159 * indent on: load filetype.vim and indent.vim
10160 * indent off: load indoff.vim
10161 */
10162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010163ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164{
10165 char_u *arg = eap->arg;
10166 int plugin = FALSE;
10167 int indent = FALSE;
10168
10169 if (*eap->arg == NUL)
10170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010171 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010172 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 filetype_detect ? "ON" : "OFF",
10174 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10175 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10176 return;
10177 }
10178
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010179 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 for (;;)
10181 {
10182 if (STRNCMP(arg, "plugin", 6) == 0)
10183 {
10184 plugin = TRUE;
10185 arg = skipwhite(arg + 6);
10186 continue;
10187 }
10188 if (STRNCMP(arg, "indent", 6) == 0)
10189 {
10190 indent = TRUE;
10191 arg = skipwhite(arg + 6);
10192 continue;
10193 }
10194 break;
10195 }
10196 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10197 {
10198 if (*arg == 'o' || !filetype_detect)
10199 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010200 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 filetype_detect = TRUE;
10202 if (plugin)
10203 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010204 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 filetype_plugin = TRUE;
10206 }
10207 if (indent)
10208 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010209 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 filetype_indent = TRUE;
10211 }
10212 }
10213 if (*arg == 'd')
10214 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010215 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010216 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 }
10218 }
10219 else if (STRCMP(arg, "off") == 0)
10220 {
10221 if (plugin || indent)
10222 {
10223 if (plugin)
10224 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010225 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 filetype_plugin = FALSE;
10227 }
10228 if (indent)
10229 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010230 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 filetype_indent = FALSE;
10232 }
10233 }
10234 else
10235 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010236 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 filetype_detect = FALSE;
10238 }
10239 }
10240 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000010241 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242}
10243
10244/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010245 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 */
10247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010248ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
zeertzjq5bf6c212024-03-31 18:41:27 +020010250 if (curbuf->b_did_filetype)
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010251 return;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010252
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010253 char_u *arg = eap->arg;
10254 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10255 arg += 9;
Bram Moolenaar3e545692017-06-04 19:00:32 +020010256
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +000010257 set_option_value_give_err((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10258 if (arg != eap->arg)
zeertzjq5bf6c212024-03-31 18:41:27 +020010259 curbuf->b_did_filetype = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010263ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264{
10265#ifdef FEAT_DIGRAPHS
10266 if (*eap->arg != NUL)
10267 putdigraph(eap->arg);
10268 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010269 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270#else
mityu61065042021-07-19 20:07:21 +020010271 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272#endif
10273}
10274
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010275#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10276 void
10277set_no_hlsearch(int flag)
10278{
10279 no_hlsearch = flag;
10280# ifdef FEAT_EVAL
10281 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10282# endif
10283}
10284
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285/*
10286 * ":nohlsearch"
10287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010289ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010291 set_no_hlsearch(TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +010010292 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294#endif
10295
10296#ifdef FEAT_CRYPT
10297/*
10298 * ":X": Get crypt key
10299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010301ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010303 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010304 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305}
10306#endif
10307
10308#ifdef FEAT_FOLDING
10309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010310ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311{
10312 if (foldManualAllowed(TRUE))
10313 foldCreate(eap->line1, eap->line2);
10314}
10315
10316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010317ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318{
10319 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10320 eap->forceit, FALSE);
10321}
10322
10323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010324ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325{
10326 linenr_T lnum;
10327
Bram Moolenaar4facea32019-10-12 20:17:40 +020010328# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010329 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010330# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010331
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010332 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10334 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10335 ml_setmarked(lnum);
10336
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010337 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010010339 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +020010340# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010341 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +020010342# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343}
10344#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010346#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +020010347/*
10348 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10349 * instead of a quickfix command.
10350 */
10351 int
10352is_loclist_cmd(int cmdidx)
10353{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010354 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010355 return FALSE;
10356 return cmdnames[cmdidx].cmd_name[0] == 'l';
10357}
10358#endif
10359
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010360 int
10361get_pressedreturn(void)
10362{
10363 return ex_pressedreturn;
10364}
10365
10366 void
10367set_pressedreturn(int val)
10368{
10369 ex_pressedreturn = val;
10370}